Skip to content

Instantly share code, notes, and snippets.

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 deepak/1275050 to your computer and use it in GitHub Desktop.
Save deepak/1275050 to your computer and use it in GitHub Desktop.
float_and_bigdecimal.rb
require 'test/unit'
require 'bigdecimal'
# http://ramblingsof.justinwinkler.com/when-crap-just-doesnt-add-up
class RoundOffTest < Test::Unit::TestCase
def test_integer
assert_equal(500 * 1/35 * 35, 490)
end
def test_float
assert_equal(500 * 1/35.0 * 35.0, 500.0)
end
def test_bigdecimal
assert_equal((BigDecimal.new("500") * 1/BigDecimal.new("35") * BigDecimal.new("35")).to_f, 500.0)
end
def test_wtf
# check this out
assert_not_equal(0.1 + 0.2, 0.3)
assert_equal(BigDecimal.new("0.1") + BigDecimal.new("0.2"), 0.3)
assert_not_equal(1.2 - 1, 0.2)
assert_equal(BigDecimal.new("1.2") - BigDecimal.new("1"), 0.2)
assert_not_equal(1.upto(10_000).inject(0) { |acc| acc += 0.0001 }, 1.0)
assert_equal(1.upto(10_000).inject(0) { |acc| acc += BigDecimal.new("0.0001") }, 1.0)
end
end
@anjalshireesh
Copy link

While this is true, floats can still be used if you are always rounding to, say 2 decimals. Try adding following lines to test_wtf above:

assert_equal((0.1 + 0.2).round(2), 0.3)
assert_equal((1.2 - 1).round(2), 0.2)
assert_equal(1.upto(10_000).inject(0) { |acc| acc = (acc + 0.0001).round(4) }, 1.0)

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