View previous topic :: View next topic |
Author |
Message |
Eduardo__
Joined: 23 Nov 2011 Posts: 197 Location: Brazil
|
PCD float seems to be not working right |
Posted: Tue Aug 21, 2012 7:40 am |
|
|
I´m using PIC30F5013 and PCD compiler Version 4.124
floats seems not to be working right.
Code: | float ttt=0,ttt1=1,ttt2=2;
while(true) {
delay_ms(1);
if (ttt>ttt1) {
liga(O_M3); //The uC enters here. It is wrong!
delay_ms(1); // :(
}
else {
desl(O_M3);
delay_ms(1);
}
} |
Some suggestions?
Very thanks _________________ Eduardo Guilherme Brandt |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Tue Aug 21, 2012 8:00 am |
|
|
If the numbers are going to be integers, use integers instead....
Float comparisons have had 'intermittent' problems with PCD for quite a while.
<http://www.ccsinfo.com/forum/viewtopic.php?t=43918&highlight=pcd+float>
Several other examples as well.
Much safer to use scaled integers for example, and quicker as well.
Best Wishes |
|
|
Eduardo__
Joined: 23 Nov 2011 Posts: 197 Location: Brazil
|
|
Posted: Tue Aug 21, 2012 8:17 am |
|
|
Thanks Ttelmah.
But floats are essential for my program. I do intensive fractional math calcs.
It have had already worked in the past, as it is.
Could it be a compiler version problem?
Thanks _________________ Eduardo Guilherme Brandt |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Tue Aug 21, 2012 8:46 am |
|
|
It appeared to work quite some time ago, but has been 'wrong' for various compilers for quite a while.
Seriously though I can't really envisage that you 'need' float comparisons. If you know the likely range of the numbers, much better to use scaled integers. If you just want to do a comparison, then convert the floats to integer for the comparison. So:
Code: |
float ttt=0,ttt1=1,ttt2=2;
while(true) {
delay_ms(1);
if ((int32)(ttt*100)>(int32)(ttt1*100)) {
liga(O_M3); //The uC enters here. It is wrong!
delay_ms(1); // :(
}
else {
desl(O_M3);
delay_ms(1);
}
|
Gives a 2 decimal place integer comparison, which does work.
Moan at CCS, but this has been happening off and on for several versions. You might want to see if the behaviour is the same with another type (float48 for example). I'd write 'round it' as being safer than relying on any CCS fix at present.
Best Wishes |
|
|
Eduardo__
Joined: 23 Nov 2011 Posts: 197 Location: Brazil
|
|
Posted: Tue Aug 21, 2012 9:02 am |
|
|
yes, its the only solution.
thanks
Eduardo _________________ Eduardo Guilherme Brandt |
|
|
Eduardo__
Joined: 23 Nov 2011 Posts: 197 Location: Brazil
|
|
Posted: Tue Aug 21, 2012 10:08 am |
|
|
float48 is working fine also.
thanks _________________ Eduardo Guilherme Brandt |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Tue Aug 21, 2012 1:37 pm |
|
|
I don't see the float compare error in present PCD V1.35. |
|
|
|