The manual is not clear on the comparison of floating-point numbers

Source: Internet
Author: User
The manual is not clear on the comparison of floating-point numbers;
$a = 1.11;
$b = 1.11;

if ($a = = $b) {
The result is true;
}

Not to say never compare two floating-point numbers for equality?
What is the meaning of this, the calculation of the floating point number can not be compared?
------to solve the idea----------------------
Not to say not to compare, but to say that because of the accuracy of the reason, seemingly the same two numbers, compared not equal
You compare two of the same precision number, of course there is no problem


------to solve the idea----------------------
Not to compare, just not accurate.

For example:

$a = 1.1000000000000000001;
$b = 1.1000000000000000011;

if ($a = = $b) {
echo ' equal ';
}else{
echo ' Not equal ';
}


Output equal
------to solve the idea----------------------
$a = 1.00000000000000001;
$b = 1.00000000000000002;
Var_dump ($a = = $b); BOOL (true) is actually equal

$s = pack (' F ', $a);
for ($i =0; $i
Echo Php_eol;

$s = pack (' F ', $b);
for ($i =0; $i
Echo Php_eol;

It turns out they're the same when they save it on a computer.
00000000 00000000 10000000 00111111
00000000 00000000 10000000 00111111
  • 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.