Skip to content

Commit 0f496ca

Browse files
committed
Fix issue where generic compare on float values would be different from the compare for type float
Fixes #6040
1 parent e4303f1 commit 0f496ca

File tree

6 files changed

+56
-16
lines changed

6 files changed

+56
-16
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
- In GenType, check annotations also in module types to decide whether to produce the `.gen.tsx` file https://github.com/rescript-lang/rescript-compiler/pull/5903
4747
- Fix issue with JSX V4 and newtype https://github.com/rescript-lang/syntax/pull/737
4848
- Fix issue with JSX V4 when components are nested https://github.com/rescript-lang/syntax/pull/738
49+
- Fix issue where generic compare on `float` values would be different from the compare for type `float` https://github.com/rescript-lang/rescript-compiler/pull/6043
4950

5051
# 10.1.2
5152

jscomp/runtime/caml_obj.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ let rec compare (a : Obj.t) (b : Obj.t) : int =
172172
| "function", _ -> 1
173173
| _, "function" -> -1
174174
| "number", "number" ->
175-
Pervasives.compare (Obj.magic a : int) (Obj.magic b : int)
175+
Pervasives.compare (Obj.magic a : float) (Obj.magic b : float)
176176
| "number", _ ->
177177
if b == Obj.repr Js.null || Caml_option.isNested b then 1 (* Some (Some ..) < x *)
178178
else

jscomp/test/float_test.js

+38-10
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
var Mt = require("./mt.js");
44
var Caml = require("../../lib/js/caml.js");
55
var $$Array = require("../../lib/js/array.js");
6+
var Caml_obj = require("../../lib/js/caml_obj.js");
67
var Mt_global = require("./mt_global.js");
78
var Caml_float = require("../../lib/js/caml_float.js");
89
var Caml_int64 = require("../../lib/js/caml_int64.js");
@@ -138,24 +139,32 @@ function from_pairs(ps) {
138139

139140
var float_compare = Caml.float_compare;
140141

141-
Mt_global.collect_eq(test_id, suites, "File \"float_test.ml\", line 47, characters 5-12", Pervasives.classify_float(3), /* FP_normal */0);
142+
var generic_compare = Caml_obj.compare;
142143

143-
Mt_global.collect_eq(test_id, suites, "File \"float_test.ml\", line 48, characters 5-12", Caml_float.modf_float(-3.125), [
144+
function float_equal(x, y) {
145+
return x === y;
146+
}
147+
148+
var generic_equal = Caml_obj.equal;
149+
150+
Mt_global.collect_eq(test_id, suites, "File \"float_test.ml\", line 50, characters 5-12", Pervasives.classify_float(3), /* FP_normal */0);
151+
152+
Mt_global.collect_eq(test_id, suites, "File \"float_test.ml\", line 51, characters 5-12", Caml_float.modf_float(-3.125), [
144153
-0.125,
145154
-3
146155
]);
147156

148157
var match$3 = Caml_float.modf_float(Number.NaN);
149158

150-
Mt_global.collect_eq(test_id, suites, "File \"float_test.ml\", line 49, characters 5-12", [
159+
Mt_global.collect_eq(test_id, suites, "File \"float_test.ml\", line 52, characters 5-12", [
151160
Number.isNaN(match$3[0]),
152161
Number.isNaN(match$3[1])
153162
], [
154163
true,
155164
true
156165
]);
157166

158-
Mt_global.collect_eq(test_id, suites, "File \"float_test.ml\", line 52, characters 5-12", $$Array.map((function (x) {
167+
Mt_global.collect_eq(test_id, suites, "File \"float_test.ml\", line 55, characters 5-12", $$Array.map((function (x) {
159168
if (x > 0) {
160169
return 1;
161170
} else if (x < 0) {
@@ -184,17 +193,33 @@ Mt_global.collect_eq(test_id, suites, "File \"float_test.ml\", line 52, characte
184193
1
185194
]);
186195

187-
Mt_global.collect_eq(test_id, suites, "File \"float_test.ml\", line 56, characters 5-12", Caml_float.copysign_float(-3, 0), 3);
196+
Mt_global.collect_eq(test_id, suites, "File \"float_test.ml\", line 59, characters 5-12", Caml_float.copysign_float(-3, 0), 3);
197+
198+
Mt_global.collect_eq(test_id, suites, "File \"float_test.ml\", line 60, characters 5-12", Caml_float.copysign_float(3, 0), 3);
199+
200+
Mt_global.collect_eq(test_id, suites, "File \"float_test.ml\", line 61, characters 5-12", Math.log10(10), 1);
201+
202+
Mt_global.collect_eq(test_id, suites, "File \"float_test.ml\", line 62, characters 5-12", Caml_float.expm1_float(0), 0);
203+
204+
Mt_global.collect_eq(test_id, suites, "File \"float_test.ml\", line 63, characters 5-12", Number("3.0"), 3.0);
205+
206+
Mt_global.collect_approx(test_id, suites, "File \"float_test.ml\", line 64, characters 9-16", Caml_float.expm1_float(2), 6.38905609893065);
207+
208+
Mt_global.collect_eq(test_id, suites, "File \"float_test.ml\", line 65, characters 5-12", Caml.float_compare(NaN, NaN), 0);
209+
210+
Mt_global.collect_eq(test_id, suites, "File \"float_test.ml\", line 66, characters 5-12", Caml_obj.compare(NaN, NaN), 0);
211+
212+
Mt_global.collect_eq(test_id, suites, "File \"float_test.ml\", line 67, characters 5-12", Caml.float_compare(NaN, Pervasives.neg_infinity), -1);
188213

189-
Mt_global.collect_eq(test_id, suites, "File \"float_test.ml\", line 57, characters 5-12", Caml_float.copysign_float(3, 0), 3);
214+
Mt_global.collect_eq(test_id, suites, "File \"float_test.ml\", line 68, characters 5-12", Caml_obj.compare(NaN, Pervasives.neg_infinity), -1);
190215

191-
Mt_global.collect_eq(test_id, suites, "File \"float_test.ml\", line 58, characters 5-12", Math.log10(10), 1);
216+
Mt_global.collect_eq(test_id, suites, "File \"float_test.ml\", line 69, characters 5-12", Caml.float_compare(Pervasives.neg_infinity, NaN), 1);
192217

193-
Mt_global.collect_eq(test_id, suites, "File \"float_test.ml\", line 59, characters 5-12", Caml_float.expm1_float(0), 0);
218+
Mt_global.collect_eq(test_id, suites, "File \"float_test.ml\", line 70, characters 5-12", Caml_obj.compare(Pervasives.neg_infinity, NaN), 1);
194219

195-
Mt_global.collect_eq(test_id, suites, "File \"float_test.ml\", line 60, characters 5-12", Number("3.0"), 3.0);
220+
Mt_global.collect_eq(test_id, suites, "File \"float_test.ml\", line 71, characters 5-12", NaN === NaN, false);
196221

197-
Mt_global.collect_approx(test_id, suites, "File \"float_test.ml\", line 61, characters 9-16", Caml_float.expm1_float(2), 6.38905609893065);
222+
Mt_global.collect_eq(test_id, suites, "File \"float_test.ml\", line 72, characters 5-12", Caml_obj.equal(NaN, NaN), false);
198223

199224
var match$4 = Caml_float.modf_float(32.3);
200225

@@ -260,4 +285,7 @@ exports.epsilon_float = epsilon_float;
260285
exports.results = results;
261286
exports.from_pairs = from_pairs;
262287
exports.float_compare = float_compare;
288+
exports.generic_compare = generic_compare;
289+
exports.float_equal = float_equal;
290+
exports.generic_equal = generic_equal;
263291
/* results Not a pure module */

jscomp/test/float_test.ml

+14-3
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,11 @@ let from_pairs ps =
4141
|> Array.to_list
4242

4343
;;
44-
let float_compare (x : float) y = Pervasives.compare x y
45-
44+
let float_compare (x : float) y = Pervasives.compare x y
45+
let generic_compare = Pervasives.compare
46+
let float_equal (x : float) y = x = y
47+
let generic_equal = (=)
48+
4649
let () =
4750
eq __LOC__ (classify_float 3. ) FP_normal;
4851
eq __LOC__ (modf (-3.125)) (-0.125, -3.);
@@ -58,7 +61,15 @@ let () =
5861
eq __LOC__ (log10 10.) 1.;
5962
eq __LOC__ (expm1 0.) 0. ;
6063
eq __LOC__ (Js.Float.fromString "3.0") 3.0;
61-
approx __LOC__ (expm1 2.) 6.38905609893065
64+
approx __LOC__ (expm1 2.) 6.38905609893065;
65+
eq __LOC__ (float_compare Js.Float._NaN Js.Float._NaN) 0;
66+
eq __LOC__ (generic_compare Js.Float._NaN Js.Float._NaN) 0;
67+
eq __LOC__ (float_compare Js.Float._NaN neg_infinity) (-1);
68+
eq __LOC__ (generic_compare Js.Float._NaN neg_infinity) (-1);
69+
eq __LOC__ (float_compare neg_infinity Js.Float._NaN) 1;
70+
eq __LOC__ (generic_compare neg_infinity Js.Float._NaN) 1;
71+
eq __LOC__ (float_equal Js.Float._NaN Js.Float._NaN) false;
72+
eq __LOC__ (generic_equal Js.Float._NaN Js.Float._NaN) false;
6273
;;
6374

6475

lib/es6/caml_obj.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ function compare(a, b) {
5959
break;
6060
case "number" :
6161
if (b_type === "number") {
62-
return Caml.int_compare(a, b);
62+
return Caml.float_compare(a, b);
6363
}
6464
break;
6565
case "string" :

lib/js/caml_obj.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ function compare(a, b) {
5959
break;
6060
case "number" :
6161
if (b_type === "number") {
62-
return Caml.int_compare(a, b);
62+
return Caml.float_compare(a, b);
6363
}
6464
break;
6565
case "string" :

0 commit comments

Comments
 (0)