View previous topic :: View next topic |
Author |
Message |
haxan7
Joined: 27 Jul 2013 Posts: 79
|
[Solved]Comparison with a negative Integer. |
Posted: Sat Mar 22, 2014 9:26 am |
|
|
Compiler Version 5.008
Compiler isn't comparing negative integers correctly. LCD confirms negative int received by the function, but the code in IF is executed every time.
Is this a bug, or am I missing something.
Code Below:
Code: |
void right(int percent){
printf(lcd_putc,"\f%d",percent);
if((int)percent>=0){
printf(lcd_putc,"\n%d,if",percent);
output_high(RightC1);
output_low(RightC2);
}else{
percent = -percent;
output_high(RightC2);
output_low(RightC1);
printf(lcd_putc,"\n%d,else",percent);
}
pwm2(percent);
}
|
Last edited by haxan7 on Sat Mar 22, 2014 10:11 am; edited 2 times in total |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9226 Location: Greensville,Ontario
|
|
Posted: Sat Mar 22, 2014 9:30 am |
|
|
could be either as 5.008 is a real early however my understanding is that 'int' hmm...defaults to unsigned integer ( 0-255).
The observation that the LCD displays the value correctly just shows the printf functions correctly.
hth
jay |
|
|
haxan7
Joined: 27 Jul 2013 Posts: 79
|
|
Posted: Sat Mar 22, 2014 9:42 am |
|
|
temtronic wrote: | could be either as 5.008 is a real early however my understanding is that 'int' hmm...defaults to unsigned integer ( 0-255).
The observation that the LCD displays the value correctly just shows the printf functions correctly.
hth
jay |
Any way of fixing this without changing the compiler? |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9226 Location: Greensville,Ontario
|
|
Posted: Sat Mar 22, 2014 9:50 am |
|
|
hmm. the obvious one is to declare the variable as a signed integer.
If you press F11 while your project is open the CCS HELP manual magically appears, goto contents,goto data definitions,goto basic types....
hth
jay |
|
|
haxan7
Joined: 27 Jul 2013 Posts: 79
|
|
Posted: Sat Mar 22, 2014 9:52 am |
|
|
Definitely a bug. int should default to signed int by C standard.
using signed int percent instead of int percent fixed the problem. |
|
|
dyeatman
Joined: 06 Sep 2003 Posts: 1934 Location: Norman, OK
|
|
Posted: Sat Mar 22, 2014 10:32 am |
|
|
It is NOT a bug just a choice by CCS.
CCS clearly states in the manual that the INT8 defaults to UNSIGNED. _________________ Google and Forum Search are some of your best tools!!!! |
|
|
|