Real structure
June 6, 2026 ยท View on GitHub
The Real structure provides arithmetic, comparison, conversion, and
classification operations for IEEE 754 double-precision floating-point
numbers.
Specified by the Standard ML Basis Library.
Synopsis
type real val radix : int val precision : int val maxFinite : real val minPos : real val minNormalPos : real val posInf : real val negInf : real val + : real * real -> real val - : real * real -> real val * : real * real -> real val / : real * real -> real val rem : real * real -> real val ~ : real -> real val abs : real -> real val min : real * real -> real val max : real * real -> real val sign : real -> int val signBit : real -> bool val sameSign : real * real -> bool val copySign : real * real -> real val compare : real * real -> order val < : real * real -> bool val <= : real * real -> bool val > : real * real -> bool val >= : real * real -> bool val = : real * real -> bool val <> : real * real -> bool val unordered : real * real -> bool val isFinite : real -> bool val isNan : real -> bool val isNormal : real -> bool val toManExp : real -> {man: real, exp: int} val fromManExp : {man: real, exp: int} -> real val split : real -> {whole: real, frac: real} val realMod : real -> real val checkFloat : real -> real val realFloor : real -> real val realCeil : real -> real val realTrunc : real -> real val realRound : real -> real val floor : real -> int val ceil : real -> int val trunc : real -> int val round : real -> int val fromInt : int -> real val fmt : realfmt -> real -> string val toString : real -> string val fromString : string -> real option
type real
is the type of IEEE 754 double-precision floating-point numbers.
radix
radix is the base of the representation, e.g., 2 or 10 for IEEE
floating point.
precision
precision is the number of digits, each between 0 and radix - 1,
in the mantissa. Note that the precision includes the implicit (or
hidden) bit used in the IEEE representation (e.g., the value of
Real64.precision is 53).
maxFinite
maxFinite is the maximum finite number.
minPos
minPos is the minimum non-zero positive number.
minNormalPos
minNormalPos is the minimum non-zero normalized number.
posInf
posInf is the positive infinity value.
negInf
negInf is the negative infinity value.
+
r1 + r2 is the sum of r1 and r2. If one argument is finite and
the other infinite, the result is infinite with the correct sign,
e.g., 5 - (-infinity) = infinity. We also have infinity + infinity =
infinity and (-infinity) + (-infinity) = (-infinity). Any other
combination of two infinities produces NaN.
-
r1 - r2 is the difference of r1 and r2. If one argument is
finite and the other infinite, the result is infinite with the correct
sign, e.g., 5 - (-infinity) = infinity. We also have infinity +
infinity = infinity and (-infinity) + (-infinity) = (-infinity). Any
other combination of two infinities produces NaN.
*
r1 * r2 is the product of r1 and r2. The product of zero and an
infinity produces NaN. Otherwise, if one argument is infinite, the
result is infinite with the correct sign, e.g., -5 * (-infinity) =
infinity, infinity * (-infinity) = -infinity.
/
r1 / r2 is the quotient of r1 and r2. We have 0 / 0 = NaN and
+-infinity / +-infinity = NaN. Dividing a finite, non-zero number by a
zero, or an infinity by a finite number produces an infinity with the
correct sign. (Note that zeros are signed.) A finite number divided by
an infinity is 0 with the correct sign.
rem
rem (x, y) (or x.rem y) returns the remainder x - n * y, where n = trunc (x / y). The result has the same sign as x and has absolute value less
than the absolute value of y. If x is an infinity or y is 0,
rem returns NaN. If y is an infinity, rem returns x.
~
~ r returns the negation of r.
abs
abs r (or r.abs ()) returns the absolute value of r.
min
min (x, y) (or x.min y) returns the smaller of the arguments. If exactly one
argument is NaN, returns the other argument. If both arguments are
NaN, returns NaN.
max
max (x, y) (or x.max y) returns the larger of the arguments. If exactly one
argument is NaN, returns the other argument. If both arguments are
NaN, returns NaN.
sign
sign r (or r.sign ()) returns ~1 if r is negative, 0 if r is zero, or 1 if r is
positive. An infinity returns its sign; a zero returns 0 regardless of
its sign. It raises Domain on NaN.
signBit
signBit r (or r.signBit ()) returns true if and only if the sign of r (infinities,
zeros, and NaN, included) is negative.
sameSign
sameSign (r1, r2) (or r1.sameSign r2) returns true if and only if signBit r1 equals
signBit r2.
copySign
copySign (x, y) (or x.copySign y) returns x with the sign of y, even if y is
NaN.
compare
compare (x, y) (or x.compare y) returns LESS, EQUAL, or GREATER according to
whether its first argument is less than, equal to, or greater than the
second. It raises IEEEReal.Unordered on unordered arguments.
<
x < y returns true if x is less than y. Return false on unordered
arguments, i.e., if either argument is NaN, so that the usual reversal
of comparison under negation does not hold, e.g., a < b is not the
same as not (a >= b).
<=
x <= y As "<"
>
x > y As "<"
>=
x >= y As "<"
=
x = y returns true if x and y are equal.
<>
x <> y returns true if x and y are not equal.
unordered
unordered (x, y) (or x.unordered y) returns true if x and y are unordered, i.e., at
least one of x and y is NaN.
isFinite
isFinite x (or x.isFinite ()) returns true if x is neither NaN nor an infinity.
isNan
isNan x (or x.isNan ()) returns true if x NaN.
isNormal
isNormal x (or x.isNormal ()) returns true if x is normal, i.e., neither zero,
subnormal, infinite nor NaN.
toManExp
toManExp r (or r.toManExp ()) returns {man, exp}, where man and exp are the
mantissa and exponent of r, respectively.
fromManExp
fromManExp r returns {man, exp}, where man and exp are the
mantissa and exponent of r, respectively.
split
split r (or r.split ()) returns {frac, whole}, where frac and whole are the
fractional and integral parts of r, respectively. Specifically,
whole is integral, and abs frac < 1.0.
realMod
realMod r (or r.realMod ()) returns the fractional parts of r; realMod is
equivalent to #frac o split.
checkFloat
checkFloat x (or x.checkFloat ()) raises Overflow if x is an infinity, and raises Div
if x is NaN. Otherwise, it returns its argument.
realFloor
realFloor r (or r.realFloor ()) produces floor(r), the largest integer not larger than
r.
realCeil
realCeil r (or r.realCeil ()) produces ceil(r), the smallest integer not less than
r.
realTrunc
realTrunc r (or r.realTrunc ()) rounds r towards zero.
realRound
realRound r (or r.realRound ()) rounds to the integer-valued real value that is nearest
to r. In the case of a tie, it rounds to the nearest even integer.
floor
floor r (or r.floor ()) produces floor(r), the largest int not larger than r.
ceil
ceil r (or r.ceil ()) produces ceil(r), the smallest int not less than r.
trunc
trunc r (or r.trunc ()) rounds r towards zero.
round
round r (or r.round ()) yields the integer nearest to r. In the case of a tie, it
rounds to the nearest even integer.
fromInt
fromInt i converts the integer i to a real value. If the
absolute value of i is larger than maxFinite, then the appropriate
infinity is returned. If i cannot be exactly represented as a real
value, uses current rounding mode to determine the resulting value.
fmt
fmt spec r (or spec.fmt r) converts a real into a string according to spec. Raises
Size when fmt spec is evaluated if spec is an invalid
precision (negative for SCI or FIX, less than 1 for GEN).
toString
toString r (or r.toString ()) converts a real into a string; equivalent to (fmt (StringCvt.GEN NONE) r)
fromString
fromString s scans a real value from a string. Returns SOME (r)
if a real value can be scanned from a prefix of s, ignoring any
initial whitespace; otherwise, it returns NONE. This function is
equivalent to StringCvt.scanString scan.