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
arithmetic overflow
Re: arithmetic overflow
Tim,swensont wrote: So, why can the ZX81 handle COS(3000000) but not the QL?
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)

Tobias
ʎɐqǝ ɯoɹɟ ǝq oʇ ƃuᴉoƃ ʇou sᴉ pɹɐoqʎǝʞ ʇxǝu ʎɯ 'ɹɐǝp ɥO
Re: arithmetic overflow
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
I love long walks, especially when they are taken by people who annoy me.
- Fred Allen
- vanpeebles
- Commissario Pebbli
- Posts: 2852
- Joined: Sat Nov 20, 2010 7:13 pm
- Location: North East UK
- Mr_Navigator
- QL Fanatic
- Posts: 782
- Joined: Mon Dec 13, 2010 11:17 pm
- Location: UK, Essex
- Contact:
Re: arithmetic overflow
Oohh first QL in space?
-----------------------------------------------------------------------------------
QLick here for the Back 2 the QL Blog http://backtotheql.blogspot.co.uk/
QLick here for the Back 2 the QL Blog http://backtotheql.blogspot.co.uk/
Re: arithmetic overflow
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
I'll look into the alternatives listed above.
Tim
Re: arithmetic overflow
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
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
I love long walks, especially when they are taken by people who annoy me.
- Fred Allen
Re: arithmetic overflow
Right - unless you want to count revolutions with the angle wherepjw 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.
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
Re: arithmetic overflow
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.