Issue #12548 has been updated by unclekiki (Kieran McCusker).


Hi again

After I raised this bug and saw the comment from matz I stopped watching assuming that sprintf would be "fixed" not that round would be changed instead. IEEE 754 is useful when you are taking them mean of a large number of rounded values and care about the mean. Rounding to even will give you a more accurate rounded mean in large sets of randomly distributed data. When rounding individual numbers it seems very unintuitive

This suggests that the typical use of sprintf is to take a large number of floating point values round them so they can be converted back from string to floats and then summed thus preserving their summed mean. Hummm, would it not be more likely to be used for formatting percentage values etc. where users would not expect 11.5 and 12.5 to be formatted to 12%.

Wouldn't it have been more sensible to make sprintf use Round half away from zero as round does (and JRuby's implementation did when I last looked) this is what I assumed would happen. There may well be a separate issue that round could be made aware of IEEE 754 for the fairly specialized case I first outlined.

As it stands in ruby 2.5 my original issue is still outstanding and the two uses are still inconsistent.

Thanks

Kieran

----------------------------------------
Bug #12548: Rounding modes inconsistency between round versus sprintf
https://bugs.ruby-lang.org/issues/12548#change-70435

* Author: unclekiki (Kieran McCusker)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
* 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:

```ruby
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:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>