forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFloat.res
59 lines (48 loc) · 2.11 KB
/
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
56
57
58
59
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"
}
external equal: (float, float) => bool = "%equal"
external compare: (float, float) => Ordering.t = "%compare"
@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, ~radix: int=?) => float = "parseInt"
@deprecated("Use `parseInt` instead") @val
external parseIntWithRadix: ('a, ~radix: int) => float = "parseInt"
@send external toExponential: (float, ~digits: int=?) => string = "toExponential"
@deprecated("Use `toExponential` instead") @send
external toExponentialWithPrecision: (float, ~digits: int) => string = "toExponential"
@send external toFixed: (float, ~digits: int=?) => string = "toFixed"
@deprecated("Use `toFixed` instead") @send
external toFixedWithPrecision: (float, ~digits: int) => string = "toFixed"
@send external toPrecision: (float, ~digits: int=?) => string = "toPrecision"
@deprecated("Use `toPrecision` instead") @send
external toPrecisionWithPrecision: (float, ~digits: int) => string = "toPrecision"
@send external toString: (float, ~radix: int=?) => string = "toString"
@deprecated("Use `toString` instead") @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"
external mod: (float, float) => float = "%modfloat"
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
}
}