php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #6019 document precision of floating point values
Submitted: 2000-08-08 08:59 UTC Modified: 2006-04-03 09:38 UTC
From: thomas at netropolis dot dk Assigned:
Status: Closed Package: Feature/Change Request
PHP Version: 4.0.1pl2 OS: NT4.0
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: thomas at netropolis dot dk
New email:
PHP Version: OS:

 

 [2000-08-08 08:59 UTC] thomas at netropolis dot dk
When comparing certain numbers using the equals operator (==) it returns true even though numbers are not equal. It doesnt matter if number is duoble or string.
It only happens when number is 16 digit. 15 & 17 digits is OK. 
Using strcmp() doesnt produce this error.

Example:

<?php

$one = 9834567891230000; //double
$two = 9834567891230000; //double
print(($one == $two)); //returns 1 (OK)

$one = 9834567891230000; //double
$two = 9834567891230001; //double
print(($one == $two)); //returns 1 (wrong)

$one = "9834567891230000"; //string
$two = "9834567891230001"; //string
print(($one == $two)); //returns 1 (wrong)

$one = 9834567891230000; //double
$two = 9834567891230002; //double
print(($one == $two)); //returns 0 (OK)

$one = 1834567891230000; //double
$two = 1834567891230001; //double
print(($one == $two)); //returns 0 (OK)

?>

I have tried under diffrent versions of php both under linux and NT4.0 with the same result.
latest: 4.0.1pl1 NT4.0 precompiled downloaded from php.net

Can send php.ini if you want.     

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2000-08-08 09:07 UTC] stas@php.net
1. Use === when you are interested in string-to-string comparison.
2. PHP has precision limits, if you need more precision please use bc extension.
 [2000-08-08 09:25 UTC] waldschrott@php.net
verified, replacing == with === in the third block will
produce 0, what is correct

for any reason the comparison is done corretly withing
strings, I think you won?t have to switch to bcmaths
functions for these tiny doubles, using var_dump() reveals
that they?re correctly stored in $GLOABLS

resum?: PHP performs return only 0,2,4 differences not 1,3,5
dealing with large numbers
example:
echo 9834567891230000-9834567891229999; // return2 s, should 1
echo 9834567891230000-9834567891230001; // returns 0, should -1
and so on
 [2000-08-08 09:37 UTC] waldschrott@php.net
I think this limitation isn?t mentioned anywhere clearly,
transforming into docu bug
In fact I don?t know where to look for it and I think the
php.ini precision settings has nothing to do with it...
 [2000-08-08 10:34 UTC] hholzgra@php.net
the precisions are machine-/build-dependant
so we can't document them in the manual

maybe we can add a section to phpinfo()
and/or some constants as in limits.h
for MIN/MAX values for integer and float 
values

(passed on to feature request)
 [2002-04-28 18:16 UTC] jimw@php.net
updating summary.
 [2006-04-03 09:38 UTC] tony2001@php.net
See http://php.net/float
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Sep 19 15:01:27 2024 UTC