arithmetic overflow

Anything QL Software or Programming Related.
Post Reply
swensont
Forum Moderator
Posts: 324
Joined: Tue Dec 06, 2011 3:30 am
Location: SF Bay Area
Contact:

arithmetic overflow

Post by swensont »

I'm working on a program that will determine the location of the 4 major moons of Jupiter. On the QL, when I run the QL, I get to a point and it does with "arithmetic overflow". I've tracked down the issue to the COS function not liking a large number. To get the error try:

COS(3000000) (that's 3 million).

COS(300000) will work (that's 5 zeros).

I've seen this error with both SMSQmulator and Q-emualtor.

I've run the same code on a ZX81 (using sz81) and it works just fine. I've tried the COS function above on the ZX81, T/S 2068 and the Spectrum (using emulators) and get no error.

So, why can the ZX81 handle COS(3000000) but not the QL?

Tim


User avatar
tofro
Font of All Knowledge
Posts: 3093
Joined: Sun Feb 13, 2011 10:53 pm
Location: SW Germany

Re: arithmetic overflow

Post by tofro »

swensont wrote: So, why can the ZX81 handle COS(3000000) but not the QL?
Tim,
maybe because with such large numbers your accuracy goes haywire and your moons might be somewhere else?

Internally, the QL calculates cos using an approximation (Taylor series) that has to calculate
cos(x) = x -x^2/2 + x^4/24 +-.....

This goes overboard in terms of accuracy already at the first iterations for such large arguments. The "older" Sinclairs probably just don't care.

The QL's floating point is't very good at all and doesn't get any better at extreme ranges. If you find the need to calculate sin and cos for such extreme angles that implies something else with your program might not be optimal - After all,

Code: Select all

cos (x) = cos (n  / (2 * PI) * x)
because of the symmetry of the cos function, which is much easier to handle for good old QL ;) Just pick a suitable n.

Tobias


ʎɐqǝ ɯoɹɟ ǝq oʇ ƃuᴉoƃ ʇou sᴉ pɹɐoqʎǝʞ ʇxǝu ʎɯ 'ɹɐǝp ɥO
User avatar
pjw
QL Wafer Drive
Posts: 1608
Joined: Fri Jul 11, 2014 8:44 am
Location: Norway
Contact:

Re: arithmetic overflow

Post by pjw »

Quote from: "MG" ROM BUGS Simon Goodwin [] Copyright 1987 Simon N Goodwin; all rights reserved. Slightly revised January 2006 for the QDOS documentation CD. Expanded from an article written for the September 1987 issue of QL World magazine.
[T]he QL trigonometric package gives silly results for COS between 16384*PI radians (about three million degrees) and 65535 radians. Greater values give an 'overflow' error. For a weird result, type:

PRINT COS(16384*PI)

CURE: don't do it. It's extremely unlikely that your program will fall foul of the bug unless it has gone haywire, in which case it will probably run into the overflow error. Three million degrees should be enough for anyone.

FIX: Should be easy to translate COS(x) to SIN(x+PI/2) when required; this doesn't increase the valid range much, so it might be as useful to trap such values as an 'overflow' - at least that avoids the 'silly' result.


Per
I love long walks, especially when they are taken by people who annoy me.
- Fred Allen
User avatar
vanpeebles
Commissario Pebbli
Posts: 2852
Joined: Sat Nov 20, 2010 7:13 pm
Location: North East UK

Re: arithmetic overflow

Post by vanpeebles »

Sounds like Tim is building a QL controlled space ship! :o


User avatar
Mr_Navigator
QL Fanatic
Posts: 782
Joined: Mon Dec 13, 2010 11:17 pm
Location: UK, Essex
Contact:

Re: arithmetic overflow

Post by Mr_Navigator »

Oohh first QL in space?


-----------------------------------------------------------------------------------
QLick here for the Back 2 the QL Blog http://backtotheql.blogspot.co.uk/
swensont
Forum Moderator
Posts: 324
Joined: Tue Dec 06, 2011 3:30 am
Location: SF Bay Area
Contact:

Re: arithmetic overflow

Post by swensont »

I don't think anything is going haywire in the code. The algorithm that I am using is from "Astronomical Formulae for Calculators" by Jean Meeus (4th edition 1988). Meuus is pretty much the standard on the subject.

I'll look into the alternatives listed above.

Tim


User avatar
pjw
QL Wafer Drive
Posts: 1608
Joined: Fri Jul 11, 2014 8:44 am
Location: Norway
Contact:

Re: arithmetic overflow

Post by pjw »

Tim, According to the documentation (both for QL and QPC2) the permitted range for COS is given as -10000..10000 radians.

Sine and cosine are periodic functions with period 2π. For angles < −2π or > +2π you could continue to rotate around the circle: cos(θ) = cos(θ + 2πk) for any angle θ and any integer k.

Per


Per
I love long walks, especially when they are taken by people who annoy me.
- Fred Allen
User avatar
tofro
Font of All Knowledge
Posts: 3093
Joined: Sun Feb 13, 2011 10:53 pm
Location: SW Germany

Re: arithmetic overflow

Post by tofro »

pjw wrote: Sine and cosine are periodic functions with period 2π. For angles < −2π or > +2π you could continue to rotate around the circle: cos(θ) = cos(θ + 2πk) for any angle θ and any integer k.
Right - unless you want to count revolutions with the angle where

rev = angle / 2 * π and
subAngle = angle FMOD (2 *π)
(Remainder in Floating point)

Tobias


ʎɐqǝ ɯoɹɟ ǝq oʇ ƃuᴉoƃ ʇou sᴉ pɹɐoqʎǝʞ ʇxǝu ʎɯ 'ɹɐǝp ɥO
swensont
Forum Moderator
Posts: 324
Joined: Tue Dec 06, 2011 3:30 am
Location: SF Bay Area
Contact:

Re: arithmetic overflow

Post by swensont »

Looks like the main issue that I was facing was that the formula was using degrees and the QL SIN() and COS() are in radians. I had to do some conversion back and forth. Also, the book mentioned converting all of the degrees into a range of 0-360 (which was easily done with MOD 360). The formula is now working fine on the QL.


Post Reply