Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 23:30 26 Nov 2024 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Should I fly to the moon using the cmm2 as flight computer?

     Page 1 of 3    
Author Message
andreas

Senior Member

Joined: 07/12/2020
Location: Germany
Posts: 207
Posted: 04:21pm 31 Dec 2023
Copy link to clipboard 
Print this post

There is a funny result when calculating trigonometric functions using the cmm2. The results are not only not zero as expected but they differ when doing the calculation either within a for..next loop or step by step:


print "within For .. Next"
print
for i = 18 to 20
 print "i=";i,"cos(pi/2)*10^i";cos(pi/2)*10^i
next i
print
print "as separat statements"
print
print "print cos(pi/2)*10^18 =",cos(pi/2)*10^18
print "print cos(pi/2)*10^19 =",cos(pi/2)*10^19
print "print cos(pi/2)*10^20 =",cos(pi/2)*10^20
print
print "cos(pi/2) should be 0 anyway"






-andreas

.. and a happy new year!
Edited 2024-01-01 02:31 by andreas
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 6792
Posted: 05:02pm 31 Dec 2023
Copy link to clipboard 
Print this post

I get cos(pi/2) to be 6.093216209e-17 on a PicoMite VGA running 5.0707. It's pretty small. :)

Happy New Year!
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 9116
Posted: 05:12pm 31 Dec 2023
Copy link to clipboard 
Print this post

double precision math has about 16 digits of precision so all you are doing is exposing the limits of that precision by scaling the error component - nothing interesting or wrong
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 3802
Posted: 05:25pm 31 Dec 2023
Copy link to clipboard 
Print this post

Why are the results for i = 19 and an explicit 19 not the same?
(And similarly for 20?)

John
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 9116
Posted: 05:43pm 31 Dec 2023
Copy link to clipboard 
Print this post

Because it has added 1.0 each time and 3.0 cannot be exactly represented (amongst others)
 
andreas

Senior Member

Joined: 07/12/2020
Location: Germany
Posts: 207
Posted: 06:29pm 31 Dec 2023
Copy link to clipboard 
Print this post

I could imagine to use a "preprocessor" which replaces expressions like 'cos(pi/2)' or 'tan(pi)' by "0" syntactically before evaluating the expression mathematically.

-andreas

small is beautiful!
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 6792
Posted: 06:43pm 31 Dec 2023
Copy link to clipboard 
Print this post

OTOH you could simply use zero rather than expression to get it. :)
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6098
Posted: 08:01pm 31 Dec 2023
Copy link to clipboard 
Print this post

The for loop is using floats because that is the default.
Try adding
OPTION DEFAULT integer

and the results for the loop and individual are the same.

Likewise, if you leave the for loop as floats and change the 18 19 and 20 to 18.0 19.0 20.0 the results are both derived from floats.

Jim
VK7JH
MMedit   MMBasic Help
 
lizby
Guru

Joined: 17/05/2016
Location: United States
Posts: 3150
Posted: 09:47pm 31 Dec 2023
Copy link to clipboard 
Print this post

  TassyJim said  The for loop is using floats because that is the default.


Good point. You can also add DIM INTEGER i and get identical results.
PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed
 
phil99

Guru

Joined: 11/02/2018
Location: Australia
Posts: 2135
Posted: 10:19pm 31 Dec 2023
Copy link to clipboard 
Print this post

They did fly to the moon with a flight computer far less capable than the CMM2.
Trying to achieve 20 digit accuracy in a real world where nothing is precise is of no value. The key is repeated measurement, feedback and correction.
 
andreas

Senior Member

Joined: 07/12/2020
Location: Germany
Posts: 207
Posted: 10:20pm 31 Dec 2023
Copy link to clipboard 
Print this post

  Mixtel90 said  OTOH you could simply use zero rather than expression to get it. :)


When I could identify it - yes!
But in real world problems the arguments are variables and it is not obvious how they are calculated. there may be very large or very small values or pi/2 ;-)



That means to solve a problem correctly one does not only have to know the right formula but also a save way to compute it with an calculator which has only discrete states (limited number of bits within mantissa and exponent) and may be faulty if very large or very small values occur during computation which can not be represented within the calculating machine. When there are loops, small errors may generate large errors.

-andreas
 
Mixtel90

Guru

Joined: 05/10/2019
Location: United Kingdom
Posts: 6792
Posted: 10:35pm 31 Dec 2023
Copy link to clipboard 
Print this post

I don't see how you can get round that, apart from forcing high and low limits for the variable in your program. Multiplying the error doesn't help. :(

Or do everything in multiples of integers. Don't use floating point more than you absolutely have to. This used to be the standard way to avoid errors currency calculations, I believe.
Edited 2024-01-01 08:49 by Mixtel90
Mick

Zilog Inside! nascom.info for Nascom & Gemini
Preliminary MMBasic docs & my PCB designs
 
andreas

Senior Member

Joined: 07/12/2020
Location: Germany
Posts: 207
Posted: 10:49pm 31 Dec 2023
Copy link to clipboard 
Print this post

  Mixtel90 said  I don't see how you can get round that, apart from forcing high and low limits for the variable in your program. Multiplying the error doesn't help. :(

Or do everything in multiples of integers. Take the floating point out.


Yes, one can learn from FORTH programming language that integer calculation is superb in regard of precision.

example for two different ways to calculate a result:

 2 * 3 * 4 * 5 * 6                                           720
----------------------- = 2 (if you see it) or you calculate ----- = 2
   3 * 4 * 5 * 6                                             360


By first calculating 3/3 = 1 and 4/4 = 1 and 5/5 = 1 and 6/6 = 1 there is only 2 left as factor different from 1 on the left side. On the right side you possibly have to compute very large numbers and then to divide them which is more error prone.

-andreas
 
zeitfest
Guru

Joined: 31/07/2019
Location: Australia
Posts: 482
Posted: 11:01pm 31 Dec 2023
Copy link to clipboard 
Print this post

(deleted post   )
Edited 2024-01-01 09:09 by zeitfest
 
EDNEDN
Senior Member

Joined: 18/02/2023
Location: United States
Posts: 118
Posted: 02:13am 01 Jan 2024
Copy link to clipboard 
Print this post

So....    How many digits of precision do we need to accurately plot a course to the moon and establish orbit?    I would think 15 digits of precision should be enough?

Of course you would want to structure the calculations to not lose any precision, but certainly 15 digits of precision can get you to the moon????
Edited 2024-01-01 12:15 by EDNEDN
 
PhenixRising
Guru

Joined: 07/11/2023
Location: United Kingdom
Posts: 859
Posted: 02:33am 01 Jan 2024
Copy link to clipboard 
Print this post

  phil99 said  They did fly to the moon


But then they "lost the technology"

As one does.

Maybe the CMM2 is the new solution  
 
EDNEDN
Senior Member

Joined: 18/02/2023
Location: United States
Posts: 118
Posted: 03:27am 01 Jan 2024
Copy link to clipboard 
Print this post

  PhenixRising said  
But then they "lost the technology"



Besides losing all the technology...   They lost all of the videos and pictures.

It kind of sounds like "The dog ate my homework." excuse to me.
 
PhenixRising
Guru

Joined: 07/11/2023
Location: United Kingdom
Posts: 859
Posted: 05:34am 01 Jan 2024
Copy link to clipboard 
Print this post

"Hey did anyone grab the how-to-go-to-the-moon file-folder off-of Fred's desk? It was there just this morning"

 
Bryan1

Guru

Joined: 22/02/2006
Location: Australia
Posts: 1344
Posted: 06:25am 01 Jan 2024
Copy link to clipboard 
Print this post

yea tell me about it afterall all I'm looking from my CMM2 is one small step then off to the moon this cnc will go
 
swoodgate

Newbie

Joined: 24/11/2011
Location: Australia
Posts: 14
Posted: 06:28am 01 Jan 2024
Copy link to clipboard 
Print this post

  andreas said  There is a funny result when calculating trigonometric functions using the cmm2. The results are not only not zero as expected but they differ when doing the calculation either within a for..next loop or step by step:


Check out Arbitrary-precision arithmetic libraries for solutions to this problem.

Steve
 
     Page 1 of 3    
Print this page
© JAQ Software 2024