Created
October 10, 2011 11:02
-
-
Save deepak/1275050 to your computer and use it in GitHub Desktop.
float_and_bigdecimal.rb
This file contains 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
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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: