The IEEEReal
structure
The IEEEReal structure defines types associated with an IEEE implementation of floating-point numbers. In addition, it provides control for the floating-point hardware's rounding mode. Refer to the IEEE standard 754-1985 and the ANSI/IEEE standard 854-1987 for additional information.
Synopsis
signature IEEE_REAL
structure IEEEReal
: IEEE_REAL
Interface
exception Unordered
datatype real_order = LESS | EQUAL | GREATER | UNORDERED
datatype nan_mode = QUIET | SIGNALLING
datatype float_class
= NAN of nan_mode
| INF
| ZERO
| NORMAL
| SUBNORMAL
datatype rounding_mode
= TO_NEAREST
| TO_NEGINF
| TO_POSINF
| TO_ZERO
val setRoundingMode : rounding_mode -> unit
val getRoundingMode : unit -> rounding_mode
type decimal_approx = { kind : float_class, sign : bool, digits : int list, exp : int }
val toString : decimal_approx -> string
val fromString : string -> decimal_approx option
Description
-
exception Unordered
-
-
datatype real_order
-
-
datatype nan_mode
-
-
datatype float_class
-
-
datatype rounding_mode
-
-
setRoundingMode mode
getRoundingMode ()
-
set and get the rounding mode of the underlying hardware.
-
type decimal_approx
-
provides a structured decimal representation of a real. The
kind
field indicates the real class. Ifsign
istrue
, the number is negative. The integers in thedigits
list must be digits, i.e., between 0 and 9.When
kind
isNORMAL
orSUBNORMAL
, a value of of typedecimal_approx
withdigits
= [d(1), d(2), ..., d(n)] corresponds to the real number s * 0.d(1)d(2)...d(n) 10(exp), where s is -1 ifsign
istrue
and 1 otherwise. Whenkind
isZERO
orINF
, the value corresponds to zero or infinity, respectively, with its sign determined bysign
. Whenkind
isNAN _
, the value corresponds to a NaN real value whose whose fraction bits are the binary equivalent of 0.d(1)d(2)...d(n) and whose sign bit is set ifsign
istrue
.
-
toString d
-
returns a string representation of d. Assuming
digits
= [d(1), d(2), ..., d(n)] and ignoring thesign
andexp
fields,toString
generates the following strings depending on thekind
field:
ZERO 0.0 NORMAL 0.d(1)d(2)...d(n) SUBNORMAL 0.d(1)d(2)...d(n) INF inf NAN _ nan(d(1)d(2)...d(n))
If thesign
field istrue
, a#"~"
is prepended. If theexp
field is non-zero and thekind
is NORMAL or SUBNORMAL, the string"E"^(Integer.toString exp)
is appended.toString o REAL.toDecimal
is equivalent toREAL.fmt IEEEReal.EXACT
.
-
fromString s
-
returns
SOME r
if the decimal approximationr
can be parsed from a prefix of string s, ignoring initial whitespace; NONE is returned if no such conversion is possible.fromString
accepts any of the following regular expressions:[+~-]?(INF|INFINITY) [+~-]?(NAN|NAN\([0-9]+\)) [+~-]?(([0-9]+(\.[0-9]+)?)|(\.[0-9]+))([eE][+~-]?[0-9]+)?
where alphabetic characters are case-insensitive. The optional sign determines the value of thesign
field, with a default offalse
. In the first case,kind
is set to INF,digits = []
andexp = 0
. In the second case,kind
is set to NAN andexp = 0
. If the optional digits are provided, they become the list of digits; otherwise,digits = []
. In the last case, initial 0's are stripped from the integer part and trailing 0's are stripped from the fractional part, yielding two listsil
andfl
, respectively, of integers. If no digits are left, i.e., both lists are empty,kind
is set to ZERO,digits = []
andexp = 0
. Otherwise,kind
is set to NORMAL,digits = il@fl
, andexp
is set to the length ofil
plus the value of the exponent, if it exists.
See Also
REAL, MATH