forked from rescript-lang/rescript-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCore__Float.res
55 lines (44 loc) · 1.91 KB
/
Core__Float.res
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
module Constants = {
@val external nan: float = "NaN"
@val external epsilon: float = "Number.EPSILON"
@val external positiveInfinity: float = "Number.POSITIVE_INFINITY"
@val external negativeInfinity: float = "Number.NEGATIVE_INFINITY"
@val external minValue: float = "Number.MIN_VALUE"
@val external maxValue: float = "Number.MAX_VALUE"
}
let equal = (a: float, b: float) => a === b
let compare = (a: float, b: float) =>
a < b ? Core__Ordering.less : a > b ? Core__Ordering.greater : Core__Ordering.equal
@val external isNaN: float => bool = "isNaN"
@val external isFinite: float => bool = "isFinite"
@val external parseFloat: 'a => float = "parseFloat"
// parseInt's return type is a float because it can be NaN
@val external parseInt: 'a => float = "parseInt"
@val external parseIntWithRadix: ('a, ~radix: int) => float = "parseInt"
@send external toExponential: float => string = "toExponential"
@send external toExponentialWithPrecision: (float, ~digits: int) => string = "toExponential"
@send external toFixed: float => string = "toFixed"
@send external toFixedWithPrecision: (float, ~digits: int) => string = "toFixed"
@send external toPrecision: float => string = "toPrecision"
@send external toPrecisionWithPrecision: (float, ~digits: int) => string = "toPrecision"
@send external toString: float => string = "toString"
@send external toStringWithRadix: (float, ~radix: int) => string = "toString"
@send external toLocaleString: float => string = "toLocaleString"
let fromString = i =>
switch parseFloat(i) {
| i if isNaN(i) => None
| i => Some(i)
}
external toInt: float => int = "%intoffloat"
external fromInt: int => float = "%identity"
@unboxed @noalloc external mod: (float, float) => float = "?fmod_float"
let clamp = (~min=?, ~max=?, value): float => {
let value = switch max {
| Some(max) if max < value => max
| _ => value
}
switch min {
| Some(min) if min > value => min
| _ => value
}
}