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

bipen
  • 36,319
  • 9
  • 49
  • 62
Shakti Patel
  • 3,762
  • 4
  • 22
  • 29

10 Answers10

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
Community
  • 1
  • 1
Luca Rainone
  • 16,138
  • 2
  • 38
  • 52
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;
btlr.com
  • 139
  • 5
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.

Stan
  • 493
  • 4
  • 15
0

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

Ayman Farhat
  • 2,010
  • 3
  • 17
  • 16
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.

Praind
  • 1,551
  • 1
  • 12
  • 25
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;
Amit
  • 2,495
  • 14
  • 22
0

did you try this approach? :

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

my result is 16

  • yes it working fine but what about floating to integer type casting? – Shakti Patel Sep 27 '13 at 10:04
  • 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(). – Butt4cak3 Sep 27 '13 at 10:07
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.

Jacek Barecki
  • 429
  • 3
  • 7
0

Use round.

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

Output

16
Padmanathan J
  • 4,614
  • 5
  • 37
  • 75
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
revo
  • 47,783
  • 14
  • 74
  • 117