|
Posted by David T. Ashley on 12/22/06 16:42
<kkmigas@gmail.com> wrote in message
news:1166719766.801640.296050@42g2000cwt.googlegroups.com...
> Can some one explain if this can be fixed using php.ini settings ?
>
> echo "round 20.545 -".round(20.545,2)."<br>";
> echo "round 20.555 -".round(20.555,2)."<br>";
> echo "number_format 20.545 -".number_format(20.545, 2, ',',
> '.')."<br>";
> echo "number_format 20.555 -".number_format(20.555, 2, ',',
> '.')."<br>";
>
> PHP Version 4.3.0 / FreeBSD
> round 20.545 -20.55
> round 20.555 -20.56
> number_format 20.545 -20,55
> number_format 20.555 -20,55
>
> PHP Version 4.4.4 / CENTOS
> round 20.545 -20.55
> round 20.555 -20.55
> number_format 20.545 -20,55
> number_format 20.555 -20,55
>
> PHP Version 5.1.4 / Windows NT
> round 20.545 -20.55
> round 20.555 -20.56
> number_format 20.545 -20,55
> number_format 20.555 -20,56
My only observation about the examples you chose is that the fractional
parts are infinitely repeating "radiximals" in binary.
0.545 = 545/1000 = 109/200 (irreducible)
0.555 = 555/1000 = 111/200 (irreducible)
Both of the numbers above can't be expressed as precise binary floating
point numbers because the denominator after reduction has prime factors that
are not "2".
PHP.INI may be a factor, but it may also be that something at a lower level
is occurring (i.e. machine arithmetic, how certain rounding settings on
floating-point processors or libraries are set).
In other words, you've chosen antagonistic examples.
Why don't you try 7/8 (0.875), 15/16 (0.9275), or 31/32 (0.96875) as the
fractional part of the numbers and see if you can get the same behavior.
I'm guessing that you may not.
Any fraction with a reasonable denomiator (< 2^16) that is a power of 2
would be a reasonable test case.
Note that these are all exactly representable by a typical machine.
Just a guess.
Navigation:
[Reply to this message]
|