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
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
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

Add a Patch

Pull Requests

Add a Pull Request

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-2024 The PHP Group
All rights reserved.
Last updated: Thu May 02 09:01:28 2024 UTC