Warning about floating point precision in PHP

Source: Internet
Author: User

Introduction: This is a detailed page of the floating point Precision warning in PHP. It introduces the related knowledge, skills, experience, and some PHP source code.

Class = 'pingjiaf' frameborder = '0' src = 'HTTP: // biancheng.dnbc?info/pingjia.php? Id = 336630 'rolling = 'no'>

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) returns 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.

More articles on "floating point Precision warning in PHP"

Love J2EE follow Java Michael Jackson video station JSON online tools

Http://biancheng.dnbcw.info/php/336630.html pageno: 10.

Related Article

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.