Last active
December 15, 2015 16:38
-
-
Save metaskills/4630660 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Odd behavior in BigDecimal's util for Float#to_d | |
# on Ruby 1.9.3 and 2.0? | |
# | |
require 'bigdecimal' | |
require 'bigdecimal/util' | |
puts "85.26.to_d.to_s('F') # => #{85.26.to_d.to_s('F')}" | |
puts "BigDecimal.new('85.26').to_s('F') # => #{BigDecimal.new('85.26').to_s('F')}" | |
oddities = ('0.00'..'99.99').to_a.select do |v| | |
v.to_f.to_d.to_s('F') =~ /\.\d{3}/ | |
end | |
puts "Found #{oddities.size} oddities:" | |
puts "--------------------------------" | |
oddities.each do |v| | |
puts "Float: #{v} #to_d: #{v.to_f.to_d.to_s('F')}" | |
end |
Or more to the point. Should the oddities above be false?
BigDecimal.new('65.09') == 65.09.to_d # => true
BigDecimal.new('65.10') == 65.10.to_d # => false
BigDecimal.new('65.11') == 65.11.to_d # => true
Thats not an oddity, thats the normal problem when you are working with floating point number on a computer..
See http://wikipedia.org/wiki/Floating_point
Just use to_d. With a precision, e.g. 65.10.to_d(10)
Thanks Klaus! I think I may spend some time in ActiveRecord so that value_to_decimal(value)
takes a precision so that column reflection can pass down the proper information.
Thanks Ken! Very helpful.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-darwin12.1.0]
Found 1237 oddities:
Some sample oddities