|
|
View previous topic :: View next topic |
Author |
Message |
autobots Guest
|
CCS math functions bug? |
Posted: Tue Dec 29, 2009 9:02 pm |
|
|
I'm using the maths function in my programs, but I found out there is a bug in some math functions.
So I purposely check it by just simply print out like this:
Code: | printf("%.4f\r\n",cos(0.7854)); |
the result I get is 0.7071, but result from my calculator is 0.9999.
as well as tan(0.7854) = 1 (in CCS).
Can I know what is the problem there?
I am using compiler version V4.088. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Dec 29, 2009 9:19 pm |
|
|
The CCS manual says this:
Quote: |
Syntax:
val = cos (rad)
Parameters:
rad is a float representing an angle in Radians -2pi to 2pi.
Value is a float. |
|
|
|
mbradley
Joined: 11 Jul 2009 Posts: 118 Location: California, USA
|
|
Posted: Tue Dec 29, 2009 9:20 pm |
|
|
This is correct, cos(0.7854) = .7071
You might be using Degrees on your calculator, not Radians. Switch your calculator over to Radians, and try again.
Try this: cos(0.7854/57.2957795)
simple macro:
#define deg2rad(d) (d / 57.2957795) _________________ Michael Bradley
www.mculabs.com
Open Drivers and Projects |
|
|
a Guest
|
|
Posted: Tue Dec 29, 2009 9:51 pm |
|
|
ok..sorry my mistaken..
thanks for the reply.
|
|
|
Ttelmah Guest
|
|
Posted: Wed Dec 30, 2009 6:11 am |
|
|
As a 'comment', make the macro:
Code: |
#define deg2rad(d) (d * 0.01745329)
|
This is a general rule', that multiplication, is always faster than division (applies even on the fast maths hardware on a PC, by a couple of clock cycles), but on the PIC, the difference really 'matters'. On a PIC16, using a multiplication, rather than a division, halves the time. In a PIC18 (wth the hardware multiplier), it improves things by a factor of nearly 4*.....
So it is worth getting into the habit, when dealing with constants like this, of always generating the 'reciprocal' value, and multiplying, rather than using division.
Best Wishes (and Happy New Year). |
|
|
|
|
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
|