#42463 [Opn->Bgs]: implicit sprintf round uses banker's algorithm.

From: Date: Tue, 28 Aug 2007 21:36:08 +0000
Subject: #42463 [Opn->Bgs]: implicit sprintf round uses banker's algorithm.
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-116786@lists.php.net to get a copy of this message
 ID:               42463
 Updated by:       jani@php.net
 Reported By:      dan dot ostrowski at gmail dot com
-Status:           Open
+Status:           Bogus
 Bug Type:         *General Issues
 Operating System: Linux
 PHP Version:      5.2.3
 New Comment:

Floating point values have a limited precision. Hence a value might 
not have the same string representation after any processing. That also
includes writing a floating point value in your script and directly 
printing it without any mathematical operations.

If you would like to know more about "floats" and what IEEE
754 is read this:
http://docs.sun.com/source/806-3568/ncg_goldberg.html
 
Thank you for your interest in PHP.




Previous Comments:
------------------------------------------------------------------------

[2007-08-28 17:17:25] dan dot ostrowski at gmail dot com

Description:
------------
When rounding (implicitly) with sprintf, halfway values are rounded
using a "banker's" algorithm instead of performing the way a call to
round() would.

This seems inconsistent and counterintuitive for no particular reason.

Thanks!

Reproduce code:
---------------
print sprintf("%2.2f", 0.015)."<br/>";
print sprintf("%2.2f", 0.025)."<br/>";
print sprintf("%2.2f", 0.035)."<br/>";
print sprintf("%2.2f", 0.045)."<br/>";
print "<br/>";
print round(0.015, 2)."<br/>";
print round(0.025, 2)."<br/>";
print round(0.035, 2)."<br/>";
print round(0.045, 2)."<br/>";

Expected result:
----------------
0.02
0.03
0.04
0.05

0.02
0.03
0.04
0.05

Actual result:
--------------
0.01
0.03
0.04
0.04

0.02
0.03
0.04
0.05


------------------------------------------------------------------------


-- 
Edit this bug report at http://bugs.php.net/?id=42463&edit=1


Thread (2 messages)

« previous php.bugs (#116786) next »