diff --git a/basis/Real.sml b/basis/Real.sml index 6de03187..c8e25496 100644 --- a/basis/Real.sml +++ b/basis/Real.sml @@ -1,979 +1,959 @@ (* Title: Standard Basis Library: Real and Real32 structures. Author: David Matthews Copyright David Matthews 2000, 2005, 2008, 2016-18, 2023 This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License version 2.1 as published by the Free Software Foundation. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *) local (* Functions common to Real and Real32 *) local open StringCvt in (* Zero padding. Handle some of the shorter case to avoid too much concatentation. *) fun padZero 0 = "" | padZero 1 = "0" | padZero 2 = "00" | padZero 3 = "000" | padZero 4 = "0000" | padZero 5 = "00000" | padZero 6 = "000000" | padZero 7 = "0000000" | padZero 8 = "00000000" | padZero n = if n < 0 then raise Size else "00000000" ^ padZero(n-8) (* How many digits will the mantissa take? It's possible to unroll this loop since the maximum for float is 9 and for double is 17. We want to avoid long-format arbitrary precision arithmetic as much as possible so we stop at 10 digits which is the maximum short-format power of ten in 32-bit mode. *) fun ndigits (i: LargeInt.int) = if i >= 1000000000 then ndigits(i div 1000000000) + 9 else if i >= 100000000 then 9 else if i >= 10000000 then 8 else if i >= 1000000 then 7 else if i >= 100000 then 6 else if i >= 10000 then 5 else if i >= 1000 then 4 else if i >= 100 then 3 else if i >= 10 then 2 else 1 (* Power of ten - unroll a few values. *) fun powerTen 0 = 1 | powerTen 1 = 10 | powerTen 2 = 100 | powerTen 3 = 1000 | powerTen 4 = 10000 | powerTen 5 = 100000 | powerTen 6 = 1000000 | powerTen n = if n < 0 then raise Size else 1000000 * powerTen(n-6) local (* PolyRealDoubleToDecimal returns an arbitrary precision number for the mantissa because it could be greater than 31 bits. In 64-bit mode it will always be short and we want to use fixed int arithmetic if we can. *) fun fixedDigitList (0, r) = r | fixedDigitList (n, r) = fixedDigitList (FixedInt.quot(n, 10), FixedInt.toInt(FixedInt.rem(n, 10)) :: r) in fun digitList(n, r) = if LibrarySupport.largeIntIsSmall n then fixedDigitList (FixedInt.fromLarge n, r) else let val (qu, rm) = IntInf.quotRem(n, 10) in digitList (qu, (Int.fromLarge rm) :: r) end end datatype realConv = RCSpecial of string | RCNormal of bool * int * LargeInt.int (* Common functions to convert real/Real32.real to strings *) (* "ndigs" is the number of digits after the decimal point. For sciFmt that means that "ndigs+1" digits are produced. For fixFmt at least "ndigs+1" digits are produced. If "ndigs" is 0 the value is printed as an integer without any ".0". In both cases ndigs > 0. *) (* These functions start with the exact representation and round if necessary by adding 0.5 to the last digit. Since the exact representation is itself a rounded value it's possible that this could result in double rounding. *) fun exactFmt convert r = case convert r of RCSpecial s => s | RCNormal (sign, exponent, mantissa) => let val s = if sign then "~" else "" val (e, exp) = if exponent = 0 then ("", "") else ("E", Int.toString exponent) in String.concat[s, "0.", LargeInt.toString mantissa, e, exp] end and fixFmt convert ndigs r = case convert r of RCSpecial s => s | RCNormal (sign, expo, mant) => let val signString = if sign then "~" else "" val digits = ndigits mant val (roundedMantissa, exp) = if digits-expo <= ndigs then (* No rounding necessary *) (mant, expo) else let val tens = powerTen(digits-expo-ndigs) val rounded = (mant + tens div 2) div tens in (* If we have rounded to zero the exponent is zero. We may also have rounded up a value of 9.999 to add an extra digit. *) if rounded = 0 then (0, 0) else (rounded, ndigits rounded - ndigs) end val mantissa = LargeInt.toString roundedMantissa val mantLength = String.size mantissa in if ndigs = 0 then (* No decimal point or anything after. *) ( if exp >= mantLength then String.concat[signString, mantissa, padZero(exp-mantLength)] else if exp <= 0 then String.concat[signString, "0"] else String.concat[signString, String.substring(mantissa, 0, exp)] ) else if exp >= mantLength then String.concat[signString, mantissa, padZero(exp-mantLength), ".", padZero ndigs] else if exp <= 0 then String.concat[signString, "0.", padZero(~exp), mantissa, padZero(ndigs-mantLength+exp)] else String.concat[signString, String.substring(mantissa, 0, exp), ".", String.substring(mantissa, exp, mantLength-exp), padZero(ndigs-mantLength+exp)] end (* sciFmt - always produces ndigs+1 significant digits *) and sciFmt convert ndigs r = case convert r of RCSpecial s => s | RCNormal (sign, expo, mant) => let val signString = if sign then "~" else "" val digits = ndigits mant val (roundedMantissa, exp) = if digits <= ndigs+1 then (* No rounding necessary *) (mant, expo-1) else let val tens = powerTen(digits-ndigs-1) val rounded = mant + tens div 2 (* It's possible that this could increase the number of digits and hence the exponent. e.g. 9.9999 -> 10.0 *) in (rounded, expo + ndigits rounded - digits - 1) end val mantissa = LargeInt.toString roundedMantissa val mantLength = String.size mantissa in if ndigs = 0 then (* No decimal point or anything after. *) String.concat[signString, String.substring(mantissa, 0, 1), "E", Int.toString exp] else String.concat[signString, String.substring(mantissa, 0, 1), ".", String.substring(mantissa, 1, Int.min(mantLength-1, ndigs)), padZero(Int.max(0, ndigs-mantLength+1)), "E", Int.toString exp] end (* General format - produces up to ndigs of output. No trailing zeros are produced except for any zeros before the DP. We also produce one ".0" if necessary so that the result looks like a real number rather than an int. *) and genFmt convert ndigs r = case convert r of RCSpecial s => s | RCNormal (sign, expo, mant) => let val signString = if sign then "~" else "" val digits = ndigits mant val (mantissa, exp) = if digits <= ndigs then (* No rounding necessary *) (LargeInt.toString mant, expo-1) else let val tens = powerTen(digits-ndigs) val rounded = mant + tens div 2 (* It's possible that this could increase the number of digits and hence the exponent. e.g. 9.9999 -> 10.0 We need to remove any trailing zeros produced. *) val asString = LargeInt.toString rounded fun stripZeros 0 = 1 | stripZeros n = if String.sub(asString, n-1) = #"0" then stripZeros(n-1) else n val sLength = stripZeros ndigs in (String.substring(asString, 0, sLength), expo + ndigits rounded - digits - 1) end val mantLength = String.size mantissa (* <= ndigs *) in if exp > ndigs orelse exp < ~5 (* Use E format. No zero padding. *) then ( if mantLength = 1 then String.concat[signString, String.substring(mantissa, 0, 1), "E", Int.toString exp] else String.concat[signString, String.substring(mantissa, 0, 1), ".", String.substring(mantissa, 1, mantLength-1), "E", Int.toString exp] ) else (* Fixed format *) if exp >= mantLength then String.concat[signString, mantissa, padZero(exp+1-mantLength), ".0"] else if exp+1 <= 0 then String.concat[signString, "0.", padZero(~exp-1), mantissa] else String.concat[signString, String.substring(mantissa, 0, exp+1), ".", if mantLength = exp+1 then "0" else String.substring(mantissa, exp+1, mantLength-exp-1)] end (* Note: The definition says, reasonably, that negative values for the number of digits raises Size. The tests also check for a very large value for the number of digits and seem to expect Size to be raised in that case. Note that the exception is raised when fmt spec is evaluated and before it is applied to an actual real argument. *) fun fmtFunction {sciFmt, ...} (SCI NONE) = sciFmt 6 | fmtFunction {sciFmt, ...} (SCI (SOME d) ) = if d < 0 orelse d > 200 then raise General.Size else sciFmt d | fmtFunction {fixFmt, ...} (FIX NONE) = fixFmt 6 | fmtFunction {fixFmt, ...} (FIX (SOME d) ) = if d < 0 orelse d > 200 then raise General.Size else fixFmt d | fmtFunction {genFmt, ...}(GEN NONE) = genFmt 12 | fmtFunction {genFmt, ...} (GEN (SOME d) ) = if d < 1 orelse d > 200 then raise General.Size else genFmt d | fmtFunction {exactFmt, ...} EXACT = exactFmt end open RealNumbersAsBits val floatMaxFiniteExp: FixedInt.int = 254 and doubleMaxFiniteExp: FixedInt.int = 2046 in structure Real: REAL = struct open IEEEReal val fromLargeInt: LargeInt.int -> real = Real.rtsCallFastI_R "PolyFloatArbitraryPrecision" val fromInt: int -> real = (* We have to select the appropriate conversion. This will be reduced down to the appropriate function but has to be type-correct whether int is arbitrary precision or fixed precision. Hence the "o Large/FixedInt.fromInt". *) if Bootstrap.intIsArbitraryPrecision then fromLargeInt o LargeInt.fromInt else Real.fromFixedInt o FixedInt.fromInt (* These are needed because we don't yet have conversion from string to real. They are filtered out by the signature. *) val zero = fromInt 0 and one = fromInt 1 and four = fromInt 4 type real = real (* Pick up from globals. *) structure Math: MATH = struct type real = real (* Pick up from globals. *) val sqrt = Real.rtsCallFastR_R "PolyRealSqrt" and sin = Real.rtsCallFastR_R "PolyRealSin" and cos = Real.rtsCallFastR_R "PolyRealCos" and atan = Real.rtsCallFastR_R "PolyRealArctan" and exp = Real.rtsCallFastR_R "PolyRealExp" and ln = Real.rtsCallFastR_R "PolyRealLog" and tan = Real.rtsCallFastR_R "PolyRealTan" and asin = Real.rtsCallFastR_R "PolyRealArcSin" and acos = Real.rtsCallFastR_R "PolyRealArcCos" and log10 = Real.rtsCallFastR_R "PolyRealLog10" and sinh = Real.rtsCallFastR_R "PolyRealSinh" and cosh = Real.rtsCallFastR_R "PolyRealCosh" and tanh = Real.rtsCallFastR_R "PolyRealTanh" val atan2 = Real.rtsCallFastRR_R "PolyRealAtan2" val pow = Real.rtsCallFastRR_R "PolyRealPow" (* Derived values. *) val e = exp one val pi = four * atan one end; infix 4 == != ?=; val op == = Real.== val op != : real * real -> bool = not o op == val radix = 2 val precision = 53 val maxFinite = doubleFromBinary{sign=false, exp=doubleMaxFiniteExp, mantissa = 0xFFFFFFFFFFFFF} val minNormalPos = doubleFromBinary{sign=false, exp=1, mantissa = 0} val minPos = doubleFromBinary{sign=false, exp=0, mantissa = 1} val posInf : real = one/zero val negInf : real = ~one/zero (* Real is LargeReal. *) fun toLarge (x: real) : (*LargeReal.*)real =x fun fromLarge (_ : IEEEReal.rounding_mode) (x: (*LargeReal.*)real): real = x (* isNan can be defined in terms of unordered. *) fun isNan x = Real.unordered(x, x) fun isFinite x = doubleExponent x <= doubleMaxFiniteExp (* This could be implemented using signBit and doubleFromBinary *) val copySign : (real * real) -> real = Real.rtsCallFastRR_R "PolyRealCopySign" val signBit = doubleSignBit fun isNormal x = let val exp = doubleExponent x in exp > 0 andalso exp <= doubleMaxFiniteExp end fun class x = let val exp = doubleExponent x in if exp > doubleMaxFiniteExp then ( if doubleMantissa x <> 0 then NAN else INF ) else if exp = 0 then ( if doubleMantissa x = 0 then ZERO else SUBNORMAL ) else NORMAL end fun sign x = if isNan x then raise General.Domain else if x == zero then 0 else if x < zero then ~1 else 1 fun sameSign (x, y) = signBit x = signBit y (* Returns the minimum. In the case where one is a NaN it returns the other. In that case the comparison will be false. *) fun min (a: real, b: real): real = if a < b orelse isNan b then a else b (* Similarly for max. *) fun max (a: real, b: real): real = if a > b orelse isNan b then a else b fun checkFloat x = if isFinite x then x else if isNan x then raise General.Div else raise General.Overflow local val frExp: real -> int * real = RunCall.rtsCallFull1 "PolyRealFrexp" val fromManAndExp: real*int -> real = Real.rtsCallFastRI_R "PolyRealLdexp" open Real in fun toManExp r = if not (isFinite r) orelse r == zero (* Nan, infinities and +/-0 all return r in the mantissa. We include 0 to preserve its sign. *) then {man=r, exp=0} else let val (exp, man) = frExp r in {man=man, exp=exp} end fun fromManExp {man, exp} = if not (isFinite man) orelse man == zero (* Nan, infinities and +/-0 in the mantissa all return their argument. *) then man else if LibrarySupport.isShortInt exp then fromManAndExp(man, exp) else (* Long arbitrary precision *) copySign(if Int.>(exp, 0) then posInf else zero, man) end (* Convert to integer. *) local (* The RTS function converts to at most a 64-bit value (even on 32-bits). That will convert all the bits of the mantissa but if the exponent is large we may have to multiply by some power of two. *) val realToInt: real -> LargeInt.int = RunCall.rtsCallFull1 "PolyRealBoxedToLongInt" (* These are defined to raise Domain rather than Overflow on Nans. *) fun checkNan x = if isNan x then raise Domain else x in val realFloor = Real.rtsCallFastR_R "PolyRealFloor" and realCeil = Real.rtsCallFastR_R "PolyRealCeil" and realTrunc = Real.rtsCallFastR_R "PolyRealTrunc" and realRound = Real.rtsCallFastR_R "PolyRealRound" fun toArbitrary x = if isNan x then raise General.Domain else if not (isFinite x) then raise General.Overflow else let val { man, exp } = toManExp x in if exp <= precision then realToInt x else IntInf.<< (realToInt(fromManExp{man=man, exp=precision}), Word.fromInt(exp - precision)) end fun toLargeInt IEEEReal.TO_NEGINF = toArbitrary o realFloor | toLargeInt IEEEReal.TO_POSINF = toArbitrary o realCeil | toLargeInt IEEEReal.TO_ZERO = toArbitrary o realTrunc | toLargeInt IEEEReal.TO_NEAREST = toArbitrary o realRound (* Conversions to FixedInt are put in by the compiler. If int is fixed we can use them otherwise we use the long versions. N.B. FixedInt.toInt is a no-op but is needed so this is type-correct when int is arbitrary. *) val floor = if Bootstrap.intIsArbitraryPrecision then LargeInt.toInt o toArbitrary o realFloor else FixedInt.toInt o Real.floorFix o checkNan and ceil = if Bootstrap.intIsArbitraryPrecision then LargeInt.toInt o toArbitrary o realCeil else FixedInt.toInt o Real.ceilFix o checkNan and trunc = if Bootstrap.intIsArbitraryPrecision then LargeInt.toInt o toArbitrary o realTrunc else FixedInt.toInt o Real.truncFix o checkNan and round = if Bootstrap.intIsArbitraryPrecision then LargeInt.toInt o toArbitrary o realRound else FixedInt.toInt o Real.roundFix o checkNan fun toInt IEEEReal.TO_NEGINF = floor | toInt IEEEReal.TO_POSINF = ceil | toInt IEEEReal.TO_ZERO = trunc | toInt IEEEReal.TO_NEAREST = round end; local val realConv: string->real = RunCall.rtsCallFull1 "PolyRealBoxedFromString" val posNan = abs(zero / zero) val negNan = ~posNan in fun fromDecimal { class = INF, sign=true, ...} = SOME negInf | fromDecimal { class = INF, sign=false, ...} = SOME posInf | fromDecimal { class = ZERO, sign=true, ...} = SOME (~ zero) | fromDecimal { class = ZERO, sign=false, ...} = SOME zero (* Generate signed Nans ignoring the digits and mantissa. There was code here to set the mantissa but there's no reference to that in the current version of the Basis library. *) | fromDecimal { class = NAN, sign=true, ... } = SOME negNan | fromDecimal { class = NAN, sign=false, ... } = SOME posNan | fromDecimal { class = _ (* NORMAL or SUBNORMAL *), sign, digits, exp} = (let fun toChar x = if x < 0 orelse x > 9 then raise General.Domain else Char.chr (x + Char.ord #"0") (* Turn the number into a string. *) val str = "0." ^ String.implode(List.map toChar digits) ^"E" ^ Int.toString exp (* Convert it to a real using the RTS conversion function. Change any Conversion exceptions into Domain. *) val result = realConv str handle RunCall.Conversion _ => raise General.Domain in if sign then SOME (~result) else SOME result end handle General.Domain => NONE ) end local - fun realToDecimal r = - let - val {sign, exponent, mantissa} = RealToDecimalConversion.d2decimal r - in - (sign, exponent + ndigits mantissa, mantissa) - end - (* We need to treat "nan" specially because IEEEReal.toString is defined to return ~nan for negative nans whereas Real.fmt is defined always to return "nan". This looks like an inconsistency in the definition but we follow it. *) fun realToRealConvert r = - if isNan r then RCSpecial "nan" - else if not (isFinite r) then if signBit r then RCSpecial "~inf" else RCSpecial "inf" - else RCNormal(realToDecimal r) + let + val {sign, exponent, mantissa, class} = RealToDecimalConversion.doubleToMinimal r + in + case class of + NAN => RCSpecial "nan" + | INF => if sign then RCSpecial "~inf" else RCSpecial "inf" + | _ (* NORMAL, ZERO, SUBNORMAL *) => + RCNormal(sign, exponent + ndigits mantissa, mantissa) + end in fun toDecimal r = let - val sign = signBit r - val kind = class r + val {sign, exponent, mantissa, class} = RealToDecimalConversion.doubleToMinimal r in - case kind of - ZERO => { class = ZERO, sign = sign, digits=[], exp = 0 } - | INF => { class = INF, sign = sign, digits=[], exp = 0 } - | NAN => { class = NAN, sign = sign, digits=[], exp = 0 } - | _ => (* NORMAL/SUBNORMAL *) - let - val (sign, exponent, mantissa) = realToDecimal r - in - { class = kind, sign = sign, exp = exponent, digits = digitList(mantissa, []) } - end + { class = class, sign = sign, exp = exponent + ndigits mantissa, digits = digitList(mantissa, []) } end val fmt = fmtFunction { sciFmt=sciFmt realToRealConvert, fixFmt=fixFmt realToRealConvert, genFmt=genFmt realToRealConvert, exactFmt=exactFmt realToRealConvert } val toString = fmt (StringCvt.GEN NONE) end (* Define these in terms of IEEEReal.scan since that deals with all the special cases. *) fun scan getc src = case IEEEReal.scan getc src of NONE => NONE | SOME (ieer, rest) => ( case fromDecimal ieer of NONE => NONE | SOME r => SOME(r, rest) ) val fromString = Option.composePartial (fromDecimal, IEEEReal.fromString) (* Converter to real values. This replaces the basic conversion function for reals installed in the bootstrap process. For more information see convInt in Int. *) local fun convReal (s: string) : real = let (* Set the rounding mode to TO_NEAREST whatever the current rounding mode. Otherwise the result of compiling a piece of code with a literal constant could depend on what the rounding mode was set to. We should always support TO_NEAREST. *) val oldRounding = IEEEReal.getRoundingMode() val () = IEEEReal.setRoundingMode IEEEReal.TO_NEAREST val scanResult = StringCvt.scanString scan s val () = IEEEReal.setRoundingMode oldRounding in case scanResult of NONE => raise RunCall.Conversion "Invalid real constant" | SOME res => res end in (* Install this as a conversion function for real literals. *) val (): unit = RunCall.addOverload convReal "convReal" end open Real (* Get the other definitions. *) fun compare (r1, r2) = if r1 == r2 then General.EQUAL else if r1 < r2 then General.LESS else if r1 > r2 then General.GREATER else raise Unordered fun compareReal (r1, r2) = if r1 == r2 then EQUAL else if r1 < r2 then LESS else if r1 > r2 then GREATER else UNORDERED (* This seems to be similar to == except that where == always returns false if either argument is a NaN this returns true. The implementation of == treats the unordered case specially so it would be possible to implement this in the same way. *) fun op ?= (x, y) = unordered(x, y) orelse x == y (* Although these may be built in in some architectures it's probably not worth treating them specially at the moment. *) fun *+ (x: real, y: real, z: real): real = x*y+z and *- (x: real, y: real, z: real): real = x*y-z val rem = Real.rtsCallFastRR_R "PolyRealRem" (* Split a real into whole and fractional parts. The fractional part must have the same sign as the number even if it is zero. *) fun split r = let val whole = realTrunc r val frac = r - whole in { whole = whole, frac = if not (isFinite r) then if isNan r then r else (* Infinity *) if r < zero then ~zero else zero else if frac == zero then if signBit r then ~zero else zero else frac } end (* Get the fractional part of a real. *) fun realMod r = #frac(split r) (* nextAfter: This was previously implemented in ML but, at the very least, needed to work with rounding to something other than TO_NEAREST. *) val nextAfter = Real.rtsCallFastRR_R "PolyRealNextAfter" end (* Real *) structure Real32: REAL where type real = Real32.real = (* Real32 uses some definitions from the Real structure above. *) struct open IEEEReal (* On both the X86 and ARM there is only a single conversion from double to float using the current rounding mode. If we want a specific rounding mode we need to set the rounding. *) fun fromLarge mode value = let val current = getRoundingMode() val () = setRoundingMode mode val result = Real32.fromReal value val () = setRoundingMode current in result end val fromRealRound = fromLarge TO_NEAREST (* Defined to use the current rounding mode. *) val fromLargeInt = Real32.fromReal o Real.fromLargeInt val fromInt: int -> Real32.real = (* We have to select the appropriate conversion. This will be reduced down to the appropriate function but has to be type-correct whether int is arbitrary precision or fixed precision. Hence the "o Large/FixedInt.fromInt". *) if Bootstrap.intIsArbitraryPrecision then fromLargeInt o LargeInt.fromInt else Real32.fromFixedInt o FixedInt.fromInt val zero = fromInt 0 and one = fromInt 1 and four = fromInt 4 val radix = 2 val precision = 24 val maxFinite = floatFromBinary{sign=false, exp=floatMaxFiniteExp, mantissa = 0x7FFFFF} val minNormalPos = floatFromBinary{sign=false, exp=1, mantissa = 0} val minPos = floatFromBinary{sign=false, exp=0, mantissa = 1} local open Real32 in val posInf : real = one/zero val negInf : real = ~one/zero val op != : real * real -> bool = not o op == end infix 4 == != ?=; (* isNan can be defined in terms of unordered. *) fun isNan x = Real32.unordered(x, x) fun isFinite x = floatExponent x <= floatMaxFiniteExp local open Real32 in val copySign : (real * real) -> real = rtsCallFastFF_F "PolyRealFCopySign" end val signBit = floatSignBit fun isNormal x = let val exp = floatExponent x in exp > 0 andalso exp <= floatMaxFiniteExp end fun class x = let val exp = floatExponent x in if exp > floatMaxFiniteExp then ( if floatMantissa x <> 0 then NAN else INF ) else if exp = 0 then ( if floatMantissa x = 0 then ZERO else SUBNORMAL ) else NORMAL end local open Real32 in fun sign x = if isNan x then raise General.Domain else if x == zero then 0 else if x < zero then ~1 else 1 end fun sameSign (x, y) = signBit x = signBit y local open Real32 in (* Returns the minimum. In the case where one is a NaN it returns the other. In that case the comparison will be false. *) fun min (a: real, b: real): real = if a < b orelse isNan b then a else b (* Similarly for max. *) fun max (a: real, b: real): real = if a > b orelse isNan b then a else b fun checkFloat x = if isFinite x then x else if isNan x then raise General.Div else raise General.Overflow (* On certain platforms e.g. mips, toLarge does not preserve the sign on nans. We deal with the non-finite cases here. *) (* Use the Real versions for the moment. *) fun toManExp r = if not (isFinite r) orelse r == zero (* Nan, infinities and +/-0 all return r in the mantissa. We include 0 to preserve its sign. *) then {man=r, exp=0} else let val {man, exp} = Real.toManExp(toLarge r) in {man = fromRealRound man, exp = exp } end and fromManExp {man, exp} = if not (isFinite man) orelse man == zero (* Nan, infinities and +/-0 in the mantissa all return their argument. *) then man else fromRealRound(Real.fromManExp{man=toLarge man, exp=exp}) fun compare (r1, r2) = if r1 == r2 then General.EQUAL else if r1 < r2 then General.LESS else if r1 > r2 then General.GREATER else raise Unordered fun compareReal (r1, r2) = if r1 == r2 then EQUAL else if r1 < r2 then LESS else if r1 > r2 then GREATER else UNORDERED fun op ?= (x, y) = unordered(x, y) orelse x == y (* Although these may be built in in some architectures it's probably not worth treating them specially at the moment. *) fun *+ (x: real, y: real, z: real): real = x*y+z and *- (x: real, y: real, z: real): real = x*y-z val realFloor = rtsCallFastF_F "PolyRealFFloor" and realCeil = rtsCallFastF_F "PolyRealFCeil" and realTrunc = rtsCallFastF_F "PolyRealFTrunc" and realRound = rtsCallFastF_F "PolyRealFRound" val rem = rtsCallFastFF_F "PolyRealFRem" (* Split a real into whole and fractional parts. The fractional part must have the same sign as the number even if it is zero. *) fun split r = let val whole = realTrunc r val frac = r - whole in { whole = whole, frac = if not (isFinite r) then if isNan r then r else (* Infinity *) if r < zero then ~zero else zero else if frac == zero then if signBit r then ~zero else zero else frac } end (* Get the fractional part of a real. *) fun realMod r = #frac(split r) val nextAfter = rtsCallFastFF_F "PolyRealFNextAfter" fun toLargeInt mode r = Real.toLargeInt mode (toLarge r) end local open Real32 (* These are defined to raise Domain rather than Overflow on Nans. *) fun checkNan x = if isNan x then raise Domain else x (* If int is fixed we use the hardware conversions otherwise we convert it to real and use the real to arbitrary conversions. *) in val floor = if Bootstrap.intIsArbitraryPrecision then LargeInt.toInt o toLargeInt IEEEReal.TO_NEGINF else FixedInt.toInt o floorFix o checkNan and ceil = if Bootstrap.intIsArbitraryPrecision then LargeInt.toInt o toLargeInt IEEEReal.TO_POSINF else FixedInt.toInt o ceilFix o checkNan and trunc = if Bootstrap.intIsArbitraryPrecision then LargeInt.toInt o toLargeInt IEEEReal.TO_ZERO else FixedInt.toInt o truncFix o checkNan and round = if Bootstrap.intIsArbitraryPrecision then LargeInt.toInt o toLargeInt IEEEReal.TO_NEAREST else FixedInt.toInt o roundFix o checkNan fun toInt IEEEReal.TO_NEGINF = floor | toInt IEEEReal.TO_POSINF = ceil | toInt IEEEReal.TO_ZERO = trunc | toInt IEEEReal.TO_NEAREST = round end (* Scan input source for a valid number. The format is the same as for double precision. Convert it using the current rounding mode. *) fun scan getc src = case Real.scan getc src of NONE => NONE | SOME (r, a) => SOME(Real32.fromReal r, a) val fromString = StringCvt.scanString scan - (* toDecimal: This is defined to return the shortest sequence so converting - it to double and then using Real.toDecimal gives the wrong result. - This now uses Ryu code specifically for 32-bit floats. *) local - fun floatToDecimal r = + fun floatToRealConvert r = let - val {sign, exponent, mantissa} = RealToDecimalConversion.f2decimal r + val {sign, exponent, mantissa, class} = RealToDecimalConversion.floatToMinimal r in - (sign, exponent + ndigits mantissa, mantissa) + case class of + NAN => RCSpecial "nan" + | INF => if sign then RCSpecial "~inf" else RCSpecial "inf" + | _ (* NORMAL, ZERO, SUBNORMAL *) => + RCNormal(sign, exponent + ndigits mantissa, mantissa) end - - fun floatToRealConvert r = - if isNan r then RCSpecial "nan" - else if not (isFinite r) then if signBit r then RCSpecial "~inf" else RCSpecial "inf" - else RCNormal(floatToDecimal r) in fun toDecimal r = - case class r of - ZERO => { class = ZERO, sign = signBit r, digits=[], exp = 0 } - | INF => { class = INF, sign = signBit r, digits=[], exp = 0 } - | NAN => { class = NAN, sign = signBit r, digits=[], exp = 0 } - | kind => - let - val (sign, exponent, mantissa) = floatToDecimal r - in - { class = kind, sign = sign, exp = exponent, digits = digitList(mantissa, []) } - end + let + val {sign, exponent, mantissa, class} = RealToDecimalConversion.floatToMinimal r + in + { class = class, sign = sign, exp = exponent + ndigits mantissa, digits = digitList(mantissa, []) } + end val fmt = fmtFunction { sciFmt=sciFmt floatToRealConvert, fixFmt=fixFmt floatToRealConvert, genFmt=genFmt floatToRealConvert, exactFmt=exactFmt floatToRealConvert } end val toString = fmt (StringCvt.GEN NONE) open Real32 (* Inherit the type and the built-in functions. *) (* Convert from decimal. This is defined to use TO_NEAREST. We need to handle NaNs specially because fromRealRound loses the sign on a NaN. *) local val posNan = abs(zero / zero) val negNan = ~posNan in fun fromDecimal { class = INF, sign=true, ...} = SOME negInf | fromDecimal { class = INF, sign=false, ...} = SOME posInf | fromDecimal { class = NAN, sign=true, ... } = SOME negNan | fromDecimal { class = NAN, sign=false, ... } = SOME posNan | fromDecimal arg = Option.map fromRealRound (Real.fromDecimal arg) end structure Math = struct type real = real val sqrt = rtsCallFastF_F "PolyRealFSqrt" and sin = rtsCallFastF_F "PolyRealFSin" and cos = rtsCallFastF_F "PolyRealFCos" and atan = rtsCallFastF_F "PolyRealFArctan" and exp = rtsCallFastF_F "PolyRealFExp" and ln = rtsCallFastF_F "PolyRealFLog" and tan = rtsCallFastF_F "PolyRealFTan" and asin = rtsCallFastF_F "PolyRealFArcSin" and acos = rtsCallFastF_F "PolyRealFArcCos" and log10 = rtsCallFastF_F "PolyRealFLog10" and sinh = rtsCallFastF_F "PolyRealFSinh" and cosh = rtsCallFastF_F "PolyRealFCosh" and tanh = rtsCallFastF_F "PolyRealFTanh" val atan2 = rtsCallFastFF_F "PolyRealFAtan2" val pow = rtsCallFastFF_F "PolyRealFPow" (* Derived values. *) val e = exp one val pi = four * atan one end (* Converter for literal constants. Copied from Real. *) local fun convReal (s: string) : real = let (* Set the rounding mode to TO_NEAREST whatever the current rounding mode. Otherwise the result of compiling a piece of code with a literal constant could depend on what the rounding mode was set to. We should always support TO_NEAREST. *) val oldRounding = IEEEReal.getRoundingMode() val () = IEEEReal.setRoundingMode IEEEReal.TO_NEAREST val scanResult = StringCvt.scanString scan s val () = IEEEReal.setRoundingMode oldRounding in case scanResult of NONE => raise RunCall.Conversion "Invalid real constant" | SOME res => res end in (* Install this as a conversion function for real literals. *) val (): unit = RunCall.addOverload convReal "convReal" end end (* Real32 *) end; structure Math = Real.Math; structure LargeReal: REAL = Real; (* Values available unqualified at the top-level. *) val real : int -> real = Real.fromInt val trunc : real -> int = Real.trunc val floor : real -> int = Real.floor val ceil : real -> int = Real.ceil val round : real -> int =Real.round; (* Overloads for Real32.real. The overloads for real were added in InitialBasis. *) val () = RunCall.addOverload Real32.>= ">=" and () = RunCall.addOverload Real32.<= "<=" and () = RunCall.addOverload Real32.> ">" and () = RunCall.addOverload Real32.< "<" and () = RunCall.addOverload Real32.+ "+" and () = RunCall.addOverload Real32.- "-" and () = RunCall.addOverload Real32.* "*" and () = RunCall.addOverload Real32.~ "~" and () = RunCall.addOverload Real32.abs "abs" and () = RunCall.addOverload Real32./ "/"; (* Install print functions. *) local fun print_real32 _ _ (r: Real32.real) = PolyML.PrettyString(Real32.fmt (StringCvt.GEN(SOME 7)) r) in val () = PolyML.addPrettyPrinter print_real32 end; local fun print_real _ _ (r: real) = PolyML.PrettyString(Real.fmt (StringCvt.GEN(SOME 10)) r) in val () = PolyML.addPrettyPrinter print_real; end; diff --git a/basis/RealToDecimalConversion.ML b/basis/RealToDecimalConversion.ML index 2649eead..092a9d9e 100644 --- a/basis/RealToDecimalConversion.ML +++ b/basis/RealToDecimalConversion.ML @@ -1,313 +1,317 @@ (* Title: Standard Basis Library: Conversion from floating point to decimal Author: David Matthews The underlying conversion code was translated from the C version of Ryu. That code is Copyright 2018 Ulf Adams and is licensed under the terms of the Apache License version 2.0 or Boost Software License, Version 1.0 Boost Software License - Version 1.0 - August 17th, 2003 Boost Licence Permission is hereby granted, free of charge, to any person or organization obtaining a copy of the software and accompanying documentation covered by this license (the "Software") to use, reproduce, display, distribute, execute, and transmit the Software, and to prepare derivative works of the Software, and to permit third-parties to whom the Software is furnished to do so, all subject to the following: The copyright notices in the Software and this entire statement, including the above license grant, this restriction and the following disclaimer, must be included in all copies of the Software, in whole or in part, and all derivative works of the Software, unless such copies or derivative works are solely in the form of machine-executable object code generated by a source language processor. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. The ML translation and related code is copyright David Matthews 2023 and is released under the Boost Licence. The remainder of the Poly/ML system is licensed under LGPL. *) (* This uses arbitrary precision arithmetic even for Real32.real (float) values. It would be possible to do some of the arithmetic using fixed precision integers but that's difficult on 32-bit platforms even for float. *) structure RealToDecimalConversion: sig - val f2decimal: Real32.real -> {sign:bool, exponent: int, mantissa: LargeInt.int} - val d2decimal: real -> {sign:bool, exponent: int, mantissa: LargeInt.int} + val floatToMinimal: + Real32.real -> {sign:bool, exponent: int, mantissa: LargeInt.int, class: IEEEReal.float_class} + val doubleToMinimal: + real -> {sign:bool, exponent: int, mantissa: LargeInt.int, class: IEEEReal.float_class} end = struct (* Common functions *) (* Returns floor(log10(2^e)) for values of e between 0 and 1650. *) fun log10Pow2 e = if e < 0 orelse e > 1650 then raise General.Domain else Int.quot(e * 78913, 0x40000) (* >> 18 *) (* Returns floor(log10(5^e)) for values of e between 0 and 2620 *) and log10Pow5 e = if e < 0 orelse e > 2620 then raise General.Domain else Int.quot(e * 732923, 0x100000) (* >> 20 *) fun pow5bits e = if e < 0 orelse e > 3528 then raise General.Domain else Int.quot(e * 1217359, 0x80000) (* >> 19 *) + 1 local (* Keep dividing by 5 while the remainder is zero *) fun p5 count value = if LargeInt.rem(value, 5) <> 0 then count else p5 (count+1) (LargeInt.quot(value, 5)) in (* Returns whether value is divisible by 5 to the power p. *) fun multipleOfPow5(value, e5) = p5 0 value >= e5 end fun multipleOfPowerOf2(value, p) = IntInf.andb(value, IntInf.<<(1, Word.fromInt p) - 1) = 0 local val posTableSize = 326 and invTableSize = 342 val pow5BitCount = 125 and pow5InvBitCount = 125 fun createInvSplit i = let val pow = IntInf.pow(5, i) val pow5len = IntInf.log2 pow + 1 (* Bit length *) val j = pow5len - 1 + pow5InvBitCount val pow5inv = IntInf.<<(1, Word.fromInt j) div pow + 1 in pow5inv end and createSplit i = let val pow = IntInf.pow(5, i) val pow5len = IntInf.log2 pow + 1 (* Bit length *) val shift = pow5len-pow5BitCount val pow5 = if shift < 0 then IntInf.<<(pow, Word.fromInt(~shift)) else IntInf.~>>(pow, Word.fromInt shift) in pow5 end val doublePow5InvSplit = Vector.tabulate(invTableSize, createInvSplit) and doublePow5Split = Vector.tabulate(posTableSize, createSplit) (* We don't have 64-bit arithmetic on 32-bit platforms so this uses arbitrary precision arithmetic. It might be possible to select different versions depending on the word length. *) fun mulShift(m: LargeInt.int, factor, shift: int): LargeInt.int = if shift <= 32 then raise Fail "mulShift32" else IntInf.~>>(factor*m, Word.fromInt shift) in fun mulPow5InvDivPow2(m, i, j) = mulShift(m, Vector.sub(doublePow5InvSplit, i), j) and mulPow5DivPow2(m, i, j) = mulShift(m, Vector.sub(doublePow5Split, i), j) val doublePow5InvBitCount = pow5InvBitCount and doublePow5BitCount = pow5BitCount end (* Apart from the first step the remainder is common. *) fun computeDecimal(e2: int, m2: LargeInt.int, mmShift, acceptBounds) = let (* Step 2: Determine the interval of valid decimal representations *) val mm = 4 * m2 - 1 - mmShift val mv = 4 * m2 val mp = 4 * m2 + 2 (* Step 3: Convert to a decimal power base *) val (e10, vr, vp, vm, lastRemovedDigit, vrIsTrailingZeros, vmIsTrailingZeros) = if e2 >= 0 then let val q = log10Pow2 e2 val e10 = q val k = doublePow5InvBitCount + pow5bits q - 1 val i = ~e2 + q + k val vr = mulPow5InvDivPow2(mv, q, i) and vp = mulPow5InvDivPow2(mp, q, i) and vm = mulPow5InvDivPow2(mm, q, i) in if q > 21 then (e10, vr, vp, vm, 0, false, false) (* Too large to be power of 5. *) else if LargeInt.rem(mv, 5) = 0 then (e10, vr, vp, vm, 0, multipleOfPow5(mv, q), false) else if acceptBounds then (e10, vr, vp, vm, 0, false, multipleOfPow5(mm, q)) else (e10, vr, vp - (if multipleOfPow5(mp, q) then 1 else 0), vm, 0, false, false) end else let val q = log10Pow5(~ e2) val e10 = q + e2 val i = ~e2 - q val k = pow5bits i - doublePow5BitCount val j = q - k val vr = mulPow5DivPow2(mv, i, j) and vp = mulPow5DivPow2(mp, i, j) and vm = mulPow5DivPow2(mm, i, j) val lastRemovedDigit = if q <> 0 andalso LargeInt.quot(vp-1, 10) <= LargeInt.quot(vm, 10) then let val j' = q-1-(pow5bits(i+1)-doublePow5BitCount) val lrm = LargeInt.rem(mulPow5DivPow2(mv, i+1, j'), 10) in lrm end else 0 in if q <= 1 then if acceptBounds then (e10, vr, vp, vm, lastRemovedDigit, true, mmShift = 1) else (e10, vr, vp-1, vm, lastRemovedDigit, true, false) else if q < 31 then (e10, vr, vp, vm, lastRemovedDigit, multipleOfPowerOf2(mv, q-1), false) else (e10, vr, vp, vm, lastRemovedDigit, false, false) end (* Step 4: Find the shortest decimal representation in the interval *) val (output, removed) = if vmIsTrailingZeros orelse vrIsTrailingZeros then let fun removeVrDigits(vr, vp, vm, removed, lastRemovedDigit, vmIsTrailingZeros, vrIsTrailingZeros) = let val vpDiv10 = LargeInt.quot(vp, 10) and vmDiv10 = LargeInt.quot(vm, 10) in if vpDiv10 > vmDiv10 then removeVrDigits(LargeInt.quot(vr, 10), vpDiv10, vmDiv10, removed+1, LargeInt.rem(vr, 10), vmIsTrailingZeros andalso LargeInt.rem(vm, 10) = 0, vrIsTrailingZeros andalso lastRemovedDigit = 0) else removeVmDigits(vr, vp, vm, removed, lastRemovedDigit, vmIsTrailingZeros, vrIsTrailingZeros) end and removeVmDigits(vr, vp, vm, removed, lastRemovedDigit, vmIsTrailingZeros, vrIsTrailingZeros) = let in if vmIsTrailingZeros andalso LargeInt.rem(vm, 10) = 0 then removeVmDigits(LargeInt.quot(vr, 10), LargeInt.quot(vp, 10), LargeInt.quot(vm, 10), removed+1, LargeInt.rem(vr, 10), vmIsTrailingZeros, vrIsTrailingZeros andalso lastRemovedDigit = 0) else let val lastRemovedDigit2 = if vrIsTrailingZeros andalso lastRemovedDigit = 5 andalso LargeInt.rem(vr, 2) = 0 then 4 (* Don't round up *) else lastRemovedDigit val vrCorrect = (vr = vm andalso (not acceptBounds orelse not vmIsTrailingZeros)) orelse lastRemovedDigit2 >= 5 in (vr + (if vrCorrect then 1 else 0), removed) end end in removeVrDigits(vr, vp, vm, 0, lastRemovedDigit, vmIsTrailingZeros, vrIsTrailingZeros) end else let fun removeDigits(vr, vp, vm, removed, lastRemovedDigit) = let val vpDiv10 = LargeInt.quot(vp, 10) and vmDiv10 = LargeInt.quot(vm, 10) in if vpDiv10 > vmDiv10 then removeDigits(LargeInt.quot(vr, 10), vpDiv10, vmDiv10, removed+1, LargeInt.rem(vr, 10)) else (vr + (if vr = vm orelse lastRemovedDigit >= 5 then 1 else 0), removed) end in removeDigits(vr, vp, vm, 0, lastRemovedDigit) end in {mantissa=output, exponent=e10+removed} end val doubleBias = 1023 (* This is the exponent value for 1.0 *) val doubleMantissaBits = 53 - 1 (* One bit is implicit *) val doubleImplicitBit = IntInf.<<(1, Word.fromInt doubleMantissaBits) val floatBias = 127 (* This is the exponent value for 1.0 *) val floatMantissaBits = 24 - 1 (* One bit is implicit *) val floatImplicitBit = Word.toInt(Word.<<(0w1, Word.fromInt floatMantissaBits)) - fun d2decimal(r: real): {sign:bool, exponent: int, mantissa: LargeInt.int} = + fun doubleToMinimal(r: real) = let - open RealNumbersAsBits + open RealNumbersAsBits IEEEReal val ieeeSign = doubleSignBit r and ieeeExponent = doubleExponent r and ieeeMantissa = doubleMantissa r in if ieeeExponent = 2047 - then raise General.Domain (* Infinities and NaN *) + then (* Non-finite *) + {sign=ieeeSign, exponent=0, mantissa=0, class=if ieeeMantissa = 0 then INF else NAN} else if ieeeExponent = 0 andalso ieeeMantissa = 0 - then {sign=ieeeSign, exponent=0, mantissa=0} + then {sign=ieeeSign, exponent=0, mantissa=0, class=ZERO} else let (* Step 1: Normalise the value. Normalised values, with exponent non-zero, have an implicit one in the top bit position. *) val (e2, m2) = if ieeeExponent = 0 then (1-doubleBias-doubleMantissaBits-2, ieeeMantissa) else (ieeeExponent-doubleBias-doubleMantissaBits-2, ieeeMantissa + doubleImplicitBit) val acceptBounds = LargeInt.rem(m2, 2) = 0 val mmShift = if ieeeMantissa <> 0 orelse ieeeExponent <= 1 then 1 else 0 val {mantissa, exponent} = computeDecimal(FixedInt.toInt e2, m2, mmShift, acceptBounds) in - {sign=ieeeSign, exponent=exponent, mantissa=mantissa} + {sign=ieeeSign, exponent=exponent, mantissa=mantissa, class=if ieeeExponent = 0 then SUBNORMAL else NORMAL} end end - and f2decimal(f: Real32.real): {sign:bool, exponent: int, mantissa: LargeInt.int} = + and floatToMinimal(f: Real32.real) = let - open RealNumbersAsBits + open RealNumbersAsBits IEEEReal val ieeeSign = floatSignBit f and ieeeExponent = floatExponent f and ieeeMantissa = floatMantissa f in if ieeeExponent = 255 - then raise General.Domain (* Infinities and NaN *) + then (* Non-finite *) + {sign=ieeeSign, exponent=0, mantissa=0, class=if ieeeMantissa = 0 then INF else NAN} else if ieeeExponent = 0 andalso ieeeMantissa = 0 - then {sign=ieeeSign, exponent=0, mantissa=0} + then {sign=ieeeSign, exponent=0, mantissa=0, class=ZERO} else let (* Step 1: Normalise the value. Normalised values, with exponent non-zero, have an implicit one in the top bit position. *) val (e2, m2) = if ieeeExponent = 0 then (1-floatBias-floatMantissaBits-2, ieeeMantissa) else (ieeeExponent-floatBias-floatMantissaBits-2, ieeeMantissa + floatImplicitBit) val isEven = Int.rem(m2, 2) = 0 val acceptBounds = isEven (* Step 2: Determine the interval of valid decimal representations (??) *) val mmShift = if ieeeMantissa <> 0 orelse ieeeExponent <= 1 then 1 else 0 val {mantissa, exponent} = computeDecimal(FixedInt.toInt e2, FixedInt.toLarge m2, mmShift, acceptBounds) in - {sign=ieeeSign, exponent=exponent, mantissa=mantissa} + {sign=ieeeSign, exponent=exponent, mantissa=mantissa, class=if ieeeExponent = 0 then SUBNORMAL else NORMAL} end end end;