| 1 |
(* Copyright (C) 2022 Matthew Fluet. |
| 2 |
* |
| 3 |
* MLton is released under a BSD-style license. |
| 4 |
* See the file MLton-LICENSE for details. |
| 5 |
*) |
| 6 |
|
| 7 |
structure Real : REAL = |
| 8 |
struct |
| 9 |
open Real |
| 10 |
val minPos = 4.9406564584124654E~324; |
| 11 |
val minNormalPos = 2.2250738585072014E~308; |
| 12 |
val maxFinite = 1.7976931348623157E308; |
| 13 |
fun *+ (a, b, c) = a * b + c |
| 14 |
fun *- (a, b, c) = a * b - c |
| 15 |
fun ?= (x, y) = |
| 16 |
if isNan x then true |
| 17 |
else if isNan y then true |
| 18 |
else == (x, y) |
| 19 |
fun checkFloat x = |
| 20 |
if isNan x then raise Div |
| 21 |
else if not (isFinite x) then raise Overflow |
| 22 |
else x |
| 23 |
fun class x = |
| 24 |
if isNan x then IEEEReal.NAN |
| 25 |
else if not (isFinite x) then IEEEReal.INF |
| 26 |
else if == (x, 0.0) then IEEEReal.ZERO |
| 27 |
else if (abs x) < minPos then IEEEReal.SUBNORMAL |
| 28 |
else IEEEReal.NORMAL |
| 29 |
fun compareReal (x, y) = |
| 30 |
(case compare (x, y) of |
| 31 |
LESS => IEEEReal.LESS |
| 32 |
| EQUAL => IEEEReal.EQUAL |
| 33 |
| GREATER => IEEEReal.GREATER) |
| 34 |
handle IEEEReal.Unordered => IEEEReal.UNORDERED |
| 35 |
fun copySign (x: real, y: real) : real = raise Fail "Real.copySign" |
| 36 |
fun fromDecimal d = raise Fail "Real.fromDecimal" |
| 37 |
fun fromLarge (_ : IEEEReal.rounding_mode) (r : LargeReal.real) : real = r |
| 38 |
fun fromLargeInt i = valOf (fromString (LargeInt.toString i)) |
| 39 |
fun fromManExp {man: real, exp: int} : real = raise Fail "Real.fromManExp" |
| 40 |
fun isNormal x = (class x = IEEEReal.NORMAL) |
| 41 |
fun nextAfter (x: real, y: real) : real = raise Fail "Real.nextAfter" |
| 42 |
val precision = 53 |
| 43 |
val radix = 2 |
| 44 |
fun realCeil (r : real) : real = raise Fail "Real.realCeil" |
| 45 |
fun realFloor (_ : real) : real = raise Fail "Real.realFloor" |
| 46 |
fun realMod (_ : real) : real = raise Fail "Real.realMod" |
| 47 |
fun realRound (_ : real) : real = raise Fail "Real.realRound" |
| 48 |
fun realTrunc (_ : real) : real = raise Fail "Real.realTrunc" |
| 49 |
fun rem (_ : real, _ : real) : real = raise Fail "Real.rem" |
| 50 |
fun signBit r = sameSign (r, ~1.0) |
| 51 |
fun split (_ : real) : {whole : real, frac : real} = raise Fail "Real.split" |
| 52 |
fun toDecimal (_ : real) : IEEEReal.decimal_approx = raise Fail "Real.toDecimal" |
| 53 |
fun toInt rm r = |
| 54 |
case rm of |
| 55 |
IEEEReal.TO_NEGINF => floor r |
| 56 |
| IEEEReal.TO_POSINF => ceil r |
| 57 |
| IEEEReal.TO_ZERO => trunc r |
| 58 |
| IEEEReal.TO_NEAREST => round r |
| 59 |
fun toLarge (r: real) : LargeReal.real = r |
| 60 |
fun toLargeInt rm r = raise Fail "Real.toLargeInt" |
| 61 |
fun toManExp (_ : real) : {man: real, exp: int} = raise Fail "Real.toManExp" |
| 62 |
fun unordered (x, y) = isNan x orelse isNan y |
| 63 |
end |