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

multiplication and div.

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







multiplication and div.
PostPosted: Thu Jul 30, 2009 11:38 pm     Reply with quote

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
Code:
 a=tmp1/1000000000;

then a=4.000000

What is wrong?
Thnx...
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Jul 31, 2009 12:20 am     Reply with quote

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:
Code:
signed int32 tmp1;
ccs_guest
Guest







multiplication and div.
PostPosted: Fri Jul 31, 2009 12:44 am     Reply with quote

After making tmp as signed, a is having -0.00000;
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Jul 31, 2009 12:50 am     Reply with quote

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








PostPosted: Fri Jul 31, 2009 12:57 am     Reply with quote

Quote:

a=((float)tmp1)/1000000000.0;

That solved my problem.
Thanks a lot.
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