php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #44110 intval problem with conversion from float to int (without decimal places)
Submitted: 2008-02-13 16:14 UTC Modified: 2008-02-13 16:52 UTC
From: dstuff at brainsware dot org Assigned:
Status: Not a bug Package: *Math Functions
PHP Version: 5.2.5 OS: Any
Private report: No CVE-ID: None
 [2008-02-13 16:14 UTC] dstuff at brainsware dot org
Description:
------------
Some float values after multiplication by 100 (for elemination of decimal places) get rounded down by one after running intval() on the result.

Reproduce code:
---------------
<?php
// prior calculation:
echo intval(70.74 * 100);

// testing for all numbers between 0 and 10 that do not hold the same value after type change
for ($j = 0.00; $j < 10.0; $j = $j + 0.01) {
	$i = ($j);
	if (strval(intval($i * 100)) != strval(($i * 100))) {
		echo $i . ': ' . intval($i*100) . ' | ' . ($i * 100) . '<br />';
	}
}

?>

Expected result:
----------------
Expected value after evaluation: 7047

Actual result:
--------------
Actual value after evaluation: 7073

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-02-13 16:52 UTC] jani@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://docs.sun.com/source/806-3568/ncg_goldberg.html
 
Thank you for your interest in PHP.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 16:01:29 2024 UTC