php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #54730 comparison float
Submitted: 2011-05-13 20:01 UTC Modified: 2011-05-14 02:49 UTC
From: dancho at hahpss dot com Assigned:
Status: Not a bug Package: Math related
PHP Version: 5.3.6 OS: Linux
Private report: No CVE-ID: None
 [2011-05-13 20:01 UTC] dancho at hahpss dot com
Description:
------------
view result

Test script:
---------------
<?php
	$v=2.06667;
	echo var_dump($v*100000)."\n";
	echo var_dump(round($v*100000,0));
	echo "\nIs equal ? "; 
	echo  ($v*100000==round($v*100000,0)) ? "true" : "false";
 ?>


Expected result:
----------------
float(206667)

float(206667)

Is equal ? true

Actual result:
--------------
float(206667)

float(206667)

Is equal ? false

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-05-14 02:49 UTC] rasmus@php.net
-Status: Open +Status: Bogus
 [2011-05-14 02:49 UTC] rasmus@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.

Try adding ini_set('precision',32); to the top of your test script and it should 
be clear why these are not equal.
 
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Tue Aug 09 20:05:45 2022 UTC