Reputation: 35
floor( 500 * (1.4 / 100) )
gives me 6
and
floor( 500 * 1.4 / 100 )
gives me 7
Can anyone help me understand how PHP arithmetic works internally.
Upvotes: 1
Views: 95
Reputation: 376
It works like in any other language. Try for example javascript:
(500*(1.4 / 100)) // this will give you 6.999999999999999
(500* 1.4 / 100) // this will give you 7
The problem is, that PHP has internal setting which tells him about precission with which he displays float numbers. Try doing something like that:
php > ini_set('precision', 17);
php > echo ( 500 * (1.4 / 100) );
6.9999999999999991
php > echo ( 500 * 1.4 / 100 );
7
php >
I assume you've tried the code above without ini_set (using default settings – probably 14 as precision), and it returned you 7 in both results:
php > echo ( 500 * 1.4 / 100 );
7
php > echo ( 500 * (1.4 / 100) );
7
php >
Upvotes: 6