Import Upstream version 20180207
[hcoop/debian/mlton.git] / basis-library / libs / basis-1997 / real / IEEE-real-convert.fun
CommitLineData
7f918cf1
CE
1(* Copyright (C) 2002-2006 Henry Cejtin, Matthew Fluet, Suresh
2 * Jagannathan, and Stephen Weeks.
3 *
4 * MLton is released under a BSD-style license.
5 * See the file MLton-LICENSE for details.
6 *)
7
8functor IEEERealConvert
9 (structure IEEEReal: IEEE_REAL):
10 sig
11 include IEEE_REAL_1997
12 val >> : IEEEReal.float_class -> float_class
13 val << : float_class -> IEEEReal.float_class
14 val >>> : IEEEReal.decimal_approx -> decimal_approx
15 val <<< : decimal_approx -> IEEEReal.decimal_approx
16 end =
17 struct
18 open IEEEReal
19
20 datatype nan_mode = QUIET | SIGNALLING
21 datatype float_class =
22 NAN of nan_mode
23 | INF
24 | ZERO
25 | NORMAL
26 | SUBNORMAL
27 val >> =
28 fn IEEEReal.NAN => NAN QUIET
29 | IEEEReal.INF => INF
30 | IEEEReal.ZERO => ZERO
31 | IEEEReal.NORMAL => NORMAL
32 | IEEEReal.SUBNORMAL => SUBNORMAL
33 val << =
34 fn NAN _ => IEEEReal.NAN
35 | INF => IEEEReal.INF
36 | ZERO => IEEEReal.ZERO
37 | NORMAL => IEEEReal.NORMAL
38 | SUBNORMAL => IEEEReal.SUBNORMAL
39
40 type decimal_approx = {kind: float_class, sign: bool,
41 digits: int list, exp: int}
42
43 val <<< = fn {kind, sign, digits, exp} =>
44 {class = << kind, sign = sign,
45 digits = digits, exp = exp}
46 val >>> = fn {class, sign, digits, exp} =>
47 {kind = >> class, sign = sign,
48 digits = digits, exp = exp}
49
50 val toString = toString o <<<
51 val fromString = fn s =>
52 Option.map (>>>) (fromString s)
53 end
54
55structure IEEEReal1997 = IEEERealConvert(structure IEEEReal = IEEEReal)