[ruby-core:77350] [Ruby trunk Bug#12548] Rounding modes inconsistency between round versus sprintf
From:
nobu@...
Date:
2016-09-22 07:01:25 UTC
List:
ruby-core #77350
Issue #12548 has been updated by Nobuyoshi Nakada.
I have a [patch] too, but it breaks compatibilities in rubyspec.
```
1)
Float#round returns rounded values for big values FAILED
Expected 200000000000000000000
to have same value and type as 300000000000000000000
spec/rubyspec/core/float/round_spec.rb:70:in `block (2 levels) in <top (required)>'
spec/rubyspec/core/float/round_spec.rb:3:in `<top (required)>'
2)
Integer#round returns itself rounded if passed a negative value FAILED
Expected 200
to have same value and type as 300
spec/rubyspec/core/integer/round_spec.rb:22:in `block (2 levels) in <top (required)>'
spec/rubyspec/core/integer/round_spec.rb:4:in `<top (required)>'
3)
Rational#round with no arguments (precision = 0) returns the truncated value toward the nearest integer FAILED
Expected 0
to equal 1
spec/rubyspec/shared/rational/round.rb:17:in `block (3 levels) in <top (required)>'
spec/rubyspec/core/rational/round_spec.rb:3:in `<top (required)>'
4)
Time#strftime rounds an offset to the nearest second when formatting with %z FAILED
Expected "+01:01:04"
to equal "+01:01:05"
spec/rubyspec/core/time/strftime_spec.rb:50:in `block (2 levels) in <top (required)>'
spec/rubyspec/core/time/strftime_spec.rb:7:in `<top (required)>'
```
[patch]: https://github.com/nobu/ruby/tree/round-to-even
----------------------------------------
Bug #12548: Rounding modes inconsistency between round versus sprintf
https://bugs.ruby-lang.org/issues/12548#change-60596
* Author: Kieran McCusker
* Status: Open
* Priority: Normal
* Assignee:
* ruby -v: 2.4.0
* Backport: 2.1: UNKNOWN, 2.2: UNKNOWN, 2.3: UNKNOWN
----------------------------------------
Hi
I've found this possible bug with sprintf in our production code, but it
seems very odd that no one has reported it so I'm doubting myself.
I've tested a few versions (CRuby 2.3.1 and 2.1.9) and it was present in
both.
To reproduce in irb:
sprintf('%1.0f', 12.5)
Expected result 13 actual 12
In fact if you look at the number sequence 0.5 -> 99.5 you find the even
half will all round down and the odd half round up. 12.5.round produces the correct result (13)
If you do the same in jruby 2.2.1 you get the expected result of 13.
Many thanks
Kieran
---Files--------------------------------
round_even.patch (4.43 KB)
--
https://bugs.ruby-lang.org/
Unsubscribe: <mailto:[email protected]?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>