mathematical discrepancies
am 14.08.2008 14:55:13 von sublimino
Hello,
Can anybody shed any light on this behaviour please?
PHP Version 5.2.5 on XP
$a = 1754.00 ;
$b = 1754.03 ;
$diff = abs($b) - abs($a);
echo "$diff
"; //output: 0.03
$a = 1754.00 ;
$b = 1754.09 ;
$diff = abs($b) - abs($a);
echo "$diff
"; //output :0.0899999999999
$a = 1754.01 ;
$b = 1754.02 ;
$diff = abs($b) - abs($a);
echo $diff; //output: 0.00999999999999
Is this expected behaviour? Can anybody reproduce this?
Many thanks,
Andy
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: mathematical discrepancies
am 14.08.2008 15:00:13 von Evert Lammerts
Check http://php.net/manual/en/language.types.float.php
Quote:
Warning
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.
On Thu, Aug 14, 2008 at 2:55 PM, Andrew Martin wrote:
> Hello,
>
> Can anybody shed any light on this behaviour please?
>
> PHP Version 5.2.5 on XP
>
>
> $a = 1754.00 ;
> $b = 1754.03 ;
> $diff = abs($b) - abs($a);
>
> echo "$diff
"; //output: 0.03
>
> $a = 1754.00 ;
> $b = 1754.09 ;
> $diff = abs($b) - abs($a);
>
> echo "$diff
"; //output :0.0899999999999
>
> $a = 1754.01 ;
> $b = 1754.02 ;
> $diff = abs($b) - abs($a);
>
> echo $diff; //output: 0.00999999999999
>
>
> Is this expected behaviour? Can anybody reproduce this?
>
> Many thanks,
>
>
> Andy
>
> --
> PHP Database Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: mathematical discrepancies
am 14.08.2008 15:09:52 von Yves Sucaet
Well, yes, it is expected behavior. I think you're confusing the abs() with
the round() function (see Evert's answer earlier).
abs(-1.05) = 1.05
round(-1.05) = -1
abs(1.35) = 1.35
round(1.35) = 1
hth,
Yves
----- Original Message -----
From: "Andrew Martin"
To:
Sent: Thursday, August 14, 2008 7:55 AM
Subject: [PHP-DB] mathematical discrepancies
> Hello,
>
> Can anybody shed any light on this behaviour please?
>
> PHP Version 5.2.5 on XP
>
>
> $a = 1754.00 ;
> $b = 1754.03 ;
> $diff = abs($b) - abs($a);
>
> echo "$diff
"; //output: 0.03
>
> $a = 1754.00 ;
> $b = 1754.09 ;
> $diff = abs($b) - abs($a);
>
> echo "$diff
"; //output :0.0899999999999
>
> $a = 1754.01 ;
> $b = 1754.02 ;
> $diff = abs($b) - abs($a);
>
> echo $diff; //output: 0.00999999999999
>
>
> Is this expected behaviour? Can anybody reproduce this?
>
> Many thanks,
>
>
> Andy
>
> --
> PHP Database Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php