PHP: awk subtraction giving exponential values

I get an awk result when I subtract two values, the error is that I get an exponent value of 2.7755575615629E-17 instead of 0. Anything that I don’t have enough to submit an application, please suggest. This happens in some cases, only as 0.66, 0.67, 0.33,

The prototype of the code I'm using is below

                    $_SESSION['x'] = 1;
            $_SESSION['x'] = $_SESSION['x'] - 0.83;
            echo ( $_SESSION['x']- 0.17) ;
            echo '<br>';

But when swapping values, everything is fine with 0

                    $_SESSION['x'] = 1;
            $_SESSION['x'] = $_SESSION['x'] - 0.17;
            echo ( $_SESSION['x']- 0.83) ;
            echo '<br>';
+2
source share
2 answers

- float . . http://en.wikipedia.org/wiki/Floating_point#Accuracy_problems. , , bcmath PHP:

$_SESSION['x'] = 1;
$_SESSION['x'] = bcsub($_SESSION['x'], 0.83, 10);
echo bcsub($_SESSION['x'], 0.17, 10);
echo '<br>';

round($result, $numberOfDecimalPlaces) .

+2

, .

" , ~ 1.8e308 14 (64- IEEE).

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

, , , (int) .

- sprintf

$a  = 0.00001234;
echo $a ;

1.234E-5

,

echo (int)$a ;
The output is 0

echo round($a) ;

output will be 0

, ,

echo sprintf('%f', $a);

We will get 0.000012
+3

All Articles