In article <11**********************@g14g2000cwa.googlegroups .com>,

A. L. <ba*****@gmail.com> wrote:

My question is that how to get the complete/precise string

representation of a float number using C standard library, but not in a

style of printf in which the output precision should be specified as a

great number (it will bring out superfluous trailing zeros).
Could somebody here give me some advices?

There is no way to do that with the standard library. It requires

extra information about the "real" precision of a number is.

Your Pi example dealt with stripping off trailing 0's, but

if I use const double Foo = 1.234567890; then the 0 *is* part of the

precision and stripping it off would be incorrect.

If you do any arithmetic manipulation of numbers, such as Pi + Foo

or Pi / Foo then "something" needs to know how to figure out the

correct precision of the result. C doesn't do that for you.

Any discussion of the "precise" string representation of a float is

doomed to failure if you use the standard machine representations

instead of indefinite-precision libraries and numeric analysis to

figure out how many digits you ought to be calculating.

For example, if you have 4.5678901 then it could happen that

the closest that a 'float' could get to representing that is

4.5678904875 which by the usual rounding rules would round down to

4.567890 and your remove-trailing-zeroes approach would output that as

4.56789 which would not be correct!

--

I was very young in those days, but I was also rather dim.

-- Christopher Priest