Hello, I have a very curious problem with float arithmetics: When I use the following code float variable; variable = 12.0; printf ("%f", variable); printf (lcd_putc, "%f", variable); the output on both the display and the RS232 is 11.9999999 This wouldn't be a problem, but in my application, I only need one number after the comma. So I use printf ("%4.1f", variable); and the "12.0" which becomes "11.9999999" appears as "11.9" !!!!! Isn't there any function for outputting variables which doesn't just "cut" the variable to the desired length like %4.1f but which rounds to the last significant number? So if the number is 11.99999, the program recognizes that after 11.9 there is a 9 following, so the number is rounded to 12.0. Just like rounding in mathematics is done. Not just ignoring the numbers after the desired range. It's a pity that I couldn't find any rounding function within the math functions of the ccs! I am using a PIC16F876A and the CCS Compiler IDE version 3.43 ; PCx version 3.203 Thanks a lot in advance, Thomas

:
Moved by Admin

If you look into the manual of CCS (i checked in version 4), you'll find the description of printf on page 186: [..] f Float with truncated decimal g Float with rounded decimal [..] If you use g instead of f, the number will be rounded instead of truncated. (I have not tried it myself) Severino

> the output on both the display and the RS232 is 11.9999999 That's strange. Normally all float numbers with no fractional part are internally represented exactly. The formatting routine should not add an error in this case. > and the "12.0" which becomes "11.9999999" appears as "11.9" !!!!! That's a bug in the C library! The C standard requires that "The value is rounded to the appropriate number of digits." ^^^^^^^ You can - ask the manufacturer of the compiler/library to fix this bug - write your own formatting routine - use a work-around by adding 0.05 to the value when passing it to the printf function: printf ("%4.1f", variable + 0.05); For negative numbers you might have to subtract 0.05 instead of adding it. You will have to remove this work-around from your software when you will eventually link it against a fixed library, otherwise it won't work correctly. > f Float with truncated decimal > g Float with rounded decimal The developers of the lib haven't understood the difference between the two format specifiers :-/