View previous topic :: View next topic |
Author |
Message |
john822179
Joined: 22 Jun 2011 Posts: 7 Location: Tandragee, Northern Ireland
|
Watch floats |
Posted: Sun Nov 09, 2014 1:50 am |
|
|
Hello,
I am working on an application which uses floats.
Whilst debugging my code, I am seeing strange values in WATCH.
My Code:
Code: | int total=0, number=0;
float percentage=0.0f;
int T1;
void main()
{
number = 65;
total = 85;
percentage=((float)number/total)*100f;
T1 = 0;
while(1)
{
}
} |
I have a BreakPoint at T1 = 0;
WATCH shows the following:
Symbol Decimal
number 65
total 85
percentage 4059043973
However, hovering over <<percentage>> shows 76.4705887
Can anyone explain or tell me how to see the correct value in WATCH?
I am using MPLAB IDE v*.92 8.92.00.00
Many thanks
John822179 |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Sun Nov 09, 2014 2:20 am |
|
|
Right click on the symbol in the watch window.
Select properties.
Change the type to MHCP float (assuming you are on a PIC16/18).
Set byte order to Low/High.
However the old comment applied. Avoid floats.....
Realistically, your incoming value is just 8bits. To waste four bytes of storage, and all the time for float on this is very silly.
If you instead use:
Code: |
int16 tenth_percent;
int total, number; //why waste time zeroing these?
number = 65;
total = 85;
tenth_percent=((int32)number*1000)/total;
|
You have an integer giving tenth percent values (can be printed with %4.1LW), and save space and time |
|
|
john822179
Joined: 22 Jun 2011 Posts: 7 Location: Tandragee, Northern Ireland
|
|
Posted: Sun Nov 09, 2014 3:59 am |
|
|
Many thanks for your speedy reply Ttelmah.
Unfortunately, my WATCH system was already set up as you suggested.
I have tried other settings, but none display the correct value.
I have also tried this in MPLab X, just downloaded and I get exactly the same display.
Incidentally, I am using a 16F1829
John 822179 |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Sun Nov 09, 2014 5:04 am |
|
|
The number you are getting displayed, says it is interpreting it as an int32, with the bytes backwards.
Make sure you are setting the watch on this exact value, by right clicking it.
You have to do it on 'percentage' in the watch window. Changing the global setting, does not change an already created watch.
Remember you have to 'apply' the changes or they are forgotten.
If I set the watch to interpret as 32bit decimal with high/low byte order, it gives 4059043973..... |
|
|
|