Skip to content

integer arithmetic #42

Open
Open

Description

@ennorehling

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?

Activity

  1. gfwilliams commented on Jan 4, 2022

    @gfwilliams
    Owner

    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.

  2. ennorehling commented on Jan 5, 2022

    @ennorehling
    Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions