Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Format of floating point numbers #22

Open
ltratt opened this issue Sep 24, 2019 · 4 comments
Open

Format of floating point numbers #22

ltratt opened this issue Sep 24, 2019 · 4 comments
Labels
help wanted Pull requests very welcome (usually easier than other issues) spec Needs specification, or is specification related.

Comments

@ltratt
Copy link

ltratt commented Sep 24, 2019

AFAIK, SOM doesn't specify how floating point numbers should be converted to strings, so there is almost certainly inconsistency across implementations. To my surprise, printing floating point numbers turns out to be an active area of research, with the recent best algorithm appearing only in 2018. yksom uses an implementation of Ryū to print out floating point numbers and it is subtly different than java-som. Consider the program:

        (1.1 + 1) println.
        (1.1 + 1.2) println.
        (1.1 + (1 << 30)) println.
        (1.1 + (1 << 60)) println.
        (1 + 1.1) println.
        (-1 - 1.1) println.
        (-1 - -1.1) println.
        (-1 * -1.1) println.
        (10 / 1.3) println.
        (10 / 0.3) println.
        (10 / 0.2) println.

java-som prints:

2.1
2.3
1.0737418251E9
1.15292150460684698E18
2.1
-2.1
0.10000000000000009
1.1
7
33
50

whereas yksom prints:

2.1
2.3
1073741825.1
1.152921504606847e18
2.1
-2.1
0.10000000000000009
1.1
7
33
50

i.e. 1.0737418251E9 -> 1073741825.1.

There are C and Java implementations of Ryū, so it is probably feasible to specify that's how floating point numbers should be printed? I admit this is a small issue in the grand scheme of things, at most, but it does make it harder to test multiple SOM variants.

@smarr
Copy link
Member

smarr commented Sep 24, 2019

I fear, it doesn't really end there.
Given that floating point operations have rounding errors, there can be differences between an interpreted execution and a compiled and optimized execution. Not sure whether Java permits such transformations, but I would assume that somewhere out there, some compiler optimized floating point operations resulting in rounding differences.

@smarr smarr added help wanted Pull requests very welcome (usually easier than other issues) spec Needs specification, or is specification related. labels Sep 24, 2019
@ltratt
Copy link
Author

ltratt commented Sep 24, 2019

Isn't it sufficient to say that floating point operations in SOM must be IEE 754 compatible?

@smarr
Copy link
Member

smarr commented Sep 24, 2019

I guess, one could say that optimizations are only allowed if they are identities according to the IEEE-754 standard.

But that would seem to preclude things such as
"Automatically improving accuracy for floating point expressions"
https://dl.acm.org/citation.cfm?id=2737959

I guess, it would be nice if accuracy is never worse than when following IEEE-754.

@ltratt
Copy link
Author

ltratt commented Sep 24, 2019

If I understand that paper correctly, it doesn't change IEEE-754, it just reorders source-level expressions so that they're less subject to rounding (which is something experienced programmers do anyway, to some extent). So I think IEEE-754 is still a reasonable spec to adhere to?

bors bot added a commit to softdevteam/yksom that referenced this issue Sep 30, 2019
47: Implement basic support for doubles. r=ptersilie a=ltratt

This implements operations such as `+-*` (though note that Doubles don't implement `/` in SOM!) but not comparisons (as we really need to improve some of our other support before it's worth fixing doubles) and many other necessary operations. However, it does largely integrate Doubles into the internal type system.

Note that this PR starts to diverge from SOM for minor reasons over formatting of doubles (SOM-st/SOM#22) and inconsistencies in Java SOM (at least) (SOM-st/SOM#20). Hopefully we will be able to fix that divergence at some point.

Co-authored-by: Laurence Tratt <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Pull requests very welcome (usually easier than other issues) spec Needs specification, or is specification related.
Projects
None yet
Development

No branches or pull requests

2 participants