php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #55843 float gives strange result when division is done prior.
Submitted: 2011-10-04 14:29 UTC Modified: 2011-10-05 00:41 UTC
From: lcdtm at yahoo dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: Irrelevant OS: linux mandriva 2011
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: lcdtm at yahoo dot com
New email:
PHP Version: OS:

 

 [2011-10-04 14:29 UTC] lcdtm at yahoo dot com
Description:
------------
float gives strange result when division is done prior.

Test script:
---------------
$tempout = 5037;
$tempin = 5000;
$tempdiff = $tempout - $tempin;

$tempout = ($tempout / 100);
$tempin = ($tempin / 100);
$tempdiff = ($tempdiff / 100);

$tempdiffActual = $tempout - $tempin;	

var_dump($tempdiffActual);
echo "gives: float(0.37) <br>";

var_dump($tempdiff);
echo "gives: float(0.37) <br>";
	
$something = 	$tempdiffActual - $tempdiff;

var_dump($something);
echo "gives: float(-2.5535129566379E-15). Should be 0 <br>";


Expected result:
----------------
var_dump($something);

should be float(0)


Actual result:
--------------
var_dump($something);

result: float(-2.5535129566379E-15)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-10-05 00:41 UTC] aharvey@php.net
-Status: Open +Status: Bogus
 [2011-10-05 00:41 UTC] aharvey@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.


 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu Dec 18 06:00:01 2025 UTC