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

strange result with float

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



Joined: 18 Mar 2009
Posts: 38

View user's profile Send private message

strange result with float
PostPosted: Sat Aug 04, 2012 7:25 am     Reply with quote

I have the following code with all variables typed as float:
Code:
dj = 16.5 - (temp_max_jour + temp_min_jour)/2;

this operation give 0 all the time what ever the values I give to the variables.

In order to get a correct result I have to cut the code in pieces as follow :
Code:
dj = temp_max_jour/2;
dj = dj + temp_min_jour/2;
dj = (16.5 - dj);

Why ? any explanation ?

thanks for the answer !!

compiler PCWHD 4.093
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Sat Aug 04, 2012 3:28 pm     Reply with quote

PCWHD is a compiler package. You should tell the compiler PCB, PCM, PCH or PCD. At least PCD V4.093 had a number of bugs with complex expressions.
dynamitron



Joined: 18 Mar 2009
Posts: 38

View user's profile Send private message

PostPosted: Sat Aug 04, 2012 3:54 pm     Reply with quote

I use the compiler integrated in mplab.
I do not kown exactly if I use PCB, PCM, PCH or PCD all I know is that my processor is 18f2525.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Aug 04, 2012 4:05 pm     Reply with quote

Also, you should post a compilable test program so we can test your
problem. That way you might get an answer in the first reply.

I installed vs. 4.093 and made a test program and ran it in MPLAB
simulator. I got this result and it's correct:
Quote:

dj = 14.500

Code:

#include <18F2525.h>
#fuses INTRC_IO,NOWDT,PUT,BROWNOUT
#use delay(clock=4M)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

//======================================
void main()
{
float dj;
float temp_max_jour;
float temp_min_jour;

temp_max_jour = 1.5;   // 1.5 + 2.5 = 4.0
temp_min_jour = 2.5;

dj = 16.5 - (temp_max_jour + temp_min_jour)/2;

printf("dj = %7.3f \n\r", dj);

while(1);
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