View previous topic :: View next topic |
Author |
Message |
Eduardo__
Joined: 23 Nov 2011 Posts: 197 Location: Brazil
|
float type is not working correctly |
Posted: Thu May 17, 2012 6:36 pm |
|
|
float type is not working correctly
I don´t know why my float calculations are going wrong. int16 or 8bits is working well. I´d never experienced something like that.
I´m using PIC18F26J11 and the compiler version is 4.124.
See the code:
#include <18F26J11.H>
#DEVICE *=16 ICD=TRUE PASS_STRINGS=IN_RAM
Code: | float SE_conv16tofloat(signed int16 val) { //Do something
signed int16 val_h,val_l;
float fval_h,fval_l,rv;
fval_h=20.0;
fval_l=5.0*0.125;
rv=fval_h+fval_l; //do a simple float sum
delay_ms(1); //<<I put a breakpoint here and watched the values of fval and rv.
//The values was inconsistent.
return rv;
}// |
I put a breakpoint inside function. For example, when I watch fval_h value,... it´s value is not 20. It gets a very strange value(some very very small value).
I´m using ICD2 debugger.
Anyone can instruct me about what to do? I´m completely lost.
Thanks a lot! ![Idea](images/smiles/icon_idea.gif) _________________ Eduardo Guilherme Brandt |
|
![](templates/subSilver/images/spacer.gif) |
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Thu May 17, 2012 7:35 pm |
|
|
I don't use floats, but I have heard that debuggers sometime misinterpret float values. You might look at the values in hex and calculate the float values yourself. _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19360
|
|
Posted: Fri May 18, 2012 1:32 am |
|
|
There are two different fundamental 'versions' of a 4byte float on the PIC. The MicroChip version (which was designed to be slightly more efficient with the early PIC architecture), and the IEEE version. CCS uses the former for the PIC16/PIC18, and the latter for the PIC24 (however they store the bytes the opposite way round to the Microchip version). PIC debuggers have to be able to handle all, and so (for example), in MPLAB, a watch put on a float, has the option to be set to be 'Coff float type', 'IEEE 754', 'Microchip High,low' or Microchip Low,high'. The first is meant to 'auto select', but often doesn't work. The second is the standard for PCD, and C18 now. The third is used with C18, on older chips, while the last is the format used by CCS. Because the 'auto' often gets it wrong, you end up seeing silly values - I'd expect to see something like 1.3456E-41 if this is happening (some very very small value...).
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
Eduardo__
Joined: 23 Nov 2011 Posts: 197 Location: Brazil
|
|
Posted: Fri May 18, 2012 7:42 am |
|
|
Dear Ttelmah,
Yes, the shown value is 1.345667e-041(for a correct value that should be 20,625) that in binary is 00000000 00000000 00100101 10000011.
I think that it is a bug from MPLAB(I´m using MPLAB version 8.73), because when I change float displaying format(inside MPLAB watch window) nothing happens(the same value is shown despite of float format I choose).
I´m downloading MPLAB X.
I´m going to put the status of this problem after installing MPLAB X.
Thanks and regards. _________________ Eduardo Guilherme Brandt |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19360
|
|
Posted: Fri May 18, 2012 7:59 am |
|
|
I think (memory being stretched here), the 'new' format is only used when the value updates.
At the moment, MPLAB-X, has more problems than the older MPLAB.
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
Eduardo__
Joined: 23 Nov 2011 Posts: 197 Location: Brazil
|
|
Posted: Fri May 18, 2012 9:03 am |
|
|
I´m thanked so much Mr. Ttelmah!
MPLABX unfortunately seems to not support my ICD2 debugger! So I tried more things in my old MPLAB 8.73.
I discovered that MPLAB only use the chosen float type to new variables that I input into watch window. The variables that I put before changing default float type watch options not update format even after refreshing then or recompiling code.
So the way to display correctly float type was changing watch window float settings to "Microchip low:high and then input watch values again.
MPLAB select MCHP Float, Low:High byte order to new variables that I input inside watch window.
Yes, I like MPLAB 8 as it is very stable. I´ve never had any problems with MPLAB before. So I´m going to keep developing in this old version IDE.
Thanks. Have a good weekend! _________________ Eduardo Guilherme Brandt |
|
![](templates/subSilver/images/spacer.gif) |
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Fri May 18, 2012 11:06 am |
|
|
Or you could look at the result of doing an sprintf().
Mike |
|
![](templates/subSilver/images/spacer.gif) |
|