0

I am trying to calculate this in php :

echo (int)((0.1 + 0.7) * 20);

why it return 15

Expected result: 16

Actual result:15

10
  • almost certain to be a floating point rounding error. Commented Sep 27, 2013 at 9:56
  • 2
    why do you have jquery tag here ?? removing it Commented Sep 27, 2013 at 9:56
  • because some jquery user may help me that Commented Sep 27, 2013 at 9:57
  • possible duplicate of PHP unexpected result of float to int type cast Commented Sep 27, 2013 at 9:58
  • 1
    I'm guessing it is because (0.1+0.7) *20 returns 15.9999998 and converting a float to int always rounds down Commented Sep 27, 2013 at 9:59

10 Answers 10

3

From doc: http://php.net/manual/en/language.types.float.php

Floating point numbers have limited precision. Although it depends on the system, PHP typically uses the IEEE 754 double precision format, which will give a maximum relative error due to rounding in the order of 1.11e-16. Non elementary arithmetic operations may give larger errors, and, of course, error propagation must be considered when several operations are compounded.

Additionally, rational numbers that are exactly representable as floating point numbers in base 10, like 0.1 or 0.7, do not have an exact representation as floating point numbers in base 2, which is used internally, no matter the size of the mantissa. Hence, they cannot be converted into their internal binary counterparts without a small loss of precision. This can lead to confusing results: for example, floor((0.1+0.7)*10) will usually return 7 instead of the expected 8, since the internal representation will be something like 7.9999999999999991118....

you can use BC Math Functions

$precision = 2;
echo bcmul( bcadd("0.1","0.7",$precision) ,"20",$precision); // 16.00
Sign up to request clarification or add additional context in comments.

Comments

1

you need

intval ((0.1 + 0.7) * 20);

sorry, it's wrong, but here is a workaround:

$n= ( (0.1 + 0.7) * 20);  //=16
$n2 = intval ($n."");     // cast it to string, then to int.
echo $n2;

2 Comments

It does exactly the same
(0.1 + 0.7) * 20 = 16, which is ok, so why to use casting at all?
0

This is caused by converting the decimals to their binary equivalent by PHP, this results in a loss of precision.

If you need a high degree of accuracy, use GMP or the bcmath library instead.

Comments

0

Type casting the result is leading to a wrong number echo ((0.1 + 0.7) * 20); should give 16

1 Comment

yes it working fine but what about floating to integer type casting?
0

Looks like a rounding error

0.1 + 0.7 = 0.7999999999

0.799999999 * 20 = 15.9999998

int(15.9999998) = 15

you have to round the result.

Comments

0
echo (int)(((0.1*10 + 0.7*10)/10) * 20);

First you need to get rid of the decimal then perform the operation else don't typecast

echo (0.1+0.7)*20;

1 Comment

don't typecast float to int.
0

did you try this approach? :

<?php
$value = (0.1+0.7)*20;
echo $value;
?>

my result is 16

2 Comments

yes it working fine but what about floating to integer type casting?
You don't have to cast the result to an integer, because as soon as you print it with echo it gets transformed into its string representation. If you want to round the result, use round().
0

Please read PHP documentation on foating point numbers. As is written in the documentation,

For example, floor((0.1+0.7)*10) will usually return 7 instead of the expected 8.

When working with floating point numbers, use BC Math to get correct results.

Comments

0

Use round.

echo round((0.1 + 0.7) * 20);

Output

16

Comments

0

Let's be more precise:

printf("%1\$.20f", 0.7); // output: 0.69999999999999995559
printf("%1\$.20f", 0.1); // output: 0.10000000000000000555
printf("%1\$.20f", (0.10000000000000000555+0.69999999999999995559)*20); // output: 15.99999999999999822364

so when you cast the last number to int :

echo (int)15.99999999999999822364; // output: 15

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.