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
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: dancho at hahpss dot com
New email:
PHP Version: OS:

 

 [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

Pull Requests

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-2025 The PHP Group
All rights reserved.
Last updated: Mon May 12 03:01:30 2025 UTC