Conversions from JS to PHP (Inconsistencies?)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Conversions from JS to PHP (Inconsistencies?)

Post 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 :lol:

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 :-)
User avatar
Todd_Z
Forum Regular
Posts: 708
Joined: Thu Nov 25, 2004 9:53 pm
Location: U Michigan

Post 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.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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...
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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 :-D

Thanks for the info guys!
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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. :oops: Still a bigger error but still less than 1%
Last edited by Chris Corbyn on Thu Apr 28, 2005 4:53 am, edited 1 time in total.
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post 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
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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 ;-)
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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 :lol:

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 :D
Post Reply