CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

error float decimal with sin() cos()

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
T0ni0



Joined: 21 Jan 2005
Posts: 11

View user's profile Send private message MSN Messenger

error float decimal with sin() cos()
PostPosted: Wed Jul 20, 2005 10:14 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Jul 20, 2005 11:08 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Jul 20, 2005 11:32 am     Reply with quote

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

View user's profile Send private message Send e-mail MSN Messenger

PostPosted: Wed Jul 20, 2005 4:54 pm     Reply with quote

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

View user's profile Send private message MSN Messenger

PostPosted: Thu Jul 21, 2005 12:45 am     Reply with quote

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







PostPosted: Thu Jul 21, 2005 3:47 am     Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group