Bug #44898 [Com]: Operators not acting correctly to floatval results

From: Date: Fri, 05 Dec 2014 14:15:29 +0000
Subject: Bug #44898 [Com]: Operators not acting correctly to floatval results
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-188932@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=44898&edit=1 ID: 44898 Comment by: robguida4 at gmail dot com Reported by: zero at infernotechnologies dot net Summary: Operators not acting correctly to floatval results Status: Not a bug Type: Bug Package: Math related Operating System: CentOS Enterprise 4.5 x86_64 PHP Version: 5.2.6 Block user comment: N Private report: N New Comment: $v = 'string'; $f = floatval($v); if ($f == $v) { echo "$f == $v" . PHP_EOL; } else { echo 'not equals' . PHP_EOL; } Previous Comments: ------------------------------------------------------------------------ [2008-05-02 20:58:28] 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. . ------------------------------------------------------------------------ [2008-05-02 20:49:24] 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:43:47] 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) ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=44898&edit=1

« previous php.bugs (#188932) next »