View previous topic :: View next topic |
Author |
Message |
dynamitron
Joined: 18 Mar 2009 Posts: 38
|
strange result with float |
Posted: Sat Aug 04, 2012 7:25 am |
|
|
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
|
|
Posted: Sat Aug 04, 2012 3:28 pm |
|
|
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
|
|
Posted: Sat Aug 04, 2012 3:54 pm |
|
|
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
|
|
Posted: Sat Aug 04, 2012 4:05 pm |
|
|
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:
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);
} |
|
|
|
|