IEEEReal
structureThe 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.
signature IEEE_REAL
structure IEEEReal
: IEEE_REAL
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
exception Unordered
datatype real_order
datatype nan_mode
datatype float_class
datatype rounding_mode
setRoundingMode mode
getRoundingMode ()
type decimal_approx
kind
field indicates the real class. If sign
is true
, the number is negative. The integers in the digits
list must be digits, i.e., between 0 and 9.
When kind
is NORMAL
or SUBNORMAL
, a value of of type decimal_approx
with digits
= [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 if sign
is true
and 1 otherwise. When kind
is ZERO
or INF
, the value corresponds to zero or infinity, respectively, with its sign determined by sign
. When kind
is NAN _
, 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 if sign
is true
.
toString d
digits
= [d_{(1)}, d_{(2)}, ..., d_{(n)}] and ignoring the sign
and exp
fields, toString
generates the following strings depending on the kind
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)}) |
sign
field is true
, a #"~"
is prepended. If the exp
field is non-zero and the kind
is NORMAL or SUBNORMAL, the string "E"^(Integer.toString exp)
is appended.
toString o REAL.toDecimal
is equivalent to REAL.fmt IEEEReal.EXACT
.
fromString s
SOME r
if the decimal approximation r
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 the
sign
field, with a default of false
. In the first case, kind
is set to INF, digits = []
and exp = 0
. In the second case, kind
is set to NAN and exp = 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 lists il
and fl
, respectively, of integers. If no digits are left, i.e., both lists are empty, kind
is set to ZERO, digits = []
and exp = 0
. Otherwise, kind
is set to NORMAL, digits = il@fl
, and exp
is set to the length of il
plus the value of the exponent, if it exists.
REAL, MATH
Last Modified January 28, 1997
Comments to John Reppy.
Copyright © 1997 Bell Labs, Lucent Technologies