mirror of
https://github.com/espruino/Espruino.git
synced 2025-12-08 19:06:15 +00:00
Fixed type equality check between int and float (should treat them as the same)
This commit is contained in:
parent
26b9675d71
commit
16f46e7df2
@ -28,6 +28,8 @@
|
||||
Increase available stack on Espruino Board
|
||||
Stop FatFS using insane amounts of RAM
|
||||
High res timer (now to 1/2^23) using SysTick with RTC as a base (fix #168)
|
||||
Added 'Infinity' constant
|
||||
Fixed type equality check between int and float (should treat them as the same)
|
||||
|
||||
1v50 : Fix broken Web IDE caused by change to printing JSON for console.log (part of #206)
|
||||
Fix bug when trying to stringify {5:5}
|
||||
|
||||
@ -1853,8 +1853,12 @@ JsVar *jsvMathsOp(JsVar *a, JsVar *b, int op) {
|
||||
if (op == LEX_TYPEEQUAL || op == LEX_NTYPEEQUAL) {
|
||||
// check type first, then call again to check data
|
||||
bool eql = (a==0) == (b==0);
|
||||
if (a && b) eql = ((a->flags & JSV_VARTYPEMASK) ==
|
||||
(b->flags & JSV_VARTYPEMASK));
|
||||
if (a && b) {
|
||||
// Check whether both are numbers, otherwise check the variable
|
||||
// type flags themselves
|
||||
eql = ((jsvIsInt(a)||jsvIsFloat(a)) == (jsvIsInt(b)||jsvIsFloat(b))) ||
|
||||
((a->flags & JSV_VARTYPEMASK) == (b->flags & JSV_VARTYPEMASK));
|
||||
}
|
||||
if (eql) {
|
||||
JsVar *contents = jsvMathsOp(a,b, LEX_EQUAL);
|
||||
if (!jsvGetBool(contents)) eql = false;
|
||||
|
||||
14
tests/test_number_equality.js
Normal file
14
tests/test_number_equality.js
Normal file
@ -0,0 +1,14 @@
|
||||
result = true;
|
||||
function test(a,b) {
|
||||
if (eval(a)!=b) {
|
||||
result=false;
|
||||
console.log("FAIL: "+a+" should be "+b);
|
||||
}
|
||||
}
|
||||
|
||||
test("3 == 3.0",true);
|
||||
test("3 === 3.0",true);
|
||||
test("Math.abs(-5) === 5",true);
|
||||
// test("Math.log(Math.E*Math.E) === 2", true); // not fair to check this one due to FP inaccuracy
|
||||
test("Math.log(1) === 0",true);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user