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

Round the number

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



Joined: 08 Sep 2020
Posts: 221

View user's profile Send private message

Round the number
PostPosted: Fri Jan 15, 2021 3:10 am     Reply with quote

CCS C 5.099
MCU 33EP512GM710
#use delay(clock=120MHz,crystal=16MHz)

Hello there
I encountered an interesting situation. I read values with the keypad. Then I put the value I read into the unsigned int16 value variable. Then I do the following on my float cal_value variable.
cal_value = value /10.0;
my value value is 6;
6 / 10.0 = 0.6. Doesn't come out. It turns out 0.59. I want to round it to the screen with% g. But rounding does not happen. What do you think I can do wrong. I used the ceil () function. it makes my value 1 instead of 0.6.
Also the debugger of the ccs c compiler does not work properly. It keeps giving an error. I don't understand how this work.

Also, isn't it possible to divide an unsigned int type variable into a Float type variable?
When I do value / 10 the code doesn't work.
Code:

 the code I used to print the screen;
                current_output = digit_1;
               calculated_value = current_output/10.0;
               glcd_rect(170,84,240,105,ON,OFF);
               sprintf(buffer,"current value:");
               glcd_text57(62,92,buffer,1,ON);
               sprintf(buffer,"%g",calculated_value);
               glcd_text57(182,87,buffer,2,ON);

_________________
Best Regards...
MCUprogrammer
_______________________________
Work Hard
Ttelmah



Joined: 11 Mar 2010
Posts: 19539

View user's profile Send private message

PostPosted: Fri Jan 15, 2021 5:00 am     Reply with quote

Welcome to the limitations of float.

There are some numbers that the standard FP format, can never represent
accurately. So (for example) 0.45. Just as with decimal notation, where
1/3, needs an infinitely long sequence of digits, in binary, 0.45, requires
an infinite sequence to represent accurately. So using the standard
single precision format, 0.45's nearest possible value is 3EE66666 (in IEEE),
which represents 0.4499999. Now you can't 'round' this to get 0.45, since
if you add one to the representation (3EE66667), this gives 0.45000002.
So you can't actually 'have' a value of 0.45....

Now you can round in the output, by adding 0.0049, and then printing just
two digits. So:
Code:

   val=0.45;

   printf("VAL = %5.2F\n\r", val+0.0049);

Wiil display 0.45.

For three digits:
Code:

   val=0.45;

   printf("VAL = %6.3F\n\r", val+0.00049);


In each case, you add just less than 0.5 of the digit beyond where you
want to display, and limit to that digit.

ceil gives the integer above the starting value. Hence your '1'.

I posted a 'round' function, to round to a number of decimals a while
ago:

https://ccsinfo.com/forum/viewtopic.php?p=50803

However this is 'maths heavy' to do.
MCUprogrammer



Joined: 08 Sep 2020
Posts: 221

View user's profile Send private message

PostPosted: Fri Jan 15, 2021 5:17 am     Reply with quote

Thank you so much. Problem solved. Very Happy
_________________
Best Regards...
MCUprogrammer
_______________________________
Work Hard
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