JustinPGriffen
JustinPGriffen

Reputation: 327

PHP Subtraction Returning Infinite Number

I have an array that store data. If I subtract two arrays I get an infinately big number. Here is an example

$i[1] = 2.14;
$i[2] = 2.15;
$diff = $i[1] - $i[2];
echo $diff;

The output of this code should be -1 but instead I am getting -0.0099999999999998? With the code I am making I need the numbers to be exact. Does anyone know why this is happening and how I can fix it?
Thank you

Upvotes: 0

Views: 199

Answers (1)

Hamish
Hamish

Reputation: 23346

This is because of inaccuracies introduced in floating point operations.

For arbitrary precision operations see BCMath in the manual.

Upvotes: 5

Related Questions