![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
fvnktion
Joined: 27 Jun 2006 Posts: 39
|
if statement evaluated incorrectly - float type |
Posted: Wed Feb 11, 2009 6:07 pm |
|
|
I am evaluating float type value and the if statement is not evaluating them correctly.
In the first cases of test_var and test_var2 it is evaluated correctly, but not in the second case where they are 16 bit values int equivalents.
Do i need to do something special to evaluate them correctly? I have tried to type cast them , but no go. Any other suggestions?
using compiler 4.083
Thanks,
Code: | //the if evaluates correctly here
/*
test_var = 1;
test_var2 = 5;
*/
//the if evaluates incorrectly here - never enters
test_var = 14810;
test_var2 = 52370;
if (test_var2 > test_var ){
T2ON = 1;
led_blink();
T2ON = 0;
} |
|
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Feb 11, 2009 6:51 pm |
|
|
Post the variable declarations. It's essential.
Post your PIC and your compiler version. Do this on any suspected bug. |
|
![](templates/subSilver/images/spacer.gif) |
rnielsen
Joined: 23 Sep 2003 Posts: 852 Location: Utah
|
|
Posted: Thu Feb 12, 2009 10:10 am |
|
|
If you have declared each variable as int8's then when you assign the different values they will actually hold the following:
Code: | int8 test_var, test_var2;
test_var = 1;// hex value of 0x01
test_var2 = 5;// hex value of 0x05
test_var = 14810; // hex value of 0xDA
test_var2 = 52370;// hex value of 0x92
|
As you can see, test_var is larger than test_var2 if they are int8's. Make sure you have them declared as int16. I believe that should cure your problem. Remember that simply declaring a variable as 'int' it will default to int8.
Ronald |
|
![](templates/subSilver/images/spacer.gif) |
cuma_polat
Joined: 24 Nov 2008 Posts: 3
|
float type values |
Posted: Sun Jun 28, 2009 5:19 am |
|
|
Hi,
I have same problem about comparison of two float type values. As you see from the code I wrote very simply in order to calculate big float type number. But the program gives "20" from the LCD.
My main question is
"Can we compare two float type numbers by using if statement in CCS C compiler?"
Code: |
#include <30F4013.h>
#device ADC=10
#use delay(clock=10000000)
#FUSES HS
#use rs232(UART2,baud=9600,xmit=PIN_F3,rcv=PIN_F2,parity=N,bits=8)
#include <math.h>
#include <flex_lcd.c>
#use fast_io(B)
#use fast_io(C)
#use fast_io(D)
#use fast_io(F)
float n=0.8;
float c=0.4;
int8 nu2;
void main(){
lcd_init();
while(true){
if ( c < n ) nu2=1;
else nu2=20;
printf(lcd_putc,"\fnu2=%d",nu2);
delay_ms(200);
}
}
|
or how can I? |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah Guest
|
|
Posted: Sun Jun 28, 2009 7:29 am |
|
|
The original poster, did not have a 'float' problem, despite the title of the thread.
What you post, works OK, on a PIC18, so if you are seeing '20', I'd suspect a bug with the 30F code.
Try something that tests both constant values, and variables, like:
Code: |
//Processor definition, clock, and fuses here
//#use RS232 here
#include <input.c>
void main(void) {
float val1,val2;
char temp[40], *ptr;
val2=0.4;
val1=0.2; //Rule out an initialisation bug
if (val2<val1) putc("\fwrong");
else putc("\fOK");
while(true) {
//Now try with a variable value
get_string(temp,39);
val1=strtod(temp,ptr);
if ( val1 < val2 ) putc("\fbelow 0.4");
else putc("\f0.4 or above");
}
}
|
This does correctly distinguish float values on a PIC18.
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
|
|
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
|