How do you calculate PHP decimals?

Source: Internet
Author: User
if (0.030! = (0.530-0.500+0+0))
{
$s = 0.530-0.500+0+0;
$s = 0.03-$s;
}


This is not equal??
It's not 0 after subtracting.

Is this PHP an alien calculation method?


Reply to discussion (solution)

Would you please cram your knowledge 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;


That's wrong.

Would you please cram your knowledge about floating-point numbers?
$f = (0.530-0.500+0+0) *1000;
$a = (0.030*1000);

$ssa = Doubleval ($a)-doubleval ($f);

It's wrong to write ... How did he figure that out?

What do you say?

Simple decimal fractions can not be converted to binary format in the case of a little bit of precision, floating-point numbers will never be accurate to the last, then in PHP, simple floating-point arithmetic is very likely to lose precision, resulting in confusion in the calculation results

What do you say?

Simple decimal fractions can not be converted to binary format in the case of a little bit of precision, floating-point numbers will never be accurate to the last, then in PHP, simple floating-point arithmetic is very likely to lose precision, resulting in confusion in the calculation results

How do you do that calculation like this?


Would you please cram your knowledge about floating-point numbers?
$f = (0.530-0.500+0+0) *1000;
$a = (0.030*1000);

$ssa = Doubleval ($a)-doubleval ($f);

It's wrong to write ... How did he figure that out?

This is not only PHP, Java also has a phenomenon
So PHP has 2 ways 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;
}
Put the variable in double quotes and it's OK.
Another way is to use the round function.

Looks like you're going to have to cram a little bit of knowledge into "effective numbers."

  • 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.