Data Structure-PHP floating point Precision

Source: Internet
Author: User
In PHPmanual, the rational number that can be accurately expressed in decimal format, such as 0.1 or 0.7, cannot be accurately expressed by any number of tails in binary format, therefore, the binary format cannot be converted without a slight loss of precision. {Code...} in example 2 and Example 3... PHP manual:

Rational numbers that can be accurately expressed in decimal form, such as 0.1 or 0.7. No matter how many tails are there, they cannot be accurately expressed by the binary used internally, therefore, the binary format cannot be converted without a slight loss of precision.

// example1$float = (0.1 + 0.7) * 10;echo (integer) $float;            // 7echo floor($float);               // 7// example2echo (integer) (1.5+1.5);         // 3echo floor(1.5+1.5);              // 3// example3echo (integer) (0.5*10);          // 5echo floor(0.5*10);               // 5

In Example 2 and Example 3, the floating pointAddAndMultiplicationCan operations preserve accuracy?

Reply content:

PHP manual says:

Rational numbers that can be accurately expressed in decimal form, such as 0.1 or 0.7. No matter how many tails are there, they cannot be accurately expressed by the binary used internally, therefore, the binary format cannot be converted without a slight loss of precision.

// example1$float = (0.1 + 0.7) * 10;echo (integer) $float;            // 7echo floor($float);               // 7// example2echo (integer) (1.5+1.5);         // 3echo floor(1.5+1.5);              // 3// example3echo (integer) (0.5*10);          // 5echo floor(0.5*10);               // 5

In Example 2 and Example 3, the floating pointAddAndMultiplicationCan operations preserve accuracy?

It's time to offer my blog again:

  • Code mystery (4)-floating point number (from surprise to thinking)
  • Code mystery (5)-floating point number (who stole your precision ?)

0.1 + 0.7The result is0.7999999999999999

0.5 1.5It can be exactly represented by a floating point number.

0.1Binary:

Symbol bit 0 index 01111011 (-4) digits 1.10011001100110011001101 (1.60000002384185791015625)

Convert the number to decimal:0.10000000149011612

0.7Binary:

Symbol bit 0 index 01111110 (-1) digits 1.01100110011001100110011 (1.39999997615814208984375)

Convert the number to decimal:0.699999988079071

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.