View previous topic :: View next topic |
Author |
Message |
ccs_guest Guest
|
multiplication and div. |
Posted: Thu Jul 30, 2009 11:38 pm |
|
|
Hi all,
Consider the following scenario.
Code: |
int32 tmp1;
float a;
tmp1=-33862;
a=(float)tmp1/1000000000;
|
The value in a should be should be -0.00003386 but a=4.294934.
If I do
then a=4.000000
What is wrong?
Thnx... |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Jul 31, 2009 12:20 am |
|
|
Quote: | int32 tmp1;
float a;
tmp1=-33862; |
In CCS, all integer data types are unsigned by default. To declare tmp1
as signed, you need to do this:
|
|
|
ccs_guest Guest
|
multiplication and div. |
Posted: Fri Jul 31, 2009 12:44 am |
|
|
After making tmp as signed, a is having -0.00000; |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Jul 31, 2009 12:50 am |
|
|
Which 'a' ? The first one, or the 2nd one ?
The first expression is doing floating point math, and the 2nd one is
doing integer math.
Always make a test program and always post it (along with your compiler
version). The program shown below displays the following results:
Quote: |
a = -0.000033862
a = 0.000000000
|
Code: | #include <16F877.H>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
//======================================
void main()
{
signed int32 tmp1;
float a;
tmp1=-33862;
a=((float)tmp1)/1000000000.0;
printf("a = %10.9f \r", a);
a=tmp1/1000000000;
printf("a = %10.9f \r", a);
while(1);
} |
|
|
|
Guest
|
|
Posted: Fri Jul 31, 2009 12:57 am |
|
|
Quote: |
a=((float)tmp1)/1000000000.0;
|
That solved my problem.
Thanks a lot. |
|
|
|