alex shishkin
alex shishkin

Reputation: 195

Rounding in PHP

$a = ((0.1 + 0.7) * 10) == (int)((0.1 + 0.7) * 10);

PHP returns false.

Could anybody explain me, why that happens? First returns 8, second 7.

Upvotes: 3

Views: 328

Answers (7)

bancer
bancer

Reputation: 7525

You can do the comparison this way:

$a = round(((0.1 + 0.7) * 10), 1) == (int)round(((0.1 + 0.7) * 10), 1);

Upvotes: 0

Mark Baker
Mark Baker

Reputation: 212522

http://en.wikipedia.org/wiki/Floating_point#Accuracy_problems

EDIT

$a = ((0.1 + 0.7) * 10) == 8;
var_dump($a);

echo '<br />';

define('PRECISION', 1.0e-08);

$a = (abs(((0.1 + 0.7) * 10) - 8) < PRECISION);
var_dump($a);

Upvotes: 2

Mark Byers
Mark Byers

Reputation: 839264

Floating point arithmetic is not precise. Instead of 8.0 exactly you can get 7.999... which gets truncated to 7 when cast to an integer.

echo number_format((0.1 + 0.7) * 10, 20);

Result:

7.99999999999999911182

Upvotes: 5

Michael Borgwardt
Michael Borgwardt

Reputation: 346566

From The Flaoting-Point Guide (click for detailed explanations):

Because internally, computers use a format (binary floating-point) that cannot accurately represent a number like 0.1, 0.2 or 0.3 at all.

When the code is compiled or interpreted, your “0.1” is already rounded to the nearest number in that format, which results in a small rounding error even before the calculation happens.

Upvotes: 1

Natalie Adams
Natalie Adams

Reputation: 2041

Mark's response hits the nail on the head, but I think instead of casting you want the round PHP function:

http://php.net/manual/en/function.round.php

Upvotes: 1

Gordon
Gordon

Reputation: 317217

Quoting the big fat red warning in the PHP Manual on Floating Point Precision:

It is typical that simple decimal fractions like 0.1 or 0.7 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.9.

This is due to the fact that it is impossible to express some fractions in decimal notation with a finite number of digits. For instance, 1/3 in decimal form becomes 0.3.

So never trust floating number results to the last digit, and never compare floating point numbers for equality. If higher precision is necessary, the arbitrary precision math functions and gmp functions are available.

Upvotes: 14

Piotr M&#252;ller
Piotr M&#252;ller

Reputation: 5558

Because operating on floating point numbers isn't 100% accurate, propably before casting value from expression is something like 7.9999...

Upvotes: 0

Related Questions