Explorar o código

Fix for (-0.0 == 0.0) comparison

mingodad %!s(int64=6) %!d(string=hai) anos
pai
achega
c8de7df3a3
Modificáronse 2 ficheiros con 23 adicións e 15 borrados
  1. 20 14
      SquiLu/squirrel/sqvm.cpp
  2. 3 1
      SquiLu/tests/squilu-test.nut

+ 20 - 14
SquiLu/squirrel/sqvm.cpp

@@ -718,11 +718,11 @@ bool SQVM::CLASS_OP(SQObjectPtr &target,SQInteger baseclass,SQInteger attributes
 
 bool SQVM::IsEqualDeep(const SQObjectPtr &o1,const SQObjectPtr &o2)
 {
+	if(IsEqualIdentity(o1, o2)) return true;
 	if(sq_type(o1) == sq_type(o2)) {
-		if(_rawval(o1) == _rawval(o2)) return true;
-        SQInteger rc;
-        if(ObjCmp(o1, o2, rc) && (rc == 0)) return true;
-        return false;
+        	SQInteger rc;
+       		if(ObjCmp(o1, o2, rc) && (rc == 0)) return true;
+        	return false;
 	}
 	return IsEqual(o1, o2);
 }
@@ -730,21 +730,21 @@ bool SQVM::IsEqualDeep(const SQObjectPtr &o1,const SQObjectPtr &o2)
 bool SQVM::IsEqual(const SQObjectPtr &o1,const SQObjectPtr &o2)
 {
 	bool res = false;
-	if(sq_type(o1) == sq_type(o2)) {
-		res = (_rawval(o1) == _rawval(o2));
+	if(IsEqualIdentity(o1, o2)) {
+		res = true;
 	}
 	else if(sq_isnumeric(o1) && sq_isnumeric(o2)) {
 		res = (tofloat(o1) == tofloat(o2));
 	}
 	else if(sq_type(o1) == OT_BOOL) {
-        if(sq_type(o2) & SQOBJECT_CANBEFALSE) {
-            res = _integer(o1) == (IsFalse(o2) ? 0 : 1);
-        }
+        	if(sq_type(o2) & SQOBJECT_CANBEFALSE) {
+            		res = _integer(o1) == (IsFalse(o2) ? 0 : 1);
+        	}
 	}
 	else if(sq_type(o2) == OT_BOOL) {
-        if(sq_type(o1) & SQOBJECT_CANBEFALSE) {
-            res = _integer(o2) == (IsFalse(o1) ? 0 : 1);
-        }
+        	if(sq_type(o1) & SQOBJECT_CANBEFALSE) {
+            		res = _integer(o2) == (IsFalse(o1) ? 0 : 1);
+        	}
 	}
 	return res;
 }
@@ -752,8 +752,14 @@ bool SQVM::IsEqual(const SQObjectPtr &o1,const SQObjectPtr &o2)
 bool SQVM::IsEqualIdentity(const SQObjectPtr &o1,const SQObjectPtr &o2)
 {
 	bool res = false;
-	if(sq_type(o1) == sq_type(o2)) {
-		res = (_rawval(o1) == _rawval(o2));
+	SQObjectType t1 = sq_type(o1), t2 = sq_type(o2);
+	if(t1 == t2) {
+		if (t1 == OT_FLOAT) {
+			res = (_float(o1) == _float(o2));
+		}
+		else {
+			res = (_rawval(o1) == _rawval(o2));
+		}
 	}
 	return res;
 }

+ 3 - 1
SquiLu/tests/squilu-test.nut

@@ -1484,6 +1484,7 @@ sqt.run("number", function(){
 	sqt.ok((123).tostring() == "123");
 	sqt.ok((-123).tostring() == "-123");
 	//sqt.ok((-0).tostring() == "-0");
+	//sqt.ok((-0.0).tostring() == "-0.0");
 	sqt.ok((12.34).tostring() == "12.34");
 	sqt.ok((-0.0001).tostring() == "-0.0001");
 
@@ -1534,6 +1535,7 @@ sqt.run("number", function(){
 	sqt.ok(123 != 124);
 	sqt.ok(-3 != 3);
 	sqt.ok(0 == -0);
+	sqt.ok(0.0 == -0.0);
 	sqt.ok(123 != "123");
 	sqt.ok(1 == true);
 	sqt.ok(0 == false);
@@ -1950,4 +1952,4 @@ sqt.run("Typescript syntax", function(){
 	sqt.ok( add(a,b) == 50 );
 });
 
-return sqt.results();           //show results
+return sqt.results();           //show results