Skip to content

Instantly share code, notes, and snippets.

@HarlemSquirrel
Last active February 24, 2023 21:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HarlemSquirrel/1d72133516aa51826c2b5a38aec15cf8 to your computer and use it in GitHub Desktop.
Save HarlemSquirrel/1d72133516aa51826c2b5a38aec15cf8 to your computer and use it in GitHub Desktop.
Ruby float and decimal math
##
# Run this with different versions of Ruby and BigDecimal to see different results.
#
# https://ruby-doc.org/3.2.1/exts/bigdecimal/BigDecimal.html
#
require "bigdecimal/util"
result_float_math = ((111.87 - 99) * 2)
puts "= Ruby v#{RUBY_VERSION}"
puts "= BigDecimal v#{BigDecimal::VERSION}"
puts "= Float::DIG #{Float::DIG}"
puts "== Basic conversion"
float = -8.37
decimal = float.to_d
puts "#{float.class} #{float}"
puts "#{decimal.class} #{decimal}"
puts "== Math"
puts "((111.87 - 99) * 2) => #{result_float_math.class} #{result_float_math}"
result_float_math_dec = result_float_math.to_d
puts "((111.87 - 99) * 2).to_d => #{result_float_math_dec.class} #{result_float_math_dec}"
result_dec_math = ((111.87 - 99).to_d * 2)
puts "((111.87 - 99).to_d * 2) => #{result_dec_math.class} #{result_dec_math}"
@HarlemSquirrel
Copy link
Author

╰─➤  rvm use 2.7.3 do ruby decimate.rb
= Ruby v2.7.3
= BigDecimal v2.0.0
= Float::DIG 15
== Basic conversion
Float -8.37
BigDecimal -0.837e1
== Math
((111.87 - 99) * 2)      => Float 25.74000000000001
((111.87 - 99) * 2).to_d => BigDecimal 0.2574e2
((111.87 - 99).to_d * 2) => BigDecimal 0.2574e2


╰─➤  rvm use 3.0.5 do ruby decimate.rb
= Ruby v3.0.5
= BigDecimal v3.0.0
= Float::DIG 15
== Basic conversion
Float -8.37
BigDecimal -0.8369999999999999e1
== Math
((111.87 - 99) * 2)      => Float 25.74000000000001
((111.87 - 99) * 2).to_d => BigDecimal 0.2574000000000001e2
((111.87 - 99).to_d * 2) => BigDecimal 0.2574e2


╰─➤  rvm use 3.2.1 do ruby decimate.rb
= Ruby v3.2.1
= BigDecimal v3.1.3
= Float::DIG 15
== Basic conversion
Float -8.37
BigDecimal -0.837e1
== Math
((111.87 - 99) * 2)      => Float 25.74000000000001
((111.87 - 99) * 2).to_d => BigDecimal 0.2574000000000001e2
((111.87 - 99).to_d * 2) => BigDecimal 0.2574e2

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