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

Fix LDouble.to_int #583

Merged
merged 1 commit into from
Dec 7, 2018
Merged

Fix LDouble.to_int #583

merged 1 commit into from
Dec 7, 2018

Conversation

fdopen
Copy link
Contributor

@fdopen fdopen commented Dec 6, 2018

long double must be casted to intnat first, not to uintnat directly. Otherwise the conversion is undefined behavior for nearly all negative values, see: http://c0x.coding-guidelines.com/6.3.1.4.html , Note 50.

The code was correct until ocaml/ocaml@24c118d
. It seems to work as intended on x86 platforms and the CI for ARM still uses OCaml 4.02.3.

@yallop
Copy link
Owner

yallop commented Dec 6, 2018

Thanks! This is an interesting bug and an unexpected consequence of the Val_long change (/cc @gasche, who might also be interested).

)
else (
let mi = -9007199254740991. in
let ma = 9007199254740991. in
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if it's worth the hassle of exposing LDBL_MANT_DIG or similar in the LDouble module, to better test platforms that have a long double distinct from double. I think it would be good to at least have a comment to the effect that this is 2⁵³-1, and perhaps also note the guarantee that we can represent integers within that range precisely.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I've added LDouble.mant_dig that is now also used inside the test.

`long double` must be casted to `intnat` first, not to `uintnat`
directly. Otherwise the conversion is undefined behavior for nearly
all negative values, see:
http://c0x.coding-guidelines.com/6.3.1.4.html , Note 50. The code was
correct until
ocaml/ocaml@24c118d
@yallop yallop merged commit 5315981 into yallop:master Dec 7, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants