package tablecloth-native

  1. Overview
  2. Docs

Source file TableclothFloat.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
type t = float

type radians = t

let fromInt = Base.Float.of_int

let from_int = fromInt

let fromString string =
  try Some (Base.Float.of_string string) with Invalid_argument _ -> None


let from_string = fromString

let zero = 0.0

let one = 1.0

let nan = Base.Float.nan

let infinity = Base.Float.infinity

let negativeInfinity = Base.Float.neg_infinity

let negative_infinity = neg_infinity

let e = Base.Float.euler

let pi = Base.Float.pi

let epsilon = Base.Float.epsilon_float

let maximumSafeInteger = (2. ** 52.) -. 1.

let maximum_safe_integer = maximumSafeInteger

let minimumSafeInteger = (-2. ** 52.) -. 1.

let minimum_safe_integer = minimumSafeInteger

let largestValue = Base.Float.max_finite_value

let largest_value = largestValue

let smallestValue = Base.Float.min_positive_normal_value

let smallest_value = smallestValue

let add = ( +. )

let ( + ) = ( +. )

let subtract = ( -. )

let ( - ) = ( -. )

let multiply = ( *. )

let ( * ) = ( *. )

let divide n ~by = n /. by

let ( / ) = ( /. )

let power ~base ~exponent = base ** exponent

let ( ** ) = ( ** )

let negate = Base.Float.neg

let ( ~- ) = negate

let absolute = Base.Float.abs

let isInteger t = t = Base.Float.round t

let is_integer = isInteger

let isSafeInteger t = isInteger t && t <= maximumSafeInteger

let is_safe_integer = isSafeInteger

let clamp n ~lower ~upper =
  if upper < lower
  then
    raise
      (Invalid_argument
         ( "~lower:"
         ^ Base.Float.to_string lower
         ^ " must be less than or equal to ~upper:"
         ^ Base.Float.to_string upper ) )
  else if Base.Float.is_nan lower
          || Base.Float.is_nan upper
          || Base.Float.is_nan n
  then Base.Float.nan
  else max lower (min upper n)


let inRange n ~lower ~upper =
  if let open Base.Float in
     upper < lower
  then
    raise
      (Invalid_argument
         ( "~lower:"
         ^ Base.Float.to_string lower
         ^ " must be less than or equal to ~upper:"
         ^ Base.Float.to_string upper ) )
  else n >= lower && n < upper


let in_range = inRange

let squareRoot = sqrt

let square_root = squareRoot

let log n ~base =
  let open Base.Float in
  log10 n / log10 base


let isNaN = Base.Float.is_nan

let is_nan = isNaN

let isInfinite = Base.Float.is_inf

let is_infinite = isInfinite

let isFinite n = (not (isInfinite n)) && not (isNaN n)

let is_finite = isFinite

let maximum x y = if isNaN x || isNaN y then nan else if y > x then y else x

let minimum x y = if isNaN x || isNaN y then nan else if y < x then y else x

let hypotenuse x y = squareRoot ((x * x) + (y * y))

let degrees n = n * (pi / 180.0)

external radians : float -> float = "%identity"

let turns n = n * 2. * pi

let cos = Base.Float.cos

let acos = Base.Float.acos

let sin = Base.Float.sin

let asin = Base.Float.asin

let tan = Base.Float.tan

let atan = Base.Float.atan

let atan2 ~y ~x = Base.Float.atan2 y x

type direction =
  [ `Zero
  | `AwayFromZero
  | `Up
  | `Down
  | `Closest of [ `Zero | `AwayFromZero | `Up | `Down | `ToEven ]
  ]

let round ?(direction = `Closest `Up) n =
  match direction with
  | (`Up | `Down | `Zero) as dir ->
      Base.Float.round n ~dir
  | `AwayFromZero ->
      if n < 0.
      then Base.Float.round n ~dir:`Down
      else Base.Float.round n ~dir:`Up
  | `Closest `Zero ->
      if n > 0.
      then Base.Float.round (n -. 0.5) ~dir:`Up
      else Base.Float.round (n +. 0.5) ~dir:`Down
  | `Closest `AwayFromZero ->
      if n > 0.
      then Base.Float.round (n +. 0.5) ~dir:`Down
      else Base.Float.round (n -. 0.5) ~dir:`Up
  | `Closest `Down ->
      Base.Float.round (n -. 0.5) ~dir:`Up
  | `Closest `Up ->
      Base.Float.round_nearest n
  | `Closest `ToEven ->
      Base.Float.round_nearest_half_to_even n


let floor = Base.Float.round_down

let ceiling = Base.Float.round_up

let truncate = Base.Float.round_towards_zero

let fromPolar (r, theta) = (r * cos theta, r * sin theta)

let from_polar = fromPolar

let toPolar (x, y) = (hypotenuse x y, atan2 ~x ~y)

let to_polar = toPolar

let toInt = Base.Float.iround_towards_zero

let to_int = toInt

let toString = Base.Float.to_string

let to_string = toString

let equal = ( = )

let compare = compare