Is PHP decimal calculation too boring?

Source: Internet
Author: User
Is PHP decimal calculation too boring? If (0.030! = (0.530-0.500 + 0 + 0 ))
{
$ S = 0.530-0.500 + 0 + 0;
$ S = 0.03-$ s;
}


Which is not equal ??
Not 0 After subtraction

Is PHP an alien computing method?


Reply to discussion (solution)

Please learn more about floating point numbers.

Even if I
$ F = (0.530-0.500 + 0 + 0) * 1000;
$ A = (0.030*1000 );
$ S = (0.530-0.500 + 0 + 0) * 1000;
$ Ssa = $ a-$ s;


This is also wrong.

Please learn more about floating point numbers.
$ F = (0.530-0.500 + 0 + 0) * 1000;
$ A = (0.030*1000 );

$ Ssa = doubleval ($ a)-doubleval ($ f );

In this case, all of them are wrong... how does he calculate them?

How can this problem be solved?

A simple decimal fraction cannot be converted to a binary format with a little bit of precision lost. floating point numbers will never be accurate to the last bit. in PHP, A simple floating-point number operation may result in loss of precision and confusion in the calculation results.

How can this problem be solved?

A simple decimal fraction cannot be converted to a binary format with a little bit of precision lost. floating point numbers will never be accurate to the last bit. in PHP, A simple floating-point number operation may result in loss of precision and confusion in the calculation results.

What should we do for such computing?


Please learn more about floating point numbers.
$ F = (0.530-0.500 + 0 + 0) * 1000;
$ A = (0.030*1000 );

$ Ssa = doubleval ($ a)-doubleval ($ f );

In this case, all of them are wrong... how does he calculate them?

This is not only true for php, but also for JAVA.
So there are two methods for PHP to solve floating point numbers.
One is
If (0.030! = (0.530-0.500 + 0 + 0 ))
{
$ S = 0.53-0.5;
$ S = 0.03-"$ s ";
Echo $ s;
}
Add up the variable's double quotation marks.
Another way is to use the round function.

It seems that you have to learn more about "effective number ".

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.