D
ddoherty03
All,
Would someone try the below unit test and explain why ruby does not
pass. It came up while I was trying to write a generalized rounding
method for floats that takes the number of place to round to as a
parameter. It gave wrong answers, but only on rare inputs.
For example, (9.245 * 100).round should be 925, but my ruby gives 924.
=================================
#! /usr/bin/env ruby
#
require 'test/unit'
class TestFloatRounding < Test::Unit::TestCase
def test_round
assert_equal(925, 924.5.round, "Rounding Error: 924.5.round")
assert_equal(9.25, (9.245 * 10.0**2).round / 10.0**2, "Rounding
Error: (9.245 * 10.0**2).round / 10.0**2")
assert_equal(925, (9.245 * 10.0**2).round, "Rounding Error
9.245
* 10.0**2).round")
assert_equal(924.5, 9.245 * 10.0**2, "Rounding Error: 9.245 *
10.0**2")
assert_equal(925, 924.5.round, "Rounding Error: 924.5.round")
assert_equal(924.5, 9.245 * 10 * 10, "Rounding Error: 9.245 * 10 *
10")
assert_equal(925, (9.245 * 10 * 10).round, "Rounding Error: (9.245
* 10 * 10).round")
assert_equal(925.0, 9.245 * 10 * 10 + 0.5, "Rounding Error: 9.245
* 10 * 10 + 0.5")
assert_equal(925, (9.245 * 10 * 10 + 0.5).floor, "Rounding Error:
(9.245 * 10 * 10 + 0.5).floor")
end
end
===================================
Would someone try the below unit test and explain why ruby does not
pass. It came up while I was trying to write a generalized rounding
method for floats that takes the number of place to round to as a
parameter. It gave wrong answers, but only on rare inputs.
For example, (9.245 * 100).round should be 925, but my ruby gives 924.
=================================
#! /usr/bin/env ruby
#
require 'test/unit'
class TestFloatRounding < Test::Unit::TestCase
def test_round
assert_equal(925, 924.5.round, "Rounding Error: 924.5.round")
assert_equal(9.25, (9.245 * 10.0**2).round / 10.0**2, "Rounding
Error: (9.245 * 10.0**2).round / 10.0**2")
assert_equal(925, (9.245 * 10.0**2).round, "Rounding Error
* 10.0**2).round")
assert_equal(924.5, 9.245 * 10.0**2, "Rounding Error: 9.245 *
10.0**2")
assert_equal(925, 924.5.round, "Rounding Error: 924.5.round")
assert_equal(924.5, 9.245 * 10 * 10, "Rounding Error: 9.245 * 10 *
10")
assert_equal(925, (9.245 * 10 * 10).round, "Rounding Error: (9.245
* 10 * 10).round")
assert_equal(925.0, 9.245 * 10 * 10 + 0.5, "Rounding Error: 9.245
* 10 * 10 + 0.5")
assert_equal(925, (9.245 * 10 * 10 + 0.5).floor, "Rounding Error:
(9.245 * 10 * 10 + 0.5).floor")
end
end
===================================