[ruby-core:76260] [Ruby trunk Bug#12548] Ruby sprintf bug

From: shyouhei@...
Date: 2016-07-05 03:01:41 UTC
List: ruby-core #76260
Issue #12548 has been updated by Shyouhei Urabe.


I doubt if this is a bug or not.  The rounding mode you describe is so-called "round to the nearest even" which is described in ISO 31-0 as "generally preferable".  Maybe the author of sprintf explicitly chose such mode (not sure).

It seems the problem here is whether should we specify sprintf's rounding mode or leave it implementation-defined.

----------------------------------------
Bug #12548: Ruby sprintf bug
https://bugs.ruby-lang.org/issues/12548#change-59497

* Author: Kieran McCusker
* Status: Open
* Priority: Normal
* Assignee: 
* ruby -v: 2.3.1
* 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




-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:[email protected]?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>

In This Thread

Prev Next