Import Upstream version 20180207
[hcoop/debian/mlton.git] / lib / mlton / basic / real.sml
CommitLineData
7f918cf1
CE
1(* Copyright (C) 1999-2006, 2008 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 Real (Real: sig
9 include PERVASIVE_REAL
10 val one: real
11 val zero: real
12 end): REAL =
13struct
14
15type real = Real.real
16
17structure In = In0
18
19structure R =
20 OrderedRing (structure R =
21 RingWithIdentity (structure R =
22 Ring (type t = real
23 open Real
24 val layout = Layout.str o toString
25 val equals = Real.==)
26 open R Real)
27 open R Real
28 val {compare, ...} =
29 Relation.lessEqual {< = op <, equals = equals})
30
31structure F = OrderedField (open R Real
32 fun inverse x = one / x)
33open F Real
34open Math
35
36exception Input
37fun input i =
38 case fromString (In.inputToSpace i) of
39 SOME x => x
40 | NONE => raise Input
41
42local
43 open Real
44in
45 val fromInt = fromInt
46 val fromIntInf = fromLargeInt
47 val toIntInf = toLargeInt IEEEReal.TO_NEAREST
48end
49
50structure Format =
51 struct
52 open StringCvt
53 type t = realfmt
54
55 val exact = EXACT
56 val sci = SCI
57 val fix = FIX
58 val gen = GEN
59 end
60
61fun format (x, f) = Real.fmt f x
62
63fun choose(n, k) =
64 let
65 val k = max (k, n - k)
66 in
67 prodFromTo {from = add1 k,
68 term = fn i => i,
69 to = n}
70 / factorial (n - k)
71 end
72
73fun log (base, arg) = ln arg / ln base
74
75val ln2 = ln two
76
77fun log2 x = ln x / ln2
78
79fun realPower(m, n) = exp(n * ln m)
80
81val ceiling = ceil
82
83structure Class =
84 struct
85 datatype t = datatype IEEEReal.float_class
86 end
87
88end
89
90structure Real64 = Real (open Real64
91 val one: real = 1.0
92 val zero: real = 0.0)
93structure Real = Real64
94structure Real32 = Real (open Real32
95 val one: real = 1.0
96 val zero: real = 0.0)