Comparison of long numeric strings

Source: Internet
Author: User
Comparison of long numeric strings. This post was last edited by anyilaoliu at 15:04:43, January 13 ,.

Example
This problem occurs.

$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 can we compare the two in the second case?

Problem 2: The two groups of numbers read from the database are displayed as string (18) after var_dump, but the comparison result is the opposite of the above example, the comparison between the two strings is also passed.

Solution


Reply to discussion (solution)

The integer with a length of more than 11 characters must be processed as a string.
The characteristics of floating point cannot be used in scenarios with high precision requirements.

The integer with a length of more than 11 characters must be processed as a string.
The characteristics of floating point cannot be used in scenarios with high precision requirements.

I learned this.

In addition, I tried again today and found that two 18-bit Arabic numbers in string format in my environment, the = operator will still ignore the last two digits, it should have been converted internally.

This problem does not occur in another environment. the Two Strings can use the = operator correctly according to the string.

Ask about settings or extensions that cause this phenomenon.

Most of the php environments we use are 32-bit, while in a 32-bit environment, a long integer is represented in 4 bytes.
That is, between-pow () and pow ()
Pow (2147483648) =
Pow (4294967296) =
This obviously cannot meet general requirements, so php has made some extensions based on the logn type. 6 bytes are used to store one integer.
That is, a positive number between-99999999999999 and 99999999999999, that is, a 14-digit integer.

In 64-bit php, this limit can be easily broken through, because in a 64-bit environment, a logn type itself is 8 bytes.


No conversion with ===

Most of the php environments we use are 32-bit, while in a 32-bit environment, a long integer is represented in 4 bytes.
That is, between-pow () and pow ()
Pow (2147483648) =
Pow (4294967296) =
This obviously cannot meet general requirements, so php has made some extensions based on the logn type. 6 bytes are used to store one integer.
That is, a positive number between-99999999999999 and 99999999999999, that is, a 14-digit integer.

In 64-bit php, this limit can be easily broken through, because in a 64-bit environment, a logn type itself is 8 bytes.
I am very grateful to xu for understanding the problem. Why didn't I think of the 64-bit and 32-bit problems before? of course, this is also related to my failure to read the PHP code and documentation.

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.