php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #44898 Operators not acting correctly to floatval results
Submitted: 2008-05-02 20:43 UTC Modified: 2008-05-02 20:58 UTC
From: zero at infernotechnologies dot net Assigned:
Status: Not a bug Package: Math related
PHP Version: 5.2.6 OS: CentOS Enterprise 4.5 x86_64
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
47 + 23 = ?
Subscribe to this entry?

 
 [2008-05-02 20:43 UTC] zero at infernotechnologies dot net
Description:
------------
floatval() two seperate strings to return a result, try result against exact same number (via >=) and condition returns false.

Reproduce code:
---------------
$value1 = '1';
$value2 = '0.9';
$value3 = 0.1;

$result = floatval(floatval($value1) - floatval($value2));

if ($result >= $value3)
{
	echo 'Working as intended';
}
else
{
	echo "$result IS NOT >= $value3<br />";
	var_dump($result, $value3);
}

Expected result:
----------------
Working as intended

Actual result:
--------------
0.1 IS NOT >= 0.1
float(0.1) float(0.1) 

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-05-02 20:49 UTC] zero at infernotechnologies dot net
It seems adding a single line fixes the issue:

Line to add:
---------------------
$result = floatval($result . ' ');


Working version:
---------------------
$value1 = '1';
$value2 = '0.9';
$value3 = 0.1;

$result = floatval(floatval($value1) - floatval($value2));

$result = floatval($result . ' ');

if ($result >= $value3)
{
	echo 'Working as intended';
}
else
{
	echo "$result IS NOT >= $value3<br />";
	var_dump($result, $value3);
}
 [2008-05-02 20:58 UTC] derick@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.

.
 [2014-12-05 14:15 UTC] robguida4 at gmail dot com
$v = 'string';
$f = floatval($v);
if ($f == $v) {
    echo "$f == $v" . PHP_EOL;
} else {
    echo 'not equals' . PHP_EOL;
}
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 16 22:01:27 2024 UTC