| 1 |
(* Copyright (C) 2009,2022 Matthew Fluet. |
| 2 |
* Copyright (C) 2005 Henry Cejtin, Matthew Fluet, Suresh |
| 3 |
* Jagannathan, and Stephen Weeks. |
| 4 |
* |
| 5 |
* MLton is released under a HPND-style license. |
| 6 |
* See the file MLton-LICENSE for details. |
| 7 |
*) |
| 8 |
|
| 9 |
signature IEEE_REAL_EXTRA = |
| 10 |
sig |
| 11 |
include IEEE_REAL |
| 12 |
|
| 13 |
val toGoodFC: Pervasive.IEEEReal.float_class -> float_class |
| 14 |
val toBadFC: float_class -> Pervasive.IEEEReal.float_class |
| 15 |
val toGoodDA: Pervasive.IEEEReal.decimal_approx -> decimal_approx |
| 16 |
val toBadDA: decimal_approx -> Pervasive.IEEEReal.decimal_approx |
| 17 |
end |
| 18 |
|
| 19 |
(* SML/NJ uses an old version of datatype IEEEReal.float_class. *) |
| 20 |
structure IEEERealExtra : IEEE_REAL_EXTRA = |
| 21 |
struct |
| 22 |
open IEEEReal |
| 23 |
|
| 24 |
datatype float_class = NAN | INF | ZERO | NORMAL | SUBNORMAL |
| 25 |
type decimal_approx = {class:float_class, digits:int list, exp:int, sign:bool} |
| 26 |
|
| 27 |
local |
| 28 |
structure P = Pervasive.IEEEReal |
| 29 |
in |
| 30 |
fun toGoodFC c = |
| 31 |
case c of |
| 32 |
P.NAN _ => NAN |
| 33 |
| P.INF => INF |
| 34 |
| P.ZERO => ZERO |
| 35 |
| P.NORMAL => NORMAL |
| 36 |
| P.SUBNORMAL => SUBNORMAL |
| 37 |
fun toBadFC c = |
| 38 |
case c of |
| 39 |
NAN => P.NAN P.QUIET |
| 40 |
| INF => P.INF |
| 41 |
| ZERO => P.ZERO |
| 42 |
| NORMAL => P.NORMAL |
| 43 |
| SUBNORMAL => P.SUBNORMAL |
| 44 |
fun toGoodDA {digits, exp, kind, sign} = |
| 45 |
{class = toGoodFC kind, digits = digits, exp = exp, sign = sign} |
| 46 |
fun toBadDA {class, digits, exp, sign} = |
| 47 |
{digits = digits, exp = exp, kind = toBadFC class, sign = sign} |
| 48 |
val toString = P.toString o toBadDA |
| 49 |
val fromString = (Option.map toGoodDA) o P.fromString |
| 50 |
fun scan r s = Option.map (fn (da,x) => (toGoodDA da,x)) (P.scan r s) |
| 51 |
end |
| 52 |
|
| 53 |
end |
| 54 |
|
| 55 |
structure IEEEReal : IEEE_REAL = IEEERealExtra |