Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
Ruby
Bug Rounding Floats? (9.245 * 100).round => 924? S/B 925!
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="ddoherty03, post: 4644627"] 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 =================================== [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
Ruby
Bug Rounding Floats? (9.245 * 100).round => 924? S/B 925!
Top