[PHP] Mathematical differences?!

2003-09-18 Thread Mario Werner

Hello all,

I ported a algorithm from JavaScript to PHP and noticed that PHP outputs a
different result than JS. For example:


t   = 0.6255264658909423
f   = 20.5
ln  = -6.983
d2r = 0.017453292519943295

$ra = (((6.6460656 + 2400.0513 * $t + 2.58e-5 * $t * $t + $f) * 15 - $ln) %
360) * $d2r;

PHP outputs 4.4156830075456535
JS outputs 4.42304792511156


I found out that ((...) % 360) returns in PHP 253.... whereas JS and a
Calculator return 253.6085
I also tried to use the bc...-functions but the result was the same.

This formular is only a part of a bigger calculation and I also noticed
further differences because when
I manually set $ra to 4.4230... (the JS result) then I still get a different
end-result than in JS but it is
100% EXACTLY the same algorithm, I checked it many times.

How could that be?! 1:1 the same code but different outputs? Is this a
PHP-bug?  :-?

Kindly regards,

Mario

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] Mathematical differences?!

2003-09-18 Thread Jay Blanchard
[snip]
How could that be?! 1:1 the same code but different outputs? Is this a
PHP-bug?  :-?
[/snip]

*feelin' kinda' smart arsed today, if no one noticed*

Maybe it was a JavaScipt bug all along ? ;)

Actually I am worried about the order of operations and encapsulation in
the equation in $ra, mathematically speaking.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Mathematical differences?!

2003-09-18 Thread Curt Zirzow
* Thus wrote Mario Werner ([EMAIL PROTECTED]):
 
 I ported a algorithm from JavaScript to PHP and noticed that PHP outputs a
 different result than JS. For example:
 
 
 t   = 0.6255264658909423
 f   = 20.5
 ln  = -6.983
 d2r = 0.017453292519943295
 
 $ra = (((6.6460656 + 2400.0513 * $t + 2.58e-5 * $t * $t + $f) * 15 - $ln) %
 360) * $d2r;
 
 PHP outputs 4.4156830075456535
 JS outputs 4.42304792511156
 
 
 I found out that ((...) % 360) returns in PHP 253.... whereas JS and a
 Calculator return 253.6085
 I also tried to use the bc...-functions but the result was the same.
 [...]
 How could that be?! 1:1 the same code but different outputs? Is this a
 PHP-bug?  :-?

Nope.

http://bugs.php.net/bug.php?id=12623

fmod() will do what you want.

Curt
-- 
I used to think I was indecisive, but now I'm not so sure.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Mathematical differences?!

2003-09-18 Thread Dan Anderson
Without spending a lot of time looking at your problem I would speculate
this is the result of round off error.

What is roundoff error you ask?

Well the number 0.6255264658909423 does not designate
0.6255264658909423... but 0.6255264658909423 and some
fraction that could not be displayed because it is too large to be
stored in the number of bits used.

But the number of bits used per data type varies from system to system.

So you are seeing round off error.

If you don't understand e-mail me for a more complete explanation.  

-Dan

On Wed, 2003-09-17 at 15:54, Mario Werner wrote:
 Hello all,
 
 I ported a algorithm from JavaScript to PHP and noticed that PHP outputs a
 different result than JS. For example:
 
 
 t   = 0.6255264658909423
 f   = 20.5
 ln  = -6.983
 d2r = 0.017453292519943295
 
 $ra = (((6.6460656 + 2400.0513 * $t + 2.58e-5 * $t * $t + $f) * 15 - $ln) %
 360) * $d2r;
 
 PHP outputs 4.4156830075456535
 JS outputs 4.42304792511156
 
 
 I found out that ((...) % 360) returns in PHP 253.... whereas JS and a
 Calculator return 253.6085
 I also tried to use the bc...-functions but the result was the same.
 
 This formular is only a part of a bigger calculation and I also noticed
 further differences because when
 I manually set $ra to 4.4230... (the JS result) then I still get a different
 end-result than in JS but it is
 100% EXACTLY the same algorithm, I checked it many times.
 
 How could that be?! 1:1 the same code but different outputs? Is this a
 PHP-bug?  :-?
 
 Kindly regards,
 
 Mario

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php