Discussion:
Sine/Cosine/Tangent accuracy error
(too old to reply)
Richard Ashbery
2019-02-23 16:28:55 UTC
Permalink
I have a very old but interesting BASIC graphic program where I've
made some minor changes to operate on a 1920 x 1080 res monitor.

After program is run for some minutes it errors with "Accuracy lost in
Sine/Cosine/Tangent".

How do I stop this error from appearing?

Program is run from SED running RISC OS 5.24 (16-Apr-18) on
Raspberry Pi.

Richard
Steve Fryatt
2019-02-23 17:55:57 UTC
Permalink
On 23 Feb, Richard Ashbery wrote in message
I have a very old but interesting BASIC graphic program where I've made
some minor changes to operate on a 1920 x 1080 res monitor.
After program is run for some minutes it errors with "Accuracy lost in
Sine/Cosine/Tangent".
How do I stop this error from appearing?
The error indicates that the input to SIN(), COS() or TAN() has fallen
outside the range +/-8388608 radians (in BASIC V). You probably need to
check the algorithm used, to make sure that it doesn't cause this to happen.
--
Steve Fryatt - Leeds, England

http://www.stevefryatt.org.uk/
Richard Ashbery
2019-02-23 20:53:46 UTC
Permalink
Post by Steve Fryatt
On 23 Feb, Richard Ashbery wrote in message
Post by Richard Ashbery
I have a very old but interesting BASIC graphic program where
I've made some minor changes to operate on a 1920 x 1080 res
monitor.
After program is run for some minutes it errors with "Accuracy
lost in Sine/Cosine/Tangent".
How do I stop this error from appearing?
The error indicates that the input to SIN(), COS() or TAN() has
fallen outside the range +/-8388608 radians (in BASIC V). You
probably need to check the algorithm used, to make sure that it
doesn't cause this to happen.
Many thanks Steve for your reply. I couldn't see the "wood for the
trees". I've checked it again and sure enough it exceeds the radians
limit of BASIC. The problem I now have is to edit the code without
stalling the graphic animation but there must be a way.

All the best

Richard
Steve Fryatt
2019-02-24 08:25:17 UTC
Permalink
On 23 Feb, Richard Ashbery wrote in message
I've checked it again and sure enough it exceeds the radians limit of
BASIC. The problem I now have is to edit the code without stalling the
graphic animation but there must be a way.
Angles wrap around at 2*PI radians, so if you're getting into the millions
of radians, you're probably doing something wrong in the approach to working
out the numbers. Without knowing what you're doing, it's hard to be more
specific, unfortunately.
--
Steve Fryatt - Leeds, England

http://www.stevefryatt.org.uk/
Richard Ashbery
2019-02-24 12:20:36 UTC
Permalink
Post by Steve Fryatt
On 23 Feb, Richard Ashbery wrote in message
Post by Richard Ashbery
I've checked it again and sure enough it exceeds the radians
limit of BASIC. The problem I now have is to edit the code
without stalling the graphic animation but there must be a way.
Angles wrap around at 2*PI radians, so if you're getting into the
millions of radians, you're probably doing something wrong in the
approach to working out the numbers. Without knowing what you're
doing, it's hard to be more specific, unfortunately.
Hi Steve

Many thanks for coming back. In the rush to animate I missed the line
to terminate the number of radians. If I fail to get any further I'll
post the program.

Richard
druck
2019-02-24 23:35:54 UTC
Permalink
Post by Richard Ashbery
I have a very old but interesting BASIC graphic program where I've
made some minor changes to operate on a 1920 x 1080 res monitor.
After program is run for some minutes it errors with "Accuracy lost in
Sine/Cosine/Tangent".
How do I stop this error from appearing?
If you are continually incrementing an angle which gets used in
SIN/COS/TAN, take a modulus to prevent it getting to big.

e.g. If you are incrementing angle variable 'a' degrees

a = (a + 1) MOD 360

Or for radians.

a = (a + 0.01) MOD (2*PI)

---druck
Richard Ashbery
2019-02-25 17:34:31 UTC
Permalink
Post by druck
Post by Richard Ashbery
I have a very old but interesting BASIC graphic program where
I've made some minor changes to operate on a 1920 x 1080 res
monitor.
After program is run for some minutes it errors with "Accuracy
lost in Sine/Cosine/Tangent".
How do I stop this error from appearing?
If you are continually incrementing an angle which gets used in
SIN/COS/TAN, take a modulus to prevent it getting to big.
e.g. If you are incrementing angle variable 'a' degrees
a = (a + 1) MOD 360
Or for radians.
a = (a + 0.01) MOD (2*PI)
I've submitted the program for your perusal. The idea is quite simple
but I can't get my head round the programming :-(

REM - Tuttle: A pattern-drawing screen turtle
REM Refer to pages 29-33 in BBC Micro Programs in BASIC
REM original by Derrick Daines.
REM S = # of sides, D = # of sides before breakaway,
REM I = Breakaway angle, SIZE = pattern diameter
MODE 1920,1080,32 : OFF
X=1920:Y=1080
S=8 : D=7 : I=3 : SIZE=300
B=I*2*PI/S:MOVE X,Y
REPEAT
FOR TURN=1 TO S
FOR SIDE=1 TO D
A=SIDE*2*PI/S:A=A+B
PRINT TAB(10,10)A
X1=SIZE*COS A:Y1=SIZE*SIN A
PLOT1,X1,Y1
NEXT SIDE
B=B+I*2*PI/S
NEXT TURN
B=B+I*2*PI/S:MOVE X,Y
GCOL RND(63)
UNTIL0

My understanding:

Program creates a partial octagon i.e. only 7 sides are drawn. The
finish of the last octagonal line (the breakaway point) becomes the
start position for the next partial octagon to create a simple
pattern. At the next 'TURN' the pattern is repeated but offset further
from the screen centre. If the program is permitted to run for a
longish period some fascinating effects evolve which cycle c/w and
cc/w.

I can't see a way of easily using a=(a+0.01)MOD(2*PI) without
upsetting the subtle patterning. If you get time have a look at it and
see if you can come up with a solution guys. I would welcome other
improvements.

Richard
Martin Wuerthner
2019-02-26 16:17:20 UTC
Permalink
Post by druck
Post by Richard Ashbery
I have a very old but interesting BASIC graphic program where I've
made some minor changes to operate on a 1920 x 1080 res monitor.
After program is run for some minutes it errors with "Accuracy lost in
Sine/Cosine/Tangent".
How do I stop this error from appearing?
If you are continually incrementing an angle which gets used in
SIN/COS/TAN, take a modulus to prevent it getting to big.
e.g. If you are incrementing angle variable 'a' degrees
a = (a + 1) MOD 360
Or for radians.
a = (a + 0.01) MOD (2*PI)
Not such a good idea for radians, since MOD is an integer operator, so it
will round its argument to a whole number and return a whole number.
I've submitted the program for your perusal. The idea is quite simple but
I can't get my head round the programming :-(
[...]
A=SIDE*2*PI/S:A=A+B
PRINT TAB(10,10)A
X1=SIZE*COS A:Y1=SIZE*SIN A
Instead of the last line, use:

REM range reduce the argument
RR = A - INT(A / (2 * PI)) * 2 * PI
X1=SIZE*COS RR:Y1=SIZE*SIN RR
--
Martin Wuerthner MW Software http://www.mw-software.com/

------- RISC OS Software for Design, Printing and Publishing --------
j***@mdfs.net
2019-02-26 20:46:24 UTC
Permalink
If you're changing an angle it's often sufficient to do
something like:
angle=angle+update
IF angle>2*PI THEN angle=angle-2*PI
IF angle<-2*PI THEN angle=angle+2*PI

Of course you need to be careful about runaway rounding.
Maybe have something like
IF ABS angle<1E-18 THEN angle=0

depending on how accurate you want to keep the numbers.
1E-18 is half the resolution that a 5-byte float can
store, being 0.000000000000000001
Richard Ashbery
2019-02-27 16:37:20 UTC
Permalink
In article <***@mw-software.com>,
Martin
Post by Martin Wuerthner
Post by Richard Ashbery
Post by druck
Post by Richard Ashbery
I have a very old but interesting BASIC graphic program where
I've made some minor changes to operate on a 1920 x 1080 res
monitor.
After program is run for some minutes it errors with
"Accuracy lost in Sine/Cosine/Tangent".
How do I stop this error from appearing?
If you are continually incrementing an angle which gets used in
SIN/COS/TAN, take a modulus to prevent it getting to big.
Or for radians.
a = (a + 0.01) MOD (2*PI)
Not such a good idea for radians, since MOD is an integer operator,
so it will round its argument to a whole number and return a whole
number.
Post by Richard Ashbery
I've submitted the program for your perusal. The idea is quite
simple but I can't get my head round the programming :-( [...]
A=SIDE*2*PI/S:A=A+B PRINT TAB(10,10)A X1=SIZE*COS
A:Y1=SIZE*SIN A
REM range reduce the argument RR = A - INT(A / (2 * PI)) * 2
* PI X1=SIZE*COS RR:Y1=SIZE*SIN RR
Hi Martin

Restricting the radians using your example works perfectly.

Changing the parameters create some interesting patterns. An ingenious
program created all those years ago but without animation due to the
hardware limitations of the BBC Micro. Runs very fast on Raspberry Pi.

Anyone fancy a go at incorporating it into a screen saver?

Many thanks

Richard
druck
2019-02-27 20:27:10 UTC
Permalink
Post by Martin Wuerthner
Post by druck
If you are continually incrementing an angle which gets used in
SIN/COS/TAN, take a modulus to prevent it getting to big.
e.g. If you are incrementing angle variable 'a' degrees
a = (a + 1) MOD 360
Or for radians.
a = (a + 0.01) MOD (2*PI)
Not such a good idea for radians, since MOD is an integer operator, so it
will round its argument to a whole number and return a whole number.
Excellent point. I'm getting really rusty on BBC Basic now.

---druck
--
The ARM Club Free Software - http://www.armclub.org.uk/free/
32 bit Conversions Page - http://www.armclub.org.uk/32bit/
Loading...