Fixed type equality check between int and float (should treat them as the same)

This commit is contained in:
Gordon Williams 2014-02-14 08:40:14 +00:00
parent 26b9675d71
commit 16f46e7df2
3 changed files with 22 additions and 2 deletions

View File

@ -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}

View File

@ -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;

View 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);