Jump to content

[SOLVED] '210010106140040100' == '210010106140040101' ==> true


liviococco

Recommended Posts

Debugging this simple line of a PHP script

 

if($a == $b){ } 

 

I've found that with value of

$a = '210010106140040100' (type = string)

$b = '210010106140040101' (type = string)

 

the result of the comparison is TRUE

 

the same if i compare 2 integer

 

$a = 210010106140040100 (type = int)

$b = 210010106140040101 (type = int)

 

$a == $b  ( true )

 

If I use the === operator

 

'210010106140040100' ===  '210010106140040101'  (comparison of strings: result is FALSE)

210010106140040100  ===  210010106140040101  (comparison of numbers: result is TRUE)

 

I think it could be caused by the fact that the string is 'like' integer, but exceed the standard size of integer number.

But the final result remain a mistery for me: I've tested some others 'big' numbers and I've found for example:

 

18014398509481980==18014398509481981 (true)  !!!!

18014398509481982==18014398509481981 (false) !!!!

 

In my specific script i can handle this specific comparison using the === operator, because i can assume that the variable are string.

 

But i'd like to know why this happen, and i'd appreciate suggestions about how to efficiently compare 'big' integers.

 

Thanks!

 

I use PHP 5.2 with IIS

 

 

 

Link to comment
Share on other sites

You're correct thinking it's because these numbers are larger than maximum integer limit. Once a number exceeds this limit, PHP stores it as double precision number which - despite its promising name - is not precise.

 

http://en.wikipedia.org/wiki/Double_precision_floating-point_format

 

[added]

 

You can use mbstring bc math extension for dealing with large integers.

Link to comment
Share on other sites

  • 2 weeks later...

You're correct thinking it's because these numbers are larger than maximum integer limit. Once a number exceeds this limit, PHP stores it as double precision number which - despite its promising name - is not precise.

 

http://en.wikipedia.org/wiki/Double_precision_floating-point_format

 

[added]

 

You can use mbstring bc math extension for dealing with large integers.

 

In my example i needed to compare strings, and the 'numeric' value of strings was just a coincidence that broke my code ( so BC extension is not a choice ) .

 

I've fixed it using '===' operators, but thanks a lot for helping discover the cause.

 

Please mark as Resolved.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.