| 1 |
(* Copyright (C) 2009,2019,2022-2023 Matthew Fluet. |
| 2 |
* Copyright (C) 1999-2006 Henry Cejtin, Matthew Fluet, Suresh |
| 3 |
* Jagannathan, and Stephen Weeks. |
| 4 |
* Copyright (C) 1997-2000 NEC Research Institute. |
| 5 |
* |
| 6 |
* MLton is released under a HPND-style license. |
| 7 |
* See the file MLton-LICENSE for details. |
| 8 |
*) |
| 9 |
|
| 10 |
functor FixReal(PReal: sig include PERVASIVE_REAL val zero : real end) : REAL = |
| 11 |
struct |
| 12 |
open PReal |
| 13 |
|
| 14 |
(* SML/NJ uses an old version of datatype IEEEReal.float_class. *) |
| 15 |
(* Fixed in SML/NJ 110.99.3. *) |
| 16 |
local |
| 17 |
val toGoodFC = IEEERealExtra.toGoodFC |
| 18 |
val toGoodDA = IEEERealExtra.toGoodDA |
| 19 |
val toBadDA = IEEERealExtra.toBadDA |
| 20 |
in |
| 21 |
val class = toGoodFC o class |
| 22 |
val fromDecimal = SOME o fromDecimal o toBadDA |
| 23 |
val toDecimal = toGoodDA o toDecimal |
| 24 |
end |
| 25 |
|
| 26 |
(* SML/NJ doesn't support EXACT. *) |
| 27 |
fun fmt f = |
| 28 |
PReal.fmt |
| 29 |
(let |
| 30 |
datatype z = datatype StringCvt.realfmt |
| 31 |
in |
| 32 |
case f of |
| 33 |
EXACT => StringCvt.GEN NONE |
| 34 |
| FIX io => StringCvt.FIX io |
| 35 |
| GEN io => StringCvt.GEN io |
| 36 |
| SCI io => StringCvt.SCI io |
| 37 |
end) |
| 38 |
|
| 39 |
val fromString = PReal.fromString |
| 40 |
(* SML/NJ raises Overflow on large exponents. *) |
| 41 |
(* Fixed in SML/NJ 110.83. *) |
| 42 |
val fromString = fn s => |
| 43 |
(case SOME (fromString s) handle Overflow => NONE of |
| 44 |
NONE => |
| 45 |
let |
| 46 |
val manexp = |
| 47 |
String.tokens |
| 48 |
(fn c => c = #"e" orelse c = #"E") |
| 49 |
s |
| 50 |
fun isNeg s = |
| 51 |
String.sub (s, 0) = #"~" |
| 52 |
orelse String.sub (s, 0) = #"+" |
| 53 |
fun isNonzero s = |
| 54 |
CharVector.exists |
| 55 |
(fn c => Char.<= (#"1", c) andalso Char.<= (c, #"9")) |
| 56 |
s |
| 57 |
in |
| 58 |
case manexp of |
| 59 |
[man,exp] => |
| 60 |
if isNeg exp |
| 61 |
then SOME zero |
| 62 |
else if isNonzero man |
| 63 |
then SOME posInf |
| 64 |
else SOME zero |
| 65 |
| _ => NONE |
| 66 |
end |
| 67 |
| SOME ro => ro) |
| 68 |
(* SML/NJ doesn't handle "[+~-]?(inf|infinity|nan)". *) |
| 69 |
(* Fixed in SML/NJ 110.99.3. *) |
| 70 |
val fromString = fn s => |
| 71 |
case s of |
| 72 |
"inf" => SOME posInf |
| 73 |
| "infinity" => SOME posInf |
| 74 |
| "+inf" => SOME posInf |
| 75 |
| "+infinity" => SOME posInf |
| 76 |
| "~inf" => SOME negInf |
| 77 |
| "~infinity" => SOME negInf |
| 78 |
| "-inf" => SOME negInf |
| 79 |
| "-infinity" => SOME negInf |
| 80 |
| "nan" => SOME (negInf + posInf) |
| 81 |
| "+nan" => SOME (negInf + posInf) |
| 82 |
| "~nan" => SOME (negInf + posInf) |
| 83 |
| "-nan" => SOME (negInf + posInf) |
| 84 |
| _ => fromString s |
| 85 |
end |
| 86 |
|
| 87 |
structure Real = FixReal(struct open Pervasive.Real val zero : real = 0.0 end) |