Page 1 of 1
Conversions from JS to PHP (Inconsistencies?)
Posted: Wed Apr 27, 2005 4:13 pm
by Chris Corbyn
Hi,
I'm converting a >1000 line (needlessley, this guy has constructed a complete jungle of mess) JS script which is highly mathematical into PHP.
The script goes through lots of floor() and round() operations.
I have only got so far through but it appears that I have either:
a) Missed something out between one function and another, or
b) The two languages differ slightly mathematically
I have a difference of a tiny amount (like 0.003 or something in a number in the 100's of thousands), but it's still bugging me before I progress any further as to whether I've missed something.
I wont post the code, you will suffer if I do
What I'm asking is, is it possible that the difference is not my error but just the way the two languages compute (it's only in the thousands of an integer)? I honestly believed that mathematics should be 100% accurate with any language.
I can't afford to carry on if I need to iron out a bug at this stage, the code I'm reading from really is a complete and utter jungle.
Any opinions?
Cheers

Posted: Wed Apr 27, 2005 4:17 pm
by Todd_Z
I have no idea as to your question, but heres an idea to help you...
Debug ever 10 lines or so, and check to see if the numbers are the same at every stage.
Then you can see exactly which 10 line chunk is screwed up, it may be more of a simple issue than you think.
Posted: Wed Apr 27, 2005 4:56 pm
by timvw
i got bad news for you
as soon as you start using floating numbers (decimals) things become complicated because there is a minimal loss of precision (while converting between binary and decimal and the other way round)
it's explained in most basic courses on computer architecture i think. you can read (a little) at
http://support.microsoft.com/kb/q214118/. probably there are more/better sites out there that explain it...
Posted: Wed Apr 27, 2005 5:14 pm
by Chris Corbyn
It's fine if it's normal.... I just wanted to make sure I hadn't screwed up. I can afford to lose of a few thousands or whatever when I'm working with figures this large.
Here's the actual figures.... we're actually in the Millions so you know... this is very insignificant... but it would have been a big deal if both numbers were supposed to be 100% identical.
Data:
Code: Select all
My PHP Output: 2453487.79167
JavaScript Ouput: 2453487.8333
Difference: 0.04163
Error: 1.6967e-6 %
So it's not gonna matter
Thanks for the info guys!
Posted: Thu Apr 28, 2005 4:38 am
by Chris Corbyn
Oh dear....
I'm getting further and futher through this conversion and my errors are growing.
I have to perform deg2rad() and rad2deg() at several points.... this is obviously causing problems beacuse small differences are more significant when working with radians as opposed to degrees.
I'm not too sure why they round() values midway through anyway

I'm gonna take them all out and just round() the final figures.
I didn't realise the differences like this.... it's pretty interesting but all the same... very incovenient. Although what's to say that the JS values are any "better" than my PHP ones?
EDIT | Ignore that comment, it might have helped if I had been using the same values I was testing against.
Still a bigger error but still less than 1%
Posted: Thu Apr 28, 2005 4:53 am
by malcolmboston
to be honest, the reason your maths are incorrect is because you keep floor and ceil'ing before the end
only round the number when all the calculations are finished
Posted: Thu Apr 28, 2005 7:14 am
by Chris Corbyn
The floor()'ing and round()'ing were (pretty much) because I'm working with units of time.
The original script that I didn't write was written with lots of round()'ing midway through calculations. I'm tidying that mess up too

Posted: Thu Apr 28, 2005 8:07 am
by onion2k
I'd work out what the script is supposed to do and write one from scratch to achieve that goal. It's better that way.
Posted: Thu Apr 28, 2005 6:28 pm
by Chris Corbyn
onion2k wrote:I'd work out what the script is supposed to do and write one from scratch to achieve that goal. It's better that way.
LOL... I would have done too if I was a mathematician / physicist
It was an extremely mathematical script, jumping around between loads of functions etc for calculating the position of the sun in the sky given a variety of factors. This isn't basic spherical calculations neither... the world is not a sphere, it's orbit is not circular, the sun is not merely a point in the sky etc etc.... It's incredibly accurate too (it needed to be).
I have it working now anyway (and about 20% of the size of the JS jungle I have taken the algorithm from).
Thanks for all your help guys
