Doubts about numbers comparison

Aug 29, 2011 at 12:28 PM


I've a little question about numbers comparison of different types..

I tried some comparisons beetween the same number of different types.

double a = 8;
int b = 8;

jint.SetParameter("num_double", a);
jint.SetParameter("num_int", b);

string script = @"
  return num_int == num_double;
----- and -----
return System.Int16.Parse("8") == System.Int32.Parse("8");

the two above scripts will return False as result.

A strict comparison should effectively fail because types are different, but in jint (lets say c# or javascript) i expected it would not.

Can i have some clarifications for this aspect?

For example, in PHP there are two different operators to differentiate this situation (== and ===) and the normal equal operator compares only values.

How is managed the comparison beetween numbers?

Thank you in advance :)

Aug 29, 2011 at 1:42 PM

In strictly typed languages a compiler does all background to convert all operands to make them comparable.

Actually in the second script you got marshaled Int32 and Int16 values and they are considered as CLR objects of different types, therefore they are not equal (See Object.Equals(object) method to get more information).

In the first example, one object is a native JS Number, and the second is marshaled Int32 object (CLR object), since the only one object is CLR it's converted to a corresponding native JS primitive and only then comparison occurs. Therefore if your first statement is true it should be considered as a bug.

Aug 31, 2011 at 4:10 PM

Hi Cin, 

thank you for replying.

I still have some doubts about this fact.

If i execute previous second script in .net :

return System.Int16.Parse("8") == System.Int32.Parse("8");

it returns true

I was expecting to obtain same result in jint, while in this case i obtain an opposite result of c#

Thanks for patience

Aug 31, 2011 at 6:15 PM
Edited Aug 31, 2011 at 8:56 PM

As I mentioned above, a strong typed language like C# knows everything about types at the compile time and has some rules which is applied to the numeric types (this rules can vary from language to language), for example when a compiler sees that you try to compare two different operands it generates some code which first coerces operands to the same type and only then compares. This is a feature of the language and nothing more than implicit form of the following statement

return (System.Int32)System.Int16.Parse("8") == System.Int32.Parse("8");

since JS is a dynamic language and doesn't have any information about types except at runtime it can't suggest how operands should be compared. For native JS values like numbers, strings, objects Jint uses a standard algorithm defined in ecma262, for native CLR  types it delegates all work to 'Equals' method:

return System.Int16.Parse("8").Equals( System.Int32.Parse("8") );

you can do coercing manually, for example:

return Number(System.Int16.Parse("8")) == Number(System.Int32.Parse("8"));
Aug 31, 2011 at 9:04 PM

Following this topic, let's look to the code below

var x = new Number(10);
var y = new Number(10);

return x == y;

the result will be false, but if we use explicit conversion

var x = new Number(10);
var y = new Number(10);

return Number(x) == Number(y);
the result will be true

Sep 1, 2011 at 7:31 AM

Thank you a lot Cin, 

now i've better understood.