Floating point precision warning in PHP

Source: Internet
Author: User
The floating point precision warning in PHP is a prompt in PHPdocument: a simple decimal score is like 0.1 or 0.7 and cannot be converted to an internal binary format without losing a little precision. Floor (0.1 + 0.7) * 10 )?? Returns a warning of the floating point precision of 7 instead of PHP.

This is the prompt in PHP document:


A simple decimal score is like0.1Or0.7It cannot be converted to an internal binary format without losing a little bit of precision.

Floor (0.1 + 0.7) * 10)? ? Return7Instead8,

(Int) (0.1 + 0.7) * 10 )? Return 7 instead of 8.

?

The explanation for this phenomenon is "because the internal representation of the result is actually similar.7.9 ".

?

So the conclusion is:

Therefore, never believe that the result of a floating point number is accurate to the last digit, or compare whether the two floating points are equal.

Never forcibly convert an unknown score to an Int.

?

(Int) round (0.1 + 0.7) * 10) this seems safe.

The BC math function is used for numerical accuracy when a large number of multiplication or division is performed or the center value is float.

?

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.