CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Watch floats

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
john822179



Joined: 22 Jun 2011
Posts: 7
Location: Tandragee, Northern Ireland

View user's profile Send private message AIM Address

Watch floats
PostPosted: Sun Nov 09, 2014 1:50 am     Reply with quote

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: 19492

View user's profile Send private message

PostPosted: Sun Nov 09, 2014 2:20 am     Reply with quote

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

View user's profile Send private message AIM Address

PostPosted: Sun Nov 09, 2014 3:59 am     Reply with quote

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: 19492

View user's profile Send private message

PostPosted: Sun Nov 09, 2014 5:04 am     Reply with quote

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.....
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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