The strange phenomenon of the addition of two floats ~ solution ideas

Source: Internet
Author: User
The strange phenomenon of two floats added ~ ~ ~
PHP Code
  
   List ($usec, $sec) = Explode (", microtime ());p rintf (" s:%.16f ", (float) $sec);p rintf (" microseconds:%.16f ", (float) $usec);p rint F ("Add:%.16f", (float) $usec + (float) $sec);

The output is:
Seconds: 1327048300.0000000000000000
Microseconds: 0.9687510000000000
Add: 1327048300.9687509536743164

I know that the float is an approximation, but I'm printing $usec and keeping 16 digits after the decimal point, does it have a decimal number? Doesn't that mean it's equal to 1327048300?
Why isn't it 1327048300.9687510000000000 when it's two added??

Thank you!!

------Solution--------------------
Warning
Floating point Accuracy
It is obvious that a simple decimal score like 0.1 or 0.7 cannot be converted to an internal binary format without losing a little bit of precision. This can result in confusion: for example, floor ((0.1+0.7) *10 typically returns 7 instead of 8 as expected because the internal representation of the result is actually similar to 7.9999999999 ....
This is related to the fact that it is impossible to accurately express certain decimal fractions with a finite number of digits. For example, the decimal 1/3 becomes 0.3333333. . .。

So never believe that the floating-point number is accurate to the last one, and never compare two floating-point numbers for equality. If you do need a higher precision, you should use any mathematical or GMP function with any precision.
------Solution--------------------
Even if it's not clear what a valid number is, I don't understand why your parents spend a lot of money on your college.
  • 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.