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

Make BigRational.new(BigFloat) exact #13295

Merged
merged 2 commits into from
Apr 12, 2023

Conversation

HertzDevil
Copy link
Contributor

BigRational.new(Float::Primitive) is exact, and so is BigFloat.new(BigRational), which GMP provides. BigRational.new(BigFloat) isn't, because it shares the same overload as primitive floats, and assumes only 52 bits of precision are available in the mantissa:

# the default precision is already 64 bits almost everywhere
BigFloat.default_precision = 64

x = 2.to_big_f ** 64
x          # => 1.8446744073709551616e+19
x.to_big_r # => 18446744073709551616

x = 2.to_big_f ** 64 + 1.to_big_f
x          # => 1.8446744073709551617e+19
x.to_big_r # => 18446744073709551616

x = 2.to_big_f ** 64 + 2048.to_big_f
x          # => 1.8446744073709553664e+19
x.to_big_r # => 18446744073709551616

x = 2.to_big_f ** 64 + 4095.to_big_f
x          # => 1.8446744073709555711e+19
x.to_big_r # => 18446744073709551616

x = 2.to_big_f ** 64 + 4096.to_big_f
x          # => 1.8446744073709555712e+19
x.to_big_r # => 18446744073709555712

This PR ensures no bits are lost after using Math.frexp to decompose the argument.

@straight-shoota straight-shoota added this to the 1.8.0 milestone Apr 6, 2023
@straight-shoota straight-shoota merged commit 953ba96 into crystal-lang:master Apr 12, 2023
@HertzDevil HertzDevil deleted the bug/bigfloat-to_big_r branch April 12, 2023 12:33
straight-shoota pushed a commit to straight-shoota/crystal that referenced this pull request Apr 13, 2023
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants