| 1 |
(* Copyright (C) 2011-2014,2017,2019-2020 Matthew Fluet. |
| 2 |
* Copyright (C) 2003-2007 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 |
functor Real (structure W: WORD_EXTRA |
| 10 |
structure R: |
| 11 |
sig |
| 12 |
include PRE_REAL |
| 13 |
val castToWord: real -> W.word |
| 14 |
val castFromWord: W.word -> real |
| 15 |
end): REAL_EXTRA = |
| 16 |
struct |
| 17 |
structure MLton = Primitive.MLton |
| 18 |
structure Prim = R |
| 19 |
local |
| 20 |
open IEEEReal |
| 21 |
in |
| 22 |
datatype float_class = datatype float_class |
| 23 |
datatype rounding_mode = datatype rounding_mode |
| 24 |
end |
| 25 |
infix 4 == != ?= |
| 26 |
type real = R.real |
| 27 |
|
| 28 |
local |
| 29 |
open Prim |
| 30 |
in |
| 31 |
val realSize = Int32.toInt realSize |
| 32 |
val exponentBias = Int32.toInt exponentBias |
| 33 |
val precision = Int32.toInt precision |
| 34 |
val radix = Int32.toInt radix |
| 35 |
end |
| 36 |
|
| 37 |
val signBits = Word.one |
| 38 |
val exponentSignificandBits = Word.- (Word.fromInt realSize, signBits) |
| 39 |
val significandBits = Word.- (Word.fromInt precision, Word.one) |
| 40 |
val exponentBits = Word.- (exponentSignificandBits, significandBits) |
| 41 |
|
| 42 |
local |
| 43 |
val mkMask : Word.word -> W.word = |
| 44 |
fn b => W.notb (W.<< (W.notb W.zero, b)) |
| 45 |
in |
| 46 |
val signMask = |
| 47 |
W.<< (mkMask signBits, exponentSignificandBits) |
| 48 |
val exponentMask = |
| 49 |
W.<< (mkMask exponentBits, significandBits) |
| 50 |
val significandMask = |
| 51 |
mkMask significandBits |
| 52 |
end |
| 53 |
|
| 54 |
val class : real -> float_class = |
| 55 |
fn r => |
| 56 |
let |
| 57 |
val w = R.castToWord r |
| 58 |
in |
| 59 |
if W.andb (w, exponentMask) = exponentMask |
| 60 |
then if W.andb (w, significandMask) = W.zero |
| 61 |
then IEEEReal.INF |
| 62 |
else IEEEReal.NAN |
| 63 |
else if W.andb (w, exponentMask) = W.zero |
| 64 |
then if W.andb (w, significandMask) = W.zero |
| 65 |
then IEEEReal.ZERO |
| 66 |
else IEEEReal.SUBNORMAL |
| 67 |
else IEEEReal.NORMAL |
| 68 |
end |
| 69 |
|
| 70 |
val toBits : real -> {sign: bool, exponent: W.word, significand: W.word} = |
| 71 |
fn r => |
| 72 |
let |
| 73 |
val w = R.castToWord r |
| 74 |
val significand = |
| 75 |
W.andb (w, significandMask) |
| 76 |
val exponent = |
| 77 |
W.>> (W.andb (w, exponentMask), significandBits) |
| 78 |
val sign = |
| 79 |
W.andb (w, signMask) = signMask |
| 80 |
in |
| 81 |
{sign = sign, |
| 82 |
exponent = exponent, |
| 83 |
significand = significand} |
| 84 |
end |
| 85 |
|
| 86 |
val fromBits : {sign: bool, exponent: W.word, significand: W.word} -> real = |
| 87 |
fn {sign, exponent, significand} => |
| 88 |
let |
| 89 |
val w = |
| 90 |
W.orb (if sign then W.<< (W.one, exponentSignificandBits) else W.zero, |
| 91 |
W.orb (W.andb (W.<< (exponent, significandBits), exponentMask), |
| 92 |
W.andb (significand, significandMask))) |
| 93 |
val r = R.castFromWord w |
| 94 |
in |
| 95 |
r |
| 96 |
end |
| 97 |
|
| 98 |
local |
| 99 |
open Prim |
| 100 |
in |
| 101 |
val op *+ = op *+ |
| 102 |
val op *- = op *- |
| 103 |
val op * = op * |
| 104 |
val op + = op + |
| 105 |
val op - = op - |
| 106 |
val op / = op / |
| 107 |
val op < = op < |
| 108 |
val op <= = op <= |
| 109 |
val op > = op > |
| 110 |
val op >= = op >= |
| 111 |
val ~ = ~ |
| 112 |
val abs = abs |
| 113 |
end |
| 114 |
|
| 115 |
local |
| 116 |
fun 'a make {fromRealUnsafe: 'a -> real, |
| 117 |
toRealUnsafe: real -> 'a, |
| 118 |
other : {precision: Primitive.Int32.int}} = |
| 119 |
if R.precision = #precision other |
| 120 |
then (fn (_: rounding_mode) => fromRealUnsafe, |
| 121 |
toRealUnsafe) |
| 122 |
else (fn (m: rounding_mode) => fn r => |
| 123 |
IEEEReal.withRoundingMode (m, fn () => fromRealUnsafe r), |
| 124 |
toRealUnsafe) |
| 125 |
in |
| 126 |
val (fromReal32,toReal32) = |
| 127 |
make {fromRealUnsafe = R.fromReal32Unsafe, |
| 128 |
toRealUnsafe = R.toReal32Unsafe, |
| 129 |
other = {precision = Primitive.Real32.precision}} |
| 130 |
val (fromReal64,toReal64) = |
| 131 |
make {fromRealUnsafe = R.fromReal64Unsafe, |
| 132 |
toRealUnsafe = R.toReal64Unsafe, |
| 133 |
other = {precision = Primitive.Real64.precision}} |
| 134 |
end |
| 135 |
local |
| 136 |
structure S = |
| 137 |
LargeReal_ChooseRealN |
| 138 |
(type 'a t = real -> 'a |
| 139 |
val fReal32 = toReal32 |
| 140 |
val fReal64 = toReal64) |
| 141 |
in |
| 142 |
val toLarge = S.f |
| 143 |
end |
| 144 |
local |
| 145 |
structure S = |
| 146 |
LargeReal_ChooseRealN |
| 147 |
(type 'a t = rounding_mode -> 'a -> real |
| 148 |
val fReal32 = fromReal32 |
| 149 |
val fReal64 = fromReal64) |
| 150 |
in |
| 151 |
val fromLarge = S.f |
| 152 |
end |
| 153 |
|
| 154 |
val negInf = R.castFromWord (W.orb (signMask, exponentMask)) |
| 155 |
val negOne = R.castFromWord (W.orb (signMask, W.<< (W.fromInt exponentBias, significandBits))) |
| 156 |
val negZero = R.castFromWord signMask |
| 157 |
val zero = R.castFromWord W.zero |
| 158 |
val minPos = R.castFromWord W.one |
| 159 |
val minNormalPos = R.castFromWord (W.<< (W.one, significandBits)) |
| 160 |
val half = R.castFromWord (W.<< (W.- (W.fromInt exponentBias, W.one), significandBits)) |
| 161 |
val one = R.castFromWord (W.<< (W.fromInt exponentBias, significandBits)) |
| 162 |
val two = R.castFromWord (W.<< (W.+ (W.fromInt exponentBias, W.one), significandBits)) |
| 163 |
val maxFinite = R.castFromWord (W.- (exponentMask, W.one)) |
| 164 |
val posInf = R.castFromWord exponentMask |
| 165 |
|
| 166 |
val nan = posInf + negInf |
| 167 |
val posNan = R.castFromWord (W.andb (R.castToWord nan, W.notb signMask)) |
| 168 |
val negNan = R.castFromWord (W.orb (R.castToWord nan, signMask)) |
| 169 |
|
| 170 |
|
| 171 |
fun isFinite r = |
| 172 |
abs r <= maxFinite |
| 173 |
|
| 174 |
val op == = Prim.== |
| 175 |
|
| 176 |
val op != = not o op == |
| 177 |
|
| 178 |
fun isNan r = r != r |
| 179 |
|
| 180 |
fun isNormal r = class r = NORMAL |
| 181 |
|
| 182 |
val op ?= = R.?= |
| 183 |
|
| 184 |
fun min (x, y) = |
| 185 |
if x <= y then x |
| 186 |
else if x > y then y |
| 187 |
else if isNan y then x |
| 188 |
else y |
| 189 |
|
| 190 |
fun max (x, y) = |
| 191 |
if x >= y then x |
| 192 |
else if x < y then y |
| 193 |
else if isNan y then x |
| 194 |
else y |
| 195 |
|
| 196 |
fun sign (x: real): int = |
| 197 |
if x > zero then 1 |
| 198 |
else if x < zero then ~1 |
| 199 |
else if x == zero then 0 |
| 200 |
else raise Domain |
| 201 |
|
| 202 |
val signBit = #sign o toBits |
| 203 |
|
| 204 |
fun sameSign (x, y) = signBit x = signBit y |
| 205 |
|
| 206 |
fun copySign (x, y) = |
| 207 |
if sameSign (x, y) |
| 208 |
then x |
| 209 |
else ~ x |
| 210 |
|
| 211 |
local |
| 212 |
structure I = IEEEReal |
| 213 |
in |
| 214 |
fun compareReal (x, y) = |
| 215 |
if x < y then I.LESS |
| 216 |
else if x > y then I.GREATER |
| 217 |
else if x == y then I.EQUAL |
| 218 |
else I.UNORDERED |
| 219 |
end |
| 220 |
|
| 221 |
local |
| 222 |
structure I = IEEEReal |
| 223 |
structure G = General |
| 224 |
in |
| 225 |
fun compare (x, y) = |
| 226 |
case compareReal (x, y) of |
| 227 |
I.EQUAL => G.EQUAL |
| 228 |
| I.GREATER => G.GREATER |
| 229 |
| I.LESS => G.LESS |
| 230 |
| I.UNORDERED => raise IEEEReal.Unordered |
| 231 |
end |
| 232 |
|
| 233 |
fun unordered (x, y) = isNan x orelse isNan y |
| 234 |
|
| 235 |
(* nextAfter for subnormal and normal values works by converting |
| 236 |
* the real to a word of equivalent size and doing an increment |
| 237 |
* or decrement on the word. Because of the way IEEE floating |
| 238 |
* point numbers are represented, word {de,in}crement |
| 239 |
* automatically does the right thing at the boundary between |
| 240 |
* normals and denormals. Also, convienently, |
| 241 |
* maxFinite+1 = posInf and minFinite-1 = negInf. |
| 242 |
*) |
| 243 |
val nextAfter: real * real -> real = |
| 244 |
fn (r, t) => |
| 245 |
case (class r, class t) of |
| 246 |
(NAN, _) => nan |
| 247 |
| (_, NAN) => nan |
| 248 |
| (INF, _) => r |
| 249 |
| (ZERO, ZERO) => t (* want "t", not "r", to get the sign right *) |
| 250 |
| (ZERO, _) => if t > zero then minPos else ~minPos |
| 251 |
| _ => |
| 252 |
if r == t then |
| 253 |
r |
| 254 |
else if (r > t) = (r > zero) then |
| 255 |
R.castFromWord (W.- (R.castToWord r, W.one)) |
| 256 |
else |
| 257 |
R.castFromWord (W.+ (R.castToWord r, W.one)) |
| 258 |
|
| 259 |
local |
| 260 |
val one = One.make (fn () => ref (0 : C_Int.t)) |
| 261 |
in |
| 262 |
fun toManExp x = |
| 263 |
case class x of |
| 264 |
INF => {exp = 0, man = x} |
| 265 |
| NAN => {exp = 0, man = nan} |
| 266 |
| ZERO => {exp = 0, man = x} |
| 267 |
| _ => One.use (one, fn r => |
| 268 |
let |
| 269 |
val man = R.frexp (x, r) |
| 270 |
in |
| 271 |
{exp = C_Int.toInt (!r), man = man} |
| 272 |
end) |
| 273 |
end |
| 274 |
|
| 275 |
fun fromManExp {exp, man} = |
| 276 |
(R.ldexp (man, C_Int.fromInt exp)) |
| 277 |
handle Overflow => |
| 278 |
man * (if Int.< (exp, 0) then zero else posInf) |
| 279 |
|
| 280 |
val fromManExp = |
| 281 |
if MLton.Codegen.isX86 |
| 282 |
then fromManExp |
| 283 |
else |
| 284 |
fn {exp, man} => |
| 285 |
case class man of |
| 286 |
INF => man |
| 287 |
| NAN => man |
| 288 |
| ZERO => man |
| 289 |
| _ => fromManExp {exp = exp, man = man} |
| 290 |
|
| 291 |
local |
| 292 |
val one = One.make (fn () => ref zero) |
| 293 |
in |
| 294 |
fun split x = |
| 295 |
case class x of |
| 296 |
INF => {frac = if x > zero then zero else ~zero, |
| 297 |
whole = x} |
| 298 |
| NAN => {frac = nan, whole = nan} |
| 299 |
| _ => |
| 300 |
let |
| 301 |
val (frac, whole) = |
| 302 |
One.use (one, fn int => |
| 303 |
(R.modf (x, int), !int)) |
| 304 |
(* Some platforms' C libraries don't get sign of |
| 305 |
* zero right. |
| 306 |
*) |
| 307 |
fun fix y = |
| 308 |
if class y = ZERO andalso not (sameSign (x, y)) |
| 309 |
then ~ y |
| 310 |
else y |
| 311 |
in |
| 312 |
{frac = fix frac, |
| 313 |
whole = fix whole} |
| 314 |
end |
| 315 |
end |
| 316 |
|
| 317 |
val realMod = #frac o split |
| 318 |
|
| 319 |
fun checkFloat x = |
| 320 |
if isFinite x then x |
| 321 |
else if isNan x then raise Div |
| 322 |
else raise Overflow |
| 323 |
|
| 324 |
val realCeil = R.realCeil |
| 325 |
val realFloor = R.realFloor |
| 326 |
val realTrunc = R.realTrunc |
| 327 |
|
| 328 |
(* Unfortunately, libc round ties to zero instead of even values. *) |
| 329 |
(* Fortunately, if any rounding mode is supported, it's TO_NEAREST. *) |
| 330 |
val realRound = fn r => IEEEReal.withRoundingMode (TO_NEAREST, fn () => R.round r) |
| 331 |
|
| 332 |
fun rem (x, y) = |
| 333 |
(case class x of |
| 334 |
INF => nan |
| 335 |
| NAN => nan |
| 336 |
| ZERO => zero |
| 337 |
| _ => (case class y of |
| 338 |
INF => x |
| 339 |
| NAN => nan |
| 340 |
| ZERO => nan |
| 341 |
| _ => x - realTrunc (x/y) * y)) |
| 342 |
|
| 343 |
(* fromDecimal, scan, fromString: decimal -> binary conversions *) |
| 344 |
fun strtor (str: NullString.t, |
| 345 |
rounding_mode: IEEEReal.rounding_mode) = |
| 346 |
let |
| 347 |
val rounding : C_Int.int = |
| 348 |
case rounding_mode of |
| 349 |
TO_NEAREST => 1 |
| 350 |
| TO_NEGINF => 3 |
| 351 |
| TO_POSINF => 2 |
| 352 |
| TO_ZERO => 0 |
| 353 |
in |
| 354 |
Prim.strtor (str, rounding) |
| 355 |
end |
| 356 |
exception Bad |
| 357 |
fun fromDecimalWithRoundingMode |
| 358 |
({class, digits, exp, sign}: IEEEReal.decimal_approx, |
| 359 |
rounding_mode: IEEEReal.rounding_mode) = |
| 360 |
let |
| 361 |
fun doit () = |
| 362 |
let |
| 363 |
val exp = |
| 364 |
if Int.< (exp, 0) |
| 365 |
then concat ["-", Int.toString (Int.~ exp)] |
| 366 |
else Int.toString exp |
| 367 |
(* |
| 368 |
val str = concat [if sign then "-" else "", |
| 369 |
"0.", digits, |
| 370 |
"E", exp, "\000"] |
| 371 |
*) |
| 372 |
val n = Int.+ (if sign then 1 else 0, |
| 373 |
Int.+ (4 (* "0." + "E" + "\000" *), |
| 374 |
Int.+ (List.length digits, |
| 375 |
String.size exp))) |
| 376 |
val a = Array.alloc n |
| 377 |
fun upd (i, c) = (Array.update (a, i, c); Int.+ (i, 1)) |
| 378 |
val i = 0 |
| 379 |
val i = if sign then upd (i, #"-") else i |
| 380 |
val i = upd (i, #"0") |
| 381 |
val i = upd (i, #".") |
| 382 |
val i = |
| 383 |
List.foldl |
| 384 |
(fn (d, i) => |
| 385 |
if Int.< (d, 0) orelse Int.> (d, 9) |
| 386 |
then raise Bad |
| 387 |
else upd (i, Char.chr (Int.+ (d, Char.ord #"0")))) |
| 388 |
i digits |
| 389 |
val i = upd (i, #"E") |
| 390 |
val i = CharVector.foldl (fn (c, i) => upd (i, c)) i exp |
| 391 |
val _ = upd (i, #"\000") |
| 392 |
val str = Vector.unsafeFromArray a |
| 393 |
val x = strtor (NullString.fromString str, rounding_mode) |
| 394 |
in |
| 395 |
x |
| 396 |
end |
| 397 |
in |
| 398 |
SOME (case class of |
| 399 |
INF => if sign then negInf else posInf |
| 400 |
| NAN => if sign then negNan else posNan |
| 401 |
| NORMAL => doit () |
| 402 |
| SUBNORMAL => doit () |
| 403 |
| ZERO => if sign then negZero else zero) |
| 404 |
handle Bad => NONE |
| 405 |
end |
| 406 |
|
| 407 |
fun fromDecimal da = fromDecimalWithRoundingMode (da, TO_NEAREST) |
| 408 |
|
| 409 |
fun scan reader state = |
| 410 |
case IEEEReal.scan reader state of |
| 411 |
NONE => NONE |
| 412 |
| SOME (da, state) => |
| 413 |
SOME (valOf (fromDecimalWithRoundingMode |
| 414 |
(da, IEEEReal.getRoundingMode ())), |
| 415 |
state) |
| 416 |
|
| 417 |
val fromString = StringCvt.scanString scan |
| 418 |
|
| 419 |
(* toDecimal, fmt, toString: binary -> decimal conversions. *) |
| 420 |
datatype mode = Fix | Gen | Sci |
| 421 |
local |
| 422 |
val decpt = ref (0: C_Int.int) |
| 423 |
in |
| 424 |
fun gdtoa (x: real, mode: mode, ndig: int, |
| 425 |
rounding_mode: IEEEReal.rounding_mode) = |
| 426 |
let |
| 427 |
val mode : C_Int.int = |
| 428 |
case mode of |
| 429 |
Fix => 3 |
| 430 |
| Gen => 0 |
| 431 |
| Sci => 2 |
| 432 |
val ndig : C_Int.int = C_Int.fromInt ndig |
| 433 |
val rounding : C_Int.int = |
| 434 |
case rounding_mode of |
| 435 |
TO_NEAREST => 1 |
| 436 |
| TO_NEGINF => 3 |
| 437 |
| TO_POSINF => 2 |
| 438 |
| TO_ZERO => 0 |
| 439 |
val _ = Primitive.MLton.Thread.atomicBegin () |
| 440 |
in |
| 441 |
DynamicWind.wind |
| 442 |
(fn () => |
| 443 |
(CUtil.C_String.toString |
| 444 |
(Prim.gdtoa (x, mode, ndig, rounding, decpt)), |
| 445 |
C_Int.toInt (!decpt)), |
| 446 |
Primitive.MLton.Thread.atomicEnd) |
| 447 |
end |
| 448 |
end |
| 449 |
|
| 450 |
fun toDecimal (x: real): IEEEReal.decimal_approx = |
| 451 |
case class x of |
| 452 |
INF => {class = INF, |
| 453 |
digits = [], |
| 454 |
exp = 0, |
| 455 |
sign = x < zero} |
| 456 |
| NAN => {class = NAN, |
| 457 |
digits = [], |
| 458 |
exp = 0, |
| 459 |
sign = signBit x} |
| 460 |
| ZERO => {class = ZERO, |
| 461 |
digits = [], |
| 462 |
exp = 0, |
| 463 |
sign = signBit x} |
| 464 |
| c => |
| 465 |
let |
| 466 |
val (s, exp) = gdtoa (x, Gen, 0, TO_NEAREST) |
| 467 |
fun loop (i, ac) = |
| 468 |
if Int.< (i, 0) |
| 469 |
then ac |
| 470 |
else loop (Int.- (i, 1), |
| 471 |
(Int.- (Char.ord (String.sub (s, i)), |
| 472 |
Char.ord #"0")) |
| 473 |
:: ac) |
| 474 |
val digits = loop (Int.- (String.size s, 1), []) |
| 475 |
in |
| 476 |
{class = c, |
| 477 |
digits = digits, |
| 478 |
exp = exp, |
| 479 |
sign = x < zero} |
| 480 |
end |
| 481 |
|
| 482 |
datatype realfmt = datatype StringCvt.realfmt |
| 483 |
|
| 484 |
local |
| 485 |
fun fix (sign: string, s: String.string, decpt: int, ndig: int): string = |
| 486 |
let |
| 487 |
val length = String.size s |
| 488 |
in |
| 489 |
if Int.< (decpt, 0) |
| 490 |
then |
| 491 |
concat [sign, |
| 492 |
"0.", |
| 493 |
String.new (Int.~ decpt, #"0"), |
| 494 |
s, |
| 495 |
String.new (Int.+ (Int.- (ndig, length), |
| 496 |
decpt), |
| 497 |
#"0")] |
| 498 |
else |
| 499 |
let |
| 500 |
val whole = |
| 501 |
if decpt = 0 |
| 502 |
then "0" |
| 503 |
else |
| 504 |
String.tabulate (decpt, fn i => |
| 505 |
if Int.< (i, length) |
| 506 |
then String.sub (s, i) |
| 507 |
else #"0") |
| 508 |
in |
| 509 |
if 0 = ndig |
| 510 |
then concat [sign, whole] |
| 511 |
else |
| 512 |
let |
| 513 |
val frac = |
| 514 |
String.tabulate |
| 515 |
(ndig, fn i => |
| 516 |
let |
| 517 |
val j = Int.+ (i, decpt) |
| 518 |
in |
| 519 |
if Int.< (j, length) |
| 520 |
then String.sub (s, j) |
| 521 |
else #"0" |
| 522 |
end) |
| 523 |
in |
| 524 |
concat [sign, whole, ".", frac] |
| 525 |
end |
| 526 |
end |
| 527 |
end |
| 528 |
fun sci (x: real, ndig: int): string = |
| 529 |
let |
| 530 |
val sign = if x < zero then "~" else "" |
| 531 |
val (s, decpt) = |
| 532 |
gdtoa (x, Sci, Int.+ (1, ndig), IEEEReal.getRoundingMode ()) |
| 533 |
val length = String.size s |
| 534 |
val whole = String.tabulate (1, fn _ => String.sub (s, 0)) |
| 535 |
val frac = |
| 536 |
if 0 = ndig |
| 537 |
then "" |
| 538 |
else concat [".", |
| 539 |
String.tabulate |
| 540 |
(ndig, fn i => |
| 541 |
let |
| 542 |
val j = Int.+ (i, 1) |
| 543 |
in |
| 544 |
if Int.< (j, length) |
| 545 |
then String.sub (s, j) |
| 546 |
else #"0" |
| 547 |
end)] |
| 548 |
val exp = Int.- (decpt, 1) |
| 549 |
val exp = |
| 550 |
let |
| 551 |
val (exp, sign) = |
| 552 |
if Int.< (exp, 0) |
| 553 |
then (Int.~ exp, "~") |
| 554 |
else (exp, "") |
| 555 |
in |
| 556 |
concat [sign, Int.toString exp] |
| 557 |
end |
| 558 |
in |
| 559 |
concat [sign, whole, frac, "E", exp] |
| 560 |
end |
| 561 |
fun gen (x: real, n: int): string = |
| 562 |
let |
| 563 |
val (prefix, x) = |
| 564 |
if x < zero |
| 565 |
then ("~", ~ x) |
| 566 |
else ("", x) |
| 567 |
val ss = Substring.full (sci (x, Int.- (n, 1))) |
| 568 |
fun isE c = c = #"E" |
| 569 |
fun isZero c = c = #"0" |
| 570 |
val expS = |
| 571 |
Substring.string (Substring.taker (not o isE) ss) |
| 572 |
val exp = valOf (Int.fromString expS) |
| 573 |
val man = |
| 574 |
String.translate |
| 575 |
(fn #"." => "" | c => str c) |
| 576 |
(Substring.string (Substring.dropr isZero |
| 577 |
(Substring.takel (not o isE) ss))) |
| 578 |
val manSize = String.size man |
| 579 |
fun zeros i = CharVector.tabulate (i, fn _ => #"0") |
| 580 |
fun dotAt i = |
| 581 |
concat [String.substring (man, 0, i), |
| 582 |
".", String.extract (man, i, NONE)] |
| 583 |
fun sci () = concat [prefix, |
| 584 |
if manSize = 1 then man else dotAt 1, |
| 585 |
"E", expS] |
| 586 |
val op - = Int.- |
| 587 |
val op + = Int.+ |
| 588 |
val ~ = Int.~ |
| 589 |
val op >= = Int.>= |
| 590 |
in |
| 591 |
if exp >= (if manSize = 1 then 3 else manSize + 3) |
| 592 |
then sci () |
| 593 |
else if exp >= manSize - 1 |
| 594 |
then concat [prefix, man, zeros (exp - (manSize - 1))] |
| 595 |
else if exp >= 0 |
| 596 |
then concat [prefix, dotAt (exp + 1)] |
| 597 |
else if exp >= (if manSize = 1 then ~2 else ~3) |
| 598 |
then concat [prefix, "0.", zeros (~exp - 1), man] |
| 599 |
else sci () |
| 600 |
end |
| 601 |
in |
| 602 |
fun fmt spec = |
| 603 |
let |
| 604 |
val doit = |
| 605 |
case spec of |
| 606 |
EXACT => IEEEReal.toString o toDecimal |
| 607 |
| FIX opt => |
| 608 |
let |
| 609 |
val n = |
| 610 |
case opt of |
| 611 |
NONE => 6 |
| 612 |
| SOME n => |
| 613 |
if Primitive.Controls.safe andalso Int.< (n, 0) |
| 614 |
then raise Size |
| 615 |
else n |
| 616 |
in |
| 617 |
fn x => |
| 618 |
let |
| 619 |
val sign = if x < zero then "~" else "" |
| 620 |
val (s, decpt) = |
| 621 |
gdtoa (x, Fix, n, IEEEReal.getRoundingMode ()) |
| 622 |
in |
| 623 |
fix (sign, s, decpt, n) |
| 624 |
end |
| 625 |
end |
| 626 |
| GEN opt => |
| 627 |
let |
| 628 |
val n = |
| 629 |
case opt of |
| 630 |
NONE => 12 |
| 631 |
| SOME n => |
| 632 |
if Primitive.Controls.safe andalso Int.< (n, 1) |
| 633 |
then raise Size |
| 634 |
else n |
| 635 |
in |
| 636 |
fn x => gen (x, n) |
| 637 |
end |
| 638 |
| SCI opt => |
| 639 |
let |
| 640 |
val n = |
| 641 |
case opt of |
| 642 |
NONE => 6 |
| 643 |
| SOME n => |
| 644 |
if Primitive.Controls.safe andalso Int.< (n, 0) |
| 645 |
then raise Size |
| 646 |
else n |
| 647 |
in |
| 648 |
fn x => sci (x, n) |
| 649 |
end |
| 650 |
in |
| 651 |
fn x => |
| 652 |
case class x of |
| 653 |
NAN => (* if signBit x then "~nan" else *) "nan" |
| 654 |
| INF => if x > zero then "inf" else "~inf" |
| 655 |
| _ => doit x |
| 656 |
end |
| 657 |
end |
| 658 |
|
| 659 |
val toString = fmt (StringCvt.GEN NONE) |
| 660 |
|
| 661 |
(* Not all devices support all rounding modes. |
| 662 |
* However, every device has ceil/floor/round/trunc. |
| 663 |
*) |
| 664 |
fun safeConvert (m, cvt, x) = |
| 665 |
case m of |
| 666 |
TO_POSINF => cvt (realCeil x) |
| 667 |
| TO_NEGINF => cvt (realFloor x) |
| 668 |
| TO_NEAREST => cvt (realRound x) |
| 669 |
| TO_ZERO => cvt (realTrunc x) |
| 670 |
|
| 671 |
local |
| 672 |
fun 'a make {fromIntUnsafe: 'a -> real, |
| 673 |
toIntUnsafe: real -> 'a, |
| 674 |
other : {maxInt': Word.word -> 'a, |
| 675 |
minInt': 'a, |
| 676 |
precision': int}} = |
| 677 |
(fromIntUnsafe, |
| 678 |
if Int.< (precision, #precision' other) then |
| 679 |
let |
| 680 |
val trim = Int.- (Int.- (#precision' other, precision), 1) |
| 681 |
val maxInt' = (#maxInt' other) (Word.fromInt trim) |
| 682 |
val minInt' = #minInt' other |
| 683 |
val maxInt = fromIntUnsafe maxInt' |
| 684 |
val minInt = fromIntUnsafe minInt' |
| 685 |
in |
| 686 |
fn (m: rounding_mode) => fn x => |
| 687 |
if minInt <= x then |
| 688 |
if x <= maxInt then |
| 689 |
safeConvert (m, toIntUnsafe, x) |
| 690 |
else |
| 691 |
raise Overflow |
| 692 |
else |
| 693 |
if x < minInt then |
| 694 |
raise Overflow |
| 695 |
else |
| 696 |
raise Domain (* NaN *) |
| 697 |
end |
| 698 |
else |
| 699 |
let |
| 700 |
val maxInt' = (#maxInt' other) 0w0 |
| 701 |
val minInt' = #minInt' other |
| 702 |
val maxInt = fromIntUnsafe maxInt' |
| 703 |
val minInt = fromIntUnsafe minInt' |
| 704 |
in |
| 705 |
fn (m: rounding_mode) => fn x => |
| 706 |
if minInt <= x then |
| 707 |
if x <= maxInt then |
| 708 |
safeConvert (m, toIntUnsafe, x) |
| 709 |
else |
| 710 |
if x < maxInt + one then |
| 711 |
(case m of |
| 712 |
TO_NEGINF => maxInt' |
| 713 |
| TO_POSINF => raise Overflow |
| 714 |
| TO_ZERO => maxInt' |
| 715 |
| TO_NEAREST => |
| 716 |
(* Depends on maxInt being odd. *) |
| 717 |
if x - maxInt >= half then |
| 718 |
raise Overflow |
| 719 |
else |
| 720 |
maxInt') |
| 721 |
else |
| 722 |
raise Overflow |
| 723 |
else |
| 724 |
if x < minInt then |
| 725 |
if minInt - one < x then |
| 726 |
(case m of |
| 727 |
TO_NEGINF => raise Overflow |
| 728 |
| TO_POSINF => minInt' |
| 729 |
| TO_ZERO => minInt' |
| 730 |
| TO_NEAREST => |
| 731 |
(* Depends on minInt being even. *) |
| 732 |
if x - minInt < ~half then |
| 733 |
raise Overflow |
| 734 |
else |
| 735 |
minInt') |
| 736 |
else |
| 737 |
raise Overflow |
| 738 |
else |
| 739 |
raise Domain (* NaN *) |
| 740 |
end) |
| 741 |
in |
| 742 |
val (fromInt8,toInt8) = |
| 743 |
make {fromIntUnsafe = R.fromInt8Unsafe, |
| 744 |
toIntUnsafe = R.toInt8Unsafe, |
| 745 |
other = {maxInt' = fn w => Int8.<< (Int8.>> (Int8.maxInt', w), w), |
| 746 |
minInt' = Int8.minInt', |
| 747 |
precision' = Int8.precision'}} |
| 748 |
val (fromInt16,toInt16) = |
| 749 |
make {fromIntUnsafe = R.fromInt16Unsafe, |
| 750 |
toIntUnsafe = R.toInt16Unsafe, |
| 751 |
other = {maxInt' = fn w => Int16.<< (Int16.>> (Int16.maxInt', w), w), |
| 752 |
minInt' = Int16.minInt', |
| 753 |
precision' = Int16.precision'}} |
| 754 |
val (fromInt32,toInt32) = |
| 755 |
make {fromIntUnsafe = R.fromInt32Unsafe, |
| 756 |
toIntUnsafe = R.toInt32Unsafe, |
| 757 |
other = {maxInt' = fn w => Int32.<< (Int32.>> (Int32.maxInt', w), w), |
| 758 |
minInt' = Int32.minInt', |
| 759 |
precision' = Int32.precision'}} |
| 760 |
val (fromInt64,toInt64) = |
| 761 |
make {fromIntUnsafe = R.fromInt64Unsafe, |
| 762 |
toIntUnsafe = R.toInt64Unsafe, |
| 763 |
other = {maxInt' = fn w => Int64.<< (Int64.>> (Int64.maxInt', w), w), |
| 764 |
minInt' = Int64.minInt', |
| 765 |
precision' = Int64.precision'}} |
| 766 |
end |
| 767 |
|
| 768 |
val fromIntInf: IntInf.int -> real = |
| 769 |
fn i => |
| 770 |
let |
| 771 |
val str = |
| 772 |
if IntInf.< (i, 0) |
| 773 |
then "-" ^ (IntInf.toString (IntInf.~ i)) |
| 774 |
else IntInf.toString i |
| 775 |
val x = strtor (NullString.nullTerm str, |
| 776 |
IEEEReal.getRoundingMode ()) |
| 777 |
in |
| 778 |
x |
| 779 |
end |
| 780 |
|
| 781 |
val toIntInf: rounding_mode -> real -> LargeInt.int = |
| 782 |
fn mode => fn x => |
| 783 |
case class x of |
| 784 |
INF => raise Overflow |
| 785 |
| NAN => raise Domain |
| 786 |
| ZERO => (0 : LargeInt.int) |
| 787 |
| _ => |
| 788 |
let |
| 789 |
(* This round may turn x into an INF, so we need to check the |
| 790 |
* class again. |
| 791 |
*) |
| 792 |
val x = |
| 793 |
case mode of |
| 794 |
TO_POSINF => realCeil x |
| 795 |
| TO_NEGINF => realFloor x |
| 796 |
| TO_NEAREST => realRound x |
| 797 |
| TO_ZERO => realTrunc x |
| 798 |
in |
| 799 |
case class x of |
| 800 |
INF => raise Overflow |
| 801 |
| _ => valOf (IntInf.fromString (fmt (StringCvt.FIX (SOME 0)) x)) |
| 802 |
end |
| 803 |
|
| 804 |
local |
| 805 |
structure S = |
| 806 |
Int_ChooseInt |
| 807 |
(type 'a t = 'a -> real |
| 808 |
val fInt8 = fromInt8 |
| 809 |
val fInt16 = fromInt16 |
| 810 |
val fInt32 = fromInt32 |
| 811 |
val fInt64 = fromInt64 |
| 812 |
val fIntInf = fromIntInf) |
| 813 |
in |
| 814 |
val fromInt = S.f |
| 815 |
end |
| 816 |
local |
| 817 |
structure S = |
| 818 |
LargeInt_ChooseInt |
| 819 |
(type 'a t = 'a -> real |
| 820 |
val fInt8 = fromInt8 |
| 821 |
val fInt16 = fromInt16 |
| 822 |
val fInt32 = fromInt32 |
| 823 |
val fInt64 = fromInt64 |
| 824 |
val fIntInf = fromIntInf) |
| 825 |
in |
| 826 |
val fromLargeInt = S.f |
| 827 |
end |
| 828 |
local |
| 829 |
structure S = |
| 830 |
Int_ChooseInt |
| 831 |
(type 'a t = rounding_mode -> real -> 'a |
| 832 |
val fInt8 = toInt8 |
| 833 |
val fInt16 = toInt16 |
| 834 |
val fInt32 = toInt32 |
| 835 |
val fInt64 = toInt64 |
| 836 |
val fIntInf = toIntInf) |
| 837 |
in |
| 838 |
val toInt = S.f |
| 839 |
end |
| 840 |
local |
| 841 |
structure S = |
| 842 |
LargeInt_ChooseInt |
| 843 |
(type 'a t = rounding_mode -> real -> 'a |
| 844 |
val fInt8 = toInt8 |
| 845 |
val fInt16 = toInt16 |
| 846 |
val fInt32 = toInt32 |
| 847 |
val fInt64 = toInt64 |
| 848 |
val fIntInf = toIntInf) |
| 849 |
in |
| 850 |
val toLargeInt = S.f |
| 851 |
end |
| 852 |
|
| 853 |
val floor = toInt TO_NEGINF |
| 854 |
val ceil = toInt TO_POSINF |
| 855 |
val trunc = toInt TO_ZERO |
| 856 |
val round = toInt TO_NEAREST |
| 857 |
|
| 858 |
local |
| 859 |
fun 'a make {fromWordUnsafe: 'a -> real, |
| 860 |
toWordUnsafe: real -> 'a, |
| 861 |
other : {maxWord': Word.word -> 'a, |
| 862 |
wordSize: int, |
| 863 |
zeroWord: 'a}} = |
| 864 |
(fromWordUnsafe, |
| 865 |
if Int.<= (precision, #wordSize other) |
| 866 |
then let |
| 867 |
val trim = Int.- (#wordSize other, precision) |
| 868 |
val maxWord' = (#maxWord' other) (Word.fromInt trim) |
| 869 |
val maxWord = fromWordUnsafe maxWord' |
| 870 |
val zeroWord = #zeroWord other |
| 871 |
in |
| 872 |
fn (m: rounding_mode) => fn x => |
| 873 |
case class x of |
| 874 |
INF => raise Overflow |
| 875 |
| NAN => raise Domain |
| 876 |
| _ => if zero <= x |
| 877 |
then if x <= maxWord |
| 878 |
then safeConvert (m, toWordUnsafe, x) |
| 879 |
else raise Overflow |
| 880 |
else if x > ~one |
| 881 |
then (case m of |
| 882 |
TO_NEGINF => raise Overflow |
| 883 |
| TO_POSINF => zeroWord |
| 884 |
| TO_ZERO => zeroWord |
| 885 |
| TO_NEAREST => |
| 886 |
if x < ~half |
| 887 |
then raise Overflow |
| 888 |
else zeroWord) |
| 889 |
else raise Overflow |
| 890 |
end |
| 891 |
else let |
| 892 |
val maxWord' = (#maxWord' other) 0w0 |
| 893 |
val maxWord = fromWordUnsafe maxWord' |
| 894 |
val zeroWord = #zeroWord other |
| 895 |
in |
| 896 |
fn (m: rounding_mode) => fn x => |
| 897 |
case class x of |
| 898 |
INF => raise Overflow |
| 899 |
| NAN => raise Domain |
| 900 |
| _ => if zero <= x |
| 901 |
then if x <= maxWord |
| 902 |
then safeConvert (m, toWordUnsafe, x) |
| 903 |
else if x < maxWord + one |
| 904 |
then (case m of |
| 905 |
TO_NEGINF => maxWord' |
| 906 |
| TO_POSINF => raise Overflow |
| 907 |
| TO_ZERO => maxWord' |
| 908 |
| TO_NEAREST => |
| 909 |
(* Depends on maxWord being odd. *) |
| 910 |
if x - maxWord >= half |
| 911 |
then raise Overflow |
| 912 |
else maxWord') |
| 913 |
else raise Overflow |
| 914 |
else if x > ~one |
| 915 |
then (case m of |
| 916 |
TO_NEGINF => raise Overflow |
| 917 |
| TO_POSINF => zeroWord |
| 918 |
| TO_ZERO => zeroWord |
| 919 |
| TO_NEAREST => |
| 920 |
if x < ~half |
| 921 |
then raise Overflow |
| 922 |
else zeroWord) |
| 923 |
else raise Overflow |
| 924 |
end) |
| 925 |
in |
| 926 |
val (fromWord8,toWord8) = |
| 927 |
make {fromWordUnsafe = R.fromWord8Unsafe, |
| 928 |
toWordUnsafe = R.toWord8Unsafe, |
| 929 |
other = {maxWord' = fn w => Word8.<< (Word8.>> (Word8.maxWord', w), w), |
| 930 |
wordSize = Word8.wordSize, |
| 931 |
zeroWord = Word8.zero}} |
| 932 |
val (fromWord16,toWord16) = |
| 933 |
make {fromWordUnsafe = R.fromWord16Unsafe, |
| 934 |
toWordUnsafe = R.toWord16Unsafe, |
| 935 |
other = {maxWord' = fn w => Word16.<< (Word16.>> (Word16.maxWord', w), w), |
| 936 |
wordSize = Word16.wordSize, |
| 937 |
zeroWord = Word16.zero}} |
| 938 |
val (fromWord32,toWord32) = |
| 939 |
make {fromWordUnsafe = R.fromWord32Unsafe, |
| 940 |
toWordUnsafe = R.toWord32Unsafe, |
| 941 |
other = {maxWord' = fn w => Word32.<< (Word32.>> (Word32.maxWord', w), w), |
| 942 |
wordSize = Word32.wordSize, |
| 943 |
zeroWord = Word32.zero}} |
| 944 |
val (fromWord64,toWord64) = |
| 945 |
make {fromWordUnsafe = R.fromWord64Unsafe, |
| 946 |
toWordUnsafe = R.toWord64Unsafe, |
| 947 |
other = {maxWord' = fn w => Word64.<< (Word64.>> (Word64.maxWord', w), w), |
| 948 |
wordSize = Word64.wordSize, |
| 949 |
zeroWord = Word64.zero}} |
| 950 |
end |
| 951 |
|
| 952 |
local |
| 953 |
structure S = |
| 954 |
Word_ChooseWordN |
| 955 |
(type 'a t = 'a -> real |
| 956 |
val fWord8 = fromWord8 |
| 957 |
val fWord16 = fromWord16 |
| 958 |
val fWord32 = fromWord32 |
| 959 |
val fWord64 = fromWord64) |
| 960 |
in |
| 961 |
val fromWord = S.f |
| 962 |
end |
| 963 |
local |
| 964 |
structure S = |
| 965 |
LargeWord_ChooseWordN |
| 966 |
(type 'a t = 'a -> real |
| 967 |
val fWord8 = fromWord8 |
| 968 |
val fWord16 = fromWord16 |
| 969 |
val fWord32 = fromWord32 |
| 970 |
val fWord64 = fromWord64) |
| 971 |
in |
| 972 |
val fromLargeWord = S.f |
| 973 |
end |
| 974 |
local |
| 975 |
structure S = |
| 976 |
Word_ChooseWordN |
| 977 |
(type 'a t = rounding_mode -> real -> 'a |
| 978 |
val fWord8 = toWord8 |
| 979 |
val fWord16 = toWord16 |
| 980 |
val fWord32 = toWord32 |
| 981 |
val fWord64 = toWord64) |
| 982 |
in |
| 983 |
val toWord = S.f |
| 984 |
end |
| 985 |
local |
| 986 |
structure S = |
| 987 |
LargeWord_ChooseWordN |
| 988 |
(type 'a t = rounding_mode -> real -> 'a |
| 989 |
val fWord8 = toWord8 |
| 990 |
val fWord16 = toWord16 |
| 991 |
val fWord32 = toWord32 |
| 992 |
val fWord64 = toWord64) |
| 993 |
in |
| 994 |
val toLargeWord = S.f |
| 995 |
end |
| 996 |
|
| 997 |
structure Math = |
| 998 |
struct |
| 999 |
open Prim.Math |
| 1000 |
|
| 1001 |
(* Patch functions to handle out-of-range args. Many C math |
| 1002 |
* libraries do not do what the SML Basis Spec requires. |
| 1003 |
*) |
| 1004 |
|
| 1005 |
local |
| 1006 |
fun patch f x = |
| 1007 |
if x < ~one orelse x > one |
| 1008 |
then nan |
| 1009 |
else f x |
| 1010 |
in |
| 1011 |
val acos = patch acos |
| 1012 |
val asin = patch asin |
| 1013 |
end |
| 1014 |
|
| 1015 |
local |
| 1016 |
fun patch f x = if x < zero then nan else f x |
| 1017 |
in |
| 1018 |
val ln = patch ln |
| 1019 |
val log10 = patch log10 |
| 1020 |
end |
| 1021 |
|
| 1022 |
(* The x86 doesn't get exp right on infs. *) |
| 1023 |
val exp = |
| 1024 |
if MLton.Codegen.isX86 |
| 1025 |
andalso let open MLton.Platform.Arch in host = X86 end |
| 1026 |
then (fn x => |
| 1027 |
case class x of |
| 1028 |
INF => if x > zero then posInf else zero |
| 1029 |
| _ => exp x) |
| 1030 |
else exp |
| 1031 |
|
| 1032 |
(* The Cygwin math library doesn't get pow right on some exceptional |
| 1033 |
* cases. |
| 1034 |
* |
| 1035 |
* The Linux math library doesn't get pow (x, y) right when x < 0 |
| 1036 |
* and y is large (but finite). |
| 1037 |
* |
| 1038 |
* So, we define a pow function that gives the correct result on |
| 1039 |
* exceptional cases, and only calls the C pow with x > 0. |
| 1040 |
*) |
| 1041 |
fun isInt (x: real): bool = x == realFloor x |
| 1042 |
|
| 1043 |
(* isEven x assumes isInt x. *) |
| 1044 |
fun isEven (x: real): bool = isInt (x / two) |
| 1045 |
|
| 1046 |
fun isOddInt x = isInt x andalso not (isEven x) |
| 1047 |
|
| 1048 |
fun isNeg x = x < zero |
| 1049 |
|
| 1050 |
fun pow (x, y) = |
| 1051 |
case class y of |
| 1052 |
INF => |
| 1053 |
if class x = NAN |
| 1054 |
then nan |
| 1055 |
else if x < negOne orelse x > one |
| 1056 |
then if isNeg y then zero else posInf |
| 1057 |
else if negOne < x andalso x < one |
| 1058 |
then if isNeg y then posInf else zero |
| 1059 |
else (* x = 1 orelse x = ~1 *) |
| 1060 |
nan |
| 1061 |
| NAN => nan |
| 1062 |
| ZERO => one |
| 1063 |
| _ => |
| 1064 |
(case class x of |
| 1065 |
INF => |
| 1066 |
if isNeg x |
| 1067 |
then if isNeg y |
| 1068 |
then if isOddInt y |
| 1069 |
then negZero |
| 1070 |
else zero |
| 1071 |
else if isOddInt y |
| 1072 |
then negInf |
| 1073 |
else posInf |
| 1074 |
else (* x = posInf *) |
| 1075 |
if isNeg y then zero else posInf |
| 1076 |
| NAN => nan |
| 1077 |
| ZERO => |
| 1078 |
if isNeg y |
| 1079 |
then if isOddInt y |
| 1080 |
then copySign (posInf, x) |
| 1081 |
else posInf |
| 1082 |
else if isOddInt y |
| 1083 |
then x |
| 1084 |
else zero |
| 1085 |
| _ => |
| 1086 |
if isNeg x |
| 1087 |
then if isInt y |
| 1088 |
then if isEven y |
| 1089 |
then Prim.Math.pow (~ x, y) |
| 1090 |
else negOne * Prim.Math.pow (~ x, y) |
| 1091 |
else nan |
| 1092 |
else Prim.Math.pow (x, y)) |
| 1093 |
|
| 1094 |
fun cosh x = |
| 1095 |
case class x of |
| 1096 |
INF => x |
| 1097 |
| ZERO => one |
| 1098 |
| _ => R.Math.cosh x |
| 1099 |
|
| 1100 |
fun sinh x = |
| 1101 |
case class x of |
| 1102 |
INF => x |
| 1103 |
| ZERO => x |
| 1104 |
| _ => R.Math.sinh x |
| 1105 |
|
| 1106 |
fun tanh x = |
| 1107 |
case class x of |
| 1108 |
INF => if x > zero then one else negOne |
| 1109 |
| ZERO => x |
| 1110 |
| _ => R.Math.tanh x |
| 1111 |
end |
| 1112 |
end |
| 1113 |
|
| 1114 |
structure Real32 = Real (structure W = Word32 |
| 1115 |
structure R = |
| 1116 |
struct |
| 1117 |
open Primitive.Real32 |
| 1118 |
local open Primitive.PackReal32 in |
| 1119 |
val castToWord = castToWord |
| 1120 |
val castFromWord = castFromWord |
| 1121 |
end |
| 1122 |
end) |
| 1123 |
structure Real64 = Real (structure W = Word64 |
| 1124 |
structure R = |
| 1125 |
struct |
| 1126 |
open Primitive.Real64 |
| 1127 |
local open Primitive.PackReal64 in |
| 1128 |
val castToWord = castToWord |
| 1129 |
val castFromWord = castFromWord |
| 1130 |
end |
| 1131 |
end) |