Bug #66737 [Opn->Nab]: 5.5 wrongly rounded to 5

From: Date: Tue, 18 Feb 2014 20:55:53 +0000
Subject: Bug #66737 [Opn->Nab]: 5.5 wrongly rounded to 5
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-184350@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=66737&edit=1 ID: 66737 Updated by: requinix@php.net Reported by: mail at daniel-berlin dot de Summary: 5.5 wrongly rounded to 5 -Status: Open +Status: Not a bug Type: Bug Package: Math related Operating System: Linux PHP Version: 5.5.9 Block user comment: N Private report: N 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://www.floating-point-gui.de/ Thank you for your interest in PHP. On my system, pow(1024, $n - floor($n)) == 5.4999999999989. Previous Comments: ------------------------------------------------------------------------ [2014-02-18 20:30:31] mail at daniel-berlin dot de Reproducible with PHP 5.5.4 (cli) ------------------------------------------------------------------------ [2014-02-18 20:29:45] mail at daniel-berlin dot de Description: ------------ 5.5 is wrongly rounded to 5 (even with PHP_ROUND_HALF_UP) Test script: --------------- <?php $n = 2.2459431618637; echo "Expected 5.5 / is " . round(pow(1024, $n - floor($n)), 2, PHP_ROUND_HALF_UP) . "\n"; echo "Expected 5.5 / is " . round(pow(1024, $n - floor($n)), 1, PHP_ROUND_HALF_UP) . "\n"; echo "Expected 6 / is " . round(pow(1024, $n - floor($n)), 0, PHP_ROUND_HALF_UP) . "\n"; echo "Expected 5.50 / is " . number_format(pow(1024, $n - floor($n)), 2) . "\n"; echo "Expected 5.5 / is " . number_format(pow(1024, $n - floor($n)), 1) . "\n"; echo "Expected 6 / is " . number_format(pow(1024, $n - floor($n)), 0) . "\n"; Expected result: ---------------- See output (line 3 and 6 are wrong): Expected 5.5 / is 5.5 Expected 5.5 / is 5.5 Expected 6 / is 5 Expected 5.50 / is 5.50 Expected 5.5 / is 5.5 Expected 6 / is 5 Actual result: -------------- See expected result. ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=66737&edit=1

« previous php.bugs (#184350) next »