Problems with float arithmetics using a PIC 16F876

Guest #659434
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
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
Guest
> 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 :-/

Reply

A question of your own? This thread is over 19 years old. Ask it in a thread of its own – more people read along there, and it gets answered more often and sooner.

Start a new thread

A fix, an addition or feedback on this thread? Then reply here.

Formatting
  • [c]C-Code[/c]
  • [code]code in other languages, ASCII drawings[/code]
  • [math]formula (LaTeX syntax)[/math]
  • [[Title]] – link to an article
  • Link to another post: right-click its post ID (#8078628), copy the address, paste it into the text

More about formatting

Post long source code as an attachment, not in the text.

Already registered? Log in

Not shown to anyone. We send you a link; your post appears once you confirm it there.

By submitting you create a free account and accept the terms of use.