Skip to content

Instantly share code, notes, and snippets.

@metaskills
Last active December 15, 2015 16:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save metaskills/4630660 to your computer and use it in GitHub Desktop.
Save metaskills/4630660 to your computer and use it in GitHub Desktop.
# 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
@metaskills
Copy link
Author

ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-darwin12.1.0]

Found 1237 oddities:

Some sample oddities

Float: 65.10  #to_d: 65.09999999999999
Float: 64.46  #to_d: 64.45999999999999
Float: 67.21  #to_d: 67.20999999999999
Float: 75.68  #to_d: 75.68000000000001
Float: 98.26  #to_d: 98.26000000000001
65.10.to_d.to_s('F')  # => "65.09999999999999"

@metaskills
Copy link
Author

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

@KDGundermann
Copy link

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)

@metaskills
Copy link
Author

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.

@johnbhall
Copy link

Thanks Ken! Very helpful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment