Skip to content

Commit 22db0ee

Browse files
committed
fix(float/clamp): work around compiler bug by type annotation
1 parent 714779a commit 22db0ee

File tree

4 files changed

+20
-37
lines changed

4 files changed

+20
-37
lines changed

src/Core__Float.mjs

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
// Generated by ReScript, PLEASE EDIT WITH CARE
22

3-
import * as Caml_obj from "rescript/lib/es6/caml_obj.js";
4-
import * as Caml_option from "rescript/lib/es6/caml_option.js";
53

64
var Constants = {};
75

@@ -15,19 +13,9 @@ function fromString(i) {
1513
}
1614

1715
function clamp(min, max, value) {
18-
var value$1;
19-
if (max !== undefined) {
20-
var max$1 = Caml_option.valFromOption(max);
21-
value$1 = Caml_obj.lessthan(max$1, value) ? max$1 : value;
22-
} else {
23-
value$1 = value;
24-
}
25-
if (min === undefined) {
26-
return value$1;
27-
}
28-
var min$1 = Caml_option.valFromOption(min);
29-
if (Caml_obj.greaterthan(min$1, value$1)) {
30-
return min$1;
16+
var value$1 = max !== undefined && max < value ? max : value;
17+
if (min !== undefined && min > value$1) {
18+
return min;
3119
} else {
3220
return value$1;
3321
}

src/Core__Float.res

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ external fromInt: int => float = "%identity"
3838

3939
@unboxed @noalloc external mod: (float, float) => float = "?fmod_float"
4040

41-
let clamp = (~min=?, ~max=?, value) => {
41+
let clamp = (~min=?, ~max=?, value): float => {
4242
let value = switch max {
4343
| Some(max) if max < value => max
4444
| _ => value

test/FloatTests.mjs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ Test.run([
105105
33
106106
],
107107
"clamp - nan"
108-
], Core__Float.clamp(4.1, 4.3, Number.NaN), eq, 4.1);
108+
], isNaN(Core__Float.clamp(4.1, 4.3, Number.NaN)), eq, true);
109109

110110
Test.run([
111111
[
@@ -135,7 +135,7 @@ Test.run([
135135
37
136136
],
137137
"clamp - min nan"
138-
], isNaN(Core__Float.clamp(Number.NaN, undefined, 4.2)), eq, true);
138+
], Core__Float.clamp(Number.NaN, undefined, 4.2), eq, 4.2);
139139

140140
Test.run([
141141
[
@@ -150,17 +150,17 @@ Test.run([
150150
Test.run([
151151
[
152152
"FloatTests.res",
153+
19,
153154
20,
154-
13,
155-
39
155+
46
156156
],
157157
"clamp - min nan, max nan"
158-
], isNaN(Core__Float.clamp(Number.NaN, Number.NaN, 4.2)), eq, true);
158+
], Core__Float.clamp(Number.NaN, Number.NaN, 4.2), eq, 4.2);
159159

160160
Test.run([
161161
[
162162
"FloatTests.res",
163-
25,
163+
20,
164164
20,
165165
42
166166
],
@@ -170,7 +170,7 @@ Test.run([
170170
Test.run([
171171
[
172172
"FloatTests.res",
173-
26,
173+
21,
174174
20,
175175
42
176176
],
@@ -180,7 +180,7 @@ Test.run([
180180
Test.run([
181181
[
182182
"FloatTests.res",
183-
27,
183+
22,
184184
20,
185185
43
186186
],
@@ -190,7 +190,7 @@ Test.run([
190190
Test.run([
191191
[
192192
"FloatTests.res",
193-
28,
193+
23,
194194
20,
195195
43
196196
],
@@ -200,7 +200,7 @@ Test.run([
200200
Test.run([
201201
[
202202
"FloatTests.res",
203-
30,
203+
25,
204204
13,
205205
49
206206
],
@@ -210,7 +210,7 @@ Test.run([
210210
Test.run([
211211
[
212212
"FloatTests.res",
213-
36,
213+
31,
214214
13,
215215
50
216216
],
@@ -220,7 +220,7 @@ Test.run([
220220
Test.run([
221221
[
222222
"FloatTests.res",
223-
42,
223+
37,
224224
13,
225225
50
226226
],
@@ -230,7 +230,7 @@ Test.run([
230230
Test.run([
231231
[
232232
"FloatTests.res",
233-
48,
233+
43,
234234
13,
235235
51
236236
],

test/FloatTests.res

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,12 @@ Test.run(__POS_OF__("clamp - < min, < max"), Float.clamp(~min=4.3, ~max=4.5, 4.2
1111
Test.run(__POS_OF__("clamp - < min, > max"), Float.clamp(~min=4.3, ~max=4.1, 4.2), eq, 4.3) // min wins
1212
Test.run(__POS_OF__("clamp - > min, < max"), Float.clamp(~min=4.1, ~max=4.5, 4.2), eq, 4.2)
1313
Test.run(__POS_OF__("clamp - > min, > max"), Float.clamp(~min=4.1, ~max=4.1, 4.2), eq, 4.1)
14-
Test.run(__POS_OF__("clamp - nan"), Float.clamp(~min=4.1, ~max=4.3, nan), eq, 4.1) // min wins ??
14+
Test.run(__POS_OF__("clamp - nan"), Float.clamp(~min=4.1, ~max=4.3, nan)->Float.isNaN, eq, true)
1515
Test.run(__POS_OF__("clamp - infinity"), Float.clamp(~min=4.1, ~max=4.3, infinity), eq, 4.3)
1616
Test.run(__POS_OF__("clamp - -infinity"), Float.clamp(~min=4.1, ~max=4.3, neg_infinity), eq, 4.1)
17-
Test.run(__POS_OF__("clamp - min nan"), Float.clamp(~min=nan, 4.2)->Float.isNaN, eq, true) // ??
17+
Test.run(__POS_OF__("clamp - min nan"), Float.clamp(~min=nan, 4.2), eq, 4.2)
1818
Test.run(__POS_OF__("clamp - max nan"), Float.clamp(~max=nan, 4.2), eq, 4.2)
19-
Test.run(
20-
__POS_OF__("clamp - min nan, max nan"),
21-
Float.clamp(~min=nan, ~max=nan, 4.2)->Float.isNaN,
22-
eq,
23-
true, // ??
24-
)
19+
Test.run(__POS_OF__("clamp - min nan, max nan"), Float.clamp(~min=nan, ~max=nan, 4.2), eq, 4.2)
2520
Test.run(__POS_OF__("clamp - min infinity"), Float.clamp(~min=infinity, 4.2), eq, infinity)
2621
Test.run(__POS_OF__("clamp - max infinity"), Float.clamp(~max=infinity, 4.2), eq, 4.2)
2722
Test.run(__POS_OF__("clamp - min -infinity"), Float.clamp(~min=neg_infinity, 4.2), eq, 4.2)

0 commit comments

Comments
 (0)