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 support@ccsinfo.com

CCS math functions bug?

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







CCS math functions bug?
PostPosted: Tue Dec 29, 2009 9:02 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Dec 29, 2009 9:19 pm     Reply with quote

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

View user's profile Send private message Visit poster's website

PostPosted: Tue Dec 29, 2009 9:20 pm     Reply with quote

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







PostPosted: Tue Dec 29, 2009 9:51 pm     Reply with quote

ok..sorry my mistaken..
thanks for the reply.
Smile
Ttelmah
Guest







PostPosted: Wed Dec 30, 2009 6:11 am     Reply with quote

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

Best Wishes (and Happy New Year).
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