View previous topic :: View next topic |
Author |
Message |
elmar44
Joined: 07 Jun 2005 Posts: 1
|
problem with math.h lib ??? |
Posted: Fri Jun 17, 2005 4:41 am |
|
|
Hi to all programmers..
Now i used the math.h lib first time to get access to functions like
sin() , pow(x,y) ....
So i start with a sinple check:
#include math.h
float x, result;
x = 45
result = sin(x); // the result = 0.707!
..(break)
I used the debugger to watch 'result' and the displayed result is: "1"
sin(45°) = 1 ????
OK - changed the line to result = tan(45);
The displayed result is "0"....... tan(45°) = 0 ?????
So i try another function:
float x, y, result
x = 10;
y = 3;
result = pow(x,y); // this means 10^3 = 1000 !
the displayed result is : -1.345E-24 or results like this - not a "1000"
SO: HOW CAN I USE THE MATH.H LIB ???
What's wrong ????
Thank you for help |
|
|
valemike Guest
|
|
Posted: Fri Jun 17, 2005 6:35 am |
|
|
Which debugger are you using? My past experience has shown that the MPLAB IDE debugger does not interpret CCS's floats properly. A printf ("%f", floar_var); of your value to Hyperterminal ought to give you the correct results though.
-Mike |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Jun 17, 2005 10:19 am |
|
|
Quote: | #include math.h
float x, result;
x = 45
result = sin(x); // the result = 0.707!
..(break)
I used the debugger to watch 'result' and the displayed result is: "1"
sin(45°) = 1 ???? |
The reason is because the math.h library uses Radians, not degrees.
This is stated in the CCS manual, in the section on Sin().
Do it like this to get the correct answer:
Code: | result = sin(PI/2); |
Quote: | result = pow(x,y); // this means 10^3 = 1000 !
the displayed result is : -1.345E-24 or results like this - not a "1000" |
This worked OK for me with PCM vs. 3.225 and MPLAB vs. 7.10.
I have the format of the "result" variable set for MCHP floating point
(not Scientific). This is done in the Properties box for "result" in the
Watch window.
Before we go any farther on this, please post your version of the
CCS compiler and of MPLAB. |
|
|
|