Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.sys.acorn.programmer > #5691 > unrolled thread

Sine/Cosine/Tangent accuracy error

Started byRichard Ashbery <basura@invalid.addr.uk>
First post2019-02-23 16:28 +0000
Last post2019-02-27 20:27 +0000
Articles 11 — 5 participants

Back to article view | Back to comp.sys.acorn.programmer


Contents

  Sine/Cosine/Tangent accuracy error Richard Ashbery <basura@invalid.addr.uk> - 2019-02-23 16:28 +0000
    Re: Sine/Cosine/Tangent accuracy error Steve Fryatt <news@stevefryatt.org.uk> - 2019-02-23 17:55 +0000
      Re: Sine/Cosine/Tangent accuracy error Richard Ashbery <basura@invalid.addr.uk> - 2019-02-23 20:53 +0000
        Re: Sine/Cosine/Tangent accuracy error Steve Fryatt <news@stevefryatt.org.uk> - 2019-02-24 08:25 +0000
          Re: Sine/Cosine/Tangent accuracy error Richard Ashbery <basura@invalid.addr.uk> - 2019-02-24 12:20 +0000
    Re: Sine/Cosine/Tangent accuracy error druck <news@druck.org.uk> - 2019-02-24 23:35 +0000
      Re: Sine/Cosine/Tangent accuracy error Richard Ashbery <basura@invalid.addr.uk> - 2019-02-25 17:34 +0000
        Re: Sine/Cosine/Tangent accuracy error Martin Wuerthner <spamtrap@mw-software.com> - 2019-02-26 17:17 +0100
          Re: Sine/Cosine/Tangent accuracy error jgh@mdfs.net - 2019-02-26 12:46 -0800
          Re: Sine/Cosine/Tangent accuracy error Richard Ashbery <basura@invalid.addr.uk> - 2019-02-27 16:37 +0000
          Re: Sine/Cosine/Tangent accuracy error druck <news@druck.org.uk> - 2019-02-27 20:27 +0000

#5691 — Sine/Cosine/Tangent accuracy error

FromRichard Ashbery <basura@invalid.addr.uk>
Date2019-02-23 16:28 +0000
SubjectSine/Cosine/Tangent accuracy error
Message-ID<578aeb07c9basura@invalid.addr.uk>
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

[toc] | [next] | [standalone]


#5692

FromSteve Fryatt <news@stevefryatt.org.uk>
Date2019-02-23 17:55 +0000
Message-ID<mpro.pne4h303a7pf301yy.news@stevefryatt.org.uk>
In reply to#5691
On 23 Feb, Richard Ashbery wrote in message
    <578aeb07c9basura@invalid.addr.uk>:

> 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/

[toc] | [prev] | [next] | [standalone]


#5693

FromRichard Ashbery <basura@invalid.addr.uk>
Date2019-02-23 20:53 +0000
Message-ID<578b034766basura@invalid.addr.uk>
In reply to#5692
In article <mpro.pne4h303a7pf301yy.news@stevefryatt.org.uk>, Steve
Fryatt <news@stevefryatt.org.uk> wrote:
> On 23 Feb, Richard Ashbery wrote in message
>     <578aeb07c9basura@invalid.addr.uk>:

> > 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

[toc] | [prev] | [next] | [standalone]


#5694

FromSteve Fryatt <news@stevefryatt.org.uk>
Date2019-02-24 08:25 +0000
Message-ID<mpro.pnf8q0015djaa01hl.news@stevefryatt.org.uk>
In reply to#5693
On 23 Feb, Richard Ashbery wrote in message
    <578b034766basura@invalid.addr.uk>:

> 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/

[toc] | [prev] | [next] | [standalone]


#5696

FromRichard Ashbery <basura@invalid.addr.uk>
Date2019-02-24 12:20 +0000
Message-ID<578b5821dfbasura@invalid.addr.uk>
In reply to#5694
In article <mpro.pnf8q0015djaa01hl.news@stevefryatt.org.uk>, Steve
Fryatt <news@stevefryatt.org.uk> wrote:
> On 23 Feb, Richard Ashbery wrote in message
>     <578b034766basura@invalid.addr.uk>:

> > 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

[toc] | [prev] | [next] | [standalone]


#5695

Fromdruck <news@druck.org.uk>
Date2019-02-24 23:35 +0000
Message-ID<q4v9ot$qe5$1@dont-email.me>
In reply to#5691
On 23/02/2019 16:28, Richard Ashbery wrote:
> 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

[toc] | [prev] | [next] | [standalone]


#5697

FromRichard Ashbery <basura@invalid.addr.uk>
Date2019-02-25 17:34 +0000
Message-ID<578bf8b556basura@invalid.addr.uk>
In reply to#5695
In article <q4v9ot$qe5$1@dont-email.me>, druck <news@druck.org.uk>
wrote:
> On 23/02/2019 16:28, Richard Ashbery wrote:
> > 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

[toc] | [prev] | [next] | [standalone]


#5698

FromMartin Wuerthner <spamtrap@mw-software.com>
Date2019-02-26 17:17 +0100
Message-ID<mpro.pnjjww01l45o706b4.spamtrap@mw-software.com>
In reply to#5697
Richard Ashbery <basura@invalid.addr.uk> wrote:

> In article <q4v9ot$qe5$1@dont-email.me>, druck <news@druck.org.uk> wrote:
> > On 23/02/2019 16:28, Richard Ashbery wrote:
> > > 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 --------

[toc] | [prev] | [next] | [standalone]


#5699

Fromjgh@mdfs.net
Date2019-02-26 12:46 -0800
Message-ID<8be97237-0839-4e8d-bdc4-9fe89fa1b418@googlegroups.com>
In reply to#5698
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

[toc] | [prev] | [next] | [standalone]


#5700

FromRichard Ashbery <basura@invalid.addr.uk>
Date2019-02-27 16:37 +0000
Message-ID<578cfb2430basura@invalid.addr.uk>
In reply to#5698
In article <mpro.pnjjww01l45o706b4.spamtrap@mw-software.com>,
Martin
Wuerthner <spamtrap@mw-software.com> wrote:
> Richard Ashbery <basura@invalid.addr.uk> wrote:

> > In article <q4v9ot$qe5$1@dont-email.me>, druck
> > <news@druck.org.uk> wrote:
> > > On 23/02/2019 16:28, Richard Ashbery wrote:
> > > > 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.

> > 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

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

[toc] | [prev] | [next] | [standalone]


#5701

Fromdruck <news@druck.org.uk>
Date2019-02-27 20:27 +0000
Message-ID<c42f108d57.druck@druck.org.uk>
In reply to#5698
On 26 Feb 2019 Martin Wuerthner <spamtrap@mw-software.com> wrote:
>> In article <q4v9ot$qe5$1@dont-email.me>, druck <news@druck.org.uk> wrote:
>>> 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/

[toc] | [prev] | [standalone]


Back to top | Article view | comp.sys.acorn.programmer


csiph-web