php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #66737 5.5 wrongly rounded to 5
Submitted: 2014-02-18 20:29 UTC Modified: 2014-02-18 20:55 UTC
From: mail at daniel-berlin dot de Assigned:
Status: Not a bug Package: Math related
PHP Version: 5.5.9 OS: Linux
Private report: No CVE-ID: None
 [2014-02-18 20:29 UTC] 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.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-02-18 20:30 UTC] mail at daniel-berlin dot de
Reproducible with PHP 5.5.4 (cli)
 [2014-02-18 20:55 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2014-02-18 20:55 UTC] requinix@php.net
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.
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Fri Jan 24 22:01:25 2020 UTC