In Ruby 2.2.0, why does:
BigDecimal.new(34.13985572755337, 9)
equal 34.0
but
BigDecimal.new(34.13985572755338, 9)
equal 34.1398557
?
Note that I am running this on a 64 bit machine.
In Ruby 2.2.0, why does:
BigDecimal.new(34.13985572755337, 9)
equal 34.0
but
BigDecimal.new(34.13985572755338, 9)
equal 34.1398557
?
Note that I am running this on a 64 bit machine.
In general, you can't get reliable behavior with Floats. You're making the mistake of initializing your BigDecimals with Float values instead of String values, which introduces some imprecision right at the beginning. For example, on my 64-bit system:
float1 = 34.13985572755337
float2 = 34.13985572755338
# You can use string literals here, too, if your Float can't be properly
# represented. For example:
#
# BigDecimal.new("34.13985572755337", 9)
#
# would be safer, but Float#to_s works fine with the provided corpus.
bd1 = BigDecimal.new(float1.to_s, 9)
bd2 = BigDecimal.new(float2.to_s, 9)
bd1.to_s
#=> "0.3413985572755337E2"
bd2.to_s
#=> "0.3413985572755338E2"
bd1.to_f == float1
#=> true
bd2.to_f == float2
#=> true
This is one of those cases where the internal representation of the arguments matter. Therefore, your mileage will vary depending on how you initialize your object.
34.13985572755337
just fine, the problem is that somewhere in the guts of BigDecimal, some floating point values are getting rounded in bizarre ways. I don't think ~0.13 counts as a small imprecision with values ~34. This answer is a complete cop-out. –
Dieselelectric © 2022 - 2024 — McMap. All rights reserved.
BigDecimal.new(34.13985572755337, 9).to_f
returns34.1398557
on my system ... – FrazeBigDecimal.new('34.13985572755337', 9)
when i do this it works great. – PlausiveFloat
etc. Obviously, not the case. – PlausiveBigDecimal.new('34.13985572755337', 9)
is a better 2nd example. – Freudian34.0
on Ruby 2.1.x and 2.2.x and34.1398557
on Ruby 2.0.x and 1.9.x – FrazeBigDecimal.new('32.13985572755337', 9)
if you pass a string this issue doesn't appearBigDecimal.new("34.13985572755337")
. I think you can open an issue about this on bugs.ruby-lang.org – HugFloat a = 34.13985572755337 BigDecimal.new( a , 9) Float b = 34.13985572755338 BigDecimal.new( b , 9)
– Bacchanalia