Mail Archives: djgpp/2000/08/05/09:45:24
Radical NetSurfer wrote:
> At 5.0, 6.0, etc, Fract display as "1.0" because of a rounding error
> in the printf output. Its only an "optical illusion", and if
> %3.21 is used, you won't see it.
>
> the question is obvious:
> HOW do I maintain a %3.3f output, but NOT get the
> 1.0 "optical illusion" ???
This is not an obvious question at all, especially as you didn't
use %3.3f in your previous post.
One way to print out the fractional part without this rounding
problem is to use sprintf to print the entire number to a buffer,
and then print out the fractional part from there. Or (assuming
your numbers are positive and not too big) you could do this sort
of thing:
((unsigned long)(1000.0*f+0.5)) % 1000
and print that out with leading zeroes. In fact, depending on
what you're doing, it may be better not to use floating-point
numbers at all - just use suitably scaled integers.
S.
- Raw text -