View previous topic :: View next topic |
Author |
Message |
T0ni0
Joined: 21 Jan 2005 Posts: 11
|
error float decimal with sin() cos() |
Posted: Wed Jul 20, 2005 10:14 am |
|
|
Hi i need do this formule:
Code: | return &acos(cos($a1)*cos($b1)*cos($a2)*cos($b2) + cos($a1)*sin($b1)*cos($a2)*sin($b2) + sin($a1)*sin($a2)) * $r; |
but i look error at 7 decimal of float after make sin() and cos() ¿?
i need accurasy calcul, but i compare with a calculator and i look bad results at c5,c6,c7
PIC 18F6720
PCH 3.227
Code: | dlong=6378; //radio tierra
c1=latitud * degtorad; //a1
c2=longitud * degtorad; //b1
c3=arealat * degtorad; //a2
c4=arealong * degtorad; //b2
c5= cos(c1)*cos(c2)*cos(c3)*cos(c4);
c6= cos(c1)*sin(c2)* cos(c3)*sin(c4);
c7= sin(c1)*sin(c3);
distan = c5+c6+c7;
distan = acos(distan);
distan = distan * dlong; |
Thx for advance, and sorry for my bad explanation in english |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Jul 20, 2005 11:08 am |
|
|
Quote: | but i look error at 7 decimal of float after make sin() and cos() ¿? |
Here's a web page that discusses Microchip floating point.
http://www.bknd.com/cc5x/math.shtml
It says:
Code: |
Format Resolution
32 bit 7.2 digits |
The floating point format used by CCS doesn't have as much
resolution as the format used by MSVC++. |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Wed Jul 20, 2005 11:32 am |
|
|
As a general principle, if you want accuracy stay away from floating point! _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
carlosma
Joined: 24 Mar 2004 Posts: 53 Location: Portugal
|
|
Posted: Wed Jul 20, 2005 4:54 pm |
|
|
Hello
already I had a problem as this, when I had to calculate tan() and acos(). I decided with the following Post:
[url]
http://www.ccsinfo.com/forum/viewtopic.php?t=22405&highlight=
[/url]
important to simple mathematics expression
more RAM and later 18F452.
Do you use this for a Solar tracking? |
|
|
T0ni0
Joined: 21 Jan 2005 Posts: 11
|
|
Posted: Thu Jul 21, 2005 12:45 am |
|
|
SherpaDoug wrote: | As a general principle, if you want accuracy stay away from floating point! |
Thx, but i need make Sin() Cos() radian, how i can do without float? |
|
|
Ttelmah Guest
|
|
Posted: Thu Jul 21, 2005 3:47 am |
|
|
Unfortunately, you can't. This however does not mean you have to use the standard library.
In the past with a similar problem, I decided that I needed accuracy for a full circle, to a resolution of 1 part in 129600000 (1/100th arc second). I therefore used an int32, and had this 'step' in these units. I then wrote a sin, function, which used first a look-up table to find the 'result' for values at intervals of 36000 points, then used successive approximation to get the required values in between. The result in my case was stored in a signed int32, and scaled so that it counted in 1E-9 units. This gave effectively 9DP accuracy.
There are then two answers. Write your own maths library for the functions you require, or possibly work out if the existing accuracy is good enough (it is common to find that you can get better effective accuracy, by altering the order in which arithmetic steps are performed, and it is important to realise that few mechanical processes even approach this level of accuracy in reality...).
Best Wishes |
|
|
|