A comparison of longer numeric strings

Source: Internet
Author: User
This post was last edited by Anyilaoliu on 2013-12-13 15:04:43

Title
Encounter such a problem

$a = "222111565652521142"; $b = "222111565652521139"; $c = 222111565652521142; $d = 222111565652521139;var_dump ($a); echo "
"; Var_dump ($b); echo"
"; Var_dump ($c); echo"
"; Var_dump ($d); echo"
"if ($a = = $b) {echo 3;} Else{echo 31;} echo "
"if ($c = = $d) {echo 4;} Else{echo 41;} echo "
";


The result is


String ' 222111565652521142 ' (length=18) string ' 222111565652521139 ' (length=18) float 2.2211156565252e+17float 2.2211156565252E+17314


The question is, how does the second case compare the two?

Two sets of numbers read from the database, the Var_dump is a string (18) But the comparison is the opposite of the example above, and the comparison of two strings is also passed.

Solving


Reply to discussion (solution)

Integers longer than 11 bits are handled by string
The characteristics of the floating-point number itself make it impossible to use in the case of high accuracy requirement

Integers longer than 11 bits are handled by string
The characteristics of the floating-point number itself make it impossible to use in the case of high accuracy requirement

This is a learning

In addition, today again try to find under my environment, two string format 18-bit length of Arabic numerals, = = operator comparison will still ignore the latter two bits, should be the internal digital conversion

In another environment, this problem does not occur, and two strings can be used to correctly use the = = operator as a string

Ask about what settings or extensions are causing the phenomenon to occur.

Most of the PHP environments we use are 32-bit, whereas a long integer in a 32-bit environment is represented by 4 bytes.
i.e.-pow (2,31) to POW (2,31)
Pow (2,31) = 2147483648
Pow (2,32) = 4294967296
This obviously does not satisfy the general need, so PHP has done some expansion on the basis of the Logn type. 1 integers stored in 6 bytes
That is, a positive number between 99999999999999 and 99999999999999, or a 14-bit integer.

And in 64-bit PHP can easily break this limit, because 64-bit environment, a LOGN type itself is 8 bytes


with = = = does not convert

Most of the PHP environments we use are 32-bit, whereas a long integer in a 32-bit environment is represented by 4 bytes.
i.e.-pow (2,31) to POW (2,31)
Pow (2,31) = 2147483648
Pow (2,32) = 4294967296
This obviously does not satisfy the general need, so PHP has done some expansion on the basis of the Logn type. 1 integers stored in 6 bytes
That is, a positive number between 99999999999999 and 99999999999999, or a 14-bit integer.

And in 64-bit PHP can easily break this limit, because 64-bit environment, a LOGN type itself is 8 bytes
Thank you very much Xu big doubts in place, before why I did not think of 64-bit and 32-bit problem it ... This, of course, has nothing to do with the code and documentation that I have not seen in PHP itself.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.