php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #47449 Mathenatical comparison of an expression with it's result returns false
Submitted: 2009-02-19 11:22 UTC Modified: 2009-02-19 11:25 UTC
From: office at devstudio dot ro Assigned:
Status: Not a bug Package: Feature/Change Request
PHP Version: 5.2.9RC2 OS: Windows, FreeBSD, Linux
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: office at devstudio dot ro
New email:
PHP Version: OS:

 

 [2009-02-19 11:22 UTC] office at devstudio dot ro
Description:
------------
Mathenatical comparison of an expression with it's result returns false

When I try to compare 1.5 * 0.19 with it's result, 0.285, PHP returns false, but when i display the result 1.5 * 0.19 it echoes 0.285.

Reproduce code:
---------------
<?php
// BUG!!! This expression should return TRUE, because 1.5 * 0.19 is 0.285
echo ((1.5 * 0.19) == 0.285) ? "TRUE" : "FALSE";
echo "<br />"; echo "<br />";

// Mathematical representations are the same
echo sprintf("%e", 1.5 * 0.19);
echo "<br />"; echo "<br />";
echo sprintf("%e", 0.285);
echo "<br />"; echo "<br />";

// This expression is TRUE, that's correct
echo (0.285 == 0.285) ? "TRUE" : "FALSE";
echo "<br />";


?>

Expected result:
----------------
This expression should return TRUE, because 1.5 * 0.19 is 0.285


Actual result:
--------------
Actual result is FALSE

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-02-19 11:25 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.

.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sat Sep 20 12:00:01 2025 UTC