gfwilliams / tiny-js

Automatically exported from code.google.com/p/tiny-js
MIT License
531 stars 88 forks source link

integer arithmetic #42

Open ennorehling opened 2 years ago

ennorehling commented 2 years ago

TinyJS seems to treat integers differently from other numbers?

enno@erdbaer:~/src/tiny-js@master$ ./Script
> Interactive mode... Type quit(); to exit, or print(...); to print something, or dump() to dump the symbol table!
print(5/2);
> 2
print(5.0/2);
> 2.500000
print(123 === 123.0);
> 0

This doesn't match the behavior of other Javascript implementations, or indeed the wording at https://www.w3schools.com/js/js_numbers.asp:

Unlike many other programming languages, JavaScript does not define different types of numbers, like integers, short, long, floating-point etc. JavaScript numbers are always stored as double precision floating point numbers, following the international IEEE 754 standard.

And the standard explicitly says:

ECMAScript does not perform integer division. The operands and result of all division operations are double-precision floating-point numbers.

Is this intentional? Is there a way to make TinyJS behave like the standard? Perhaps a compile-time option could be added?

gfwilliams commented 2 years ago

Yes, unfortunately TinyJS isn't very spec compliant. It hasn't been maintained in ages - you may find that other engines like mjs do a better job for you?

Honestly the best bet would probably be to rip out all the integer maths stuff - it'd simplify the code quite a bit and would then be spec compliant.

ennorehling commented 2 years ago

I didn't know about mjs, but that looks like it will work. Thanks!