Kai-Uwe Bux said:
> Richard Heathfield wrote:
>
>
> Hm. I wonder if this might be a matter of interpreting the problem.
Yes, I think it is.
> The C standard says about sqrt() that it computed the nonnegative
> square root. C++ inherits this requirement. If your interpretation
> of the rounding problem is correct and if we transfer it to
> the other arithmetic operations, then there can be no conforming
> implementations. In fact, even elementary school arithmetic (+,-,*,/)
> cannot be done correctly under that interpretation. However, that
> interpretation of the specs is not the only possible.
>
> A different interpretation of floating point computations is that an
> operation (say multiplication, addition, sqrt, or rounding to a given
> number of decimal places) should yield a double (or float or whatever
> types are topical in the corresponding newsgroup) that is closest to
> the exact mathematical result. If I recall correctly, this is by and
> large the position taken by IEEE754.
Right. The C standard (if __STDC_IEC_559__ is not #defined) imposes
weaker requirements; the result of the sqrt() function needn't be the
*closest* to the exact mathematical result. In fact, I don't think the
C standard imposes any accuracy requirements at all. I'm not certain
that an implementation with sqrt(2.0)==1.5 would be non-conforming.
It's a quality issue, and such an implementation of sqrt() would be
considered badly broken because it would be useless, not because it's
necessarily non-conforming.
But that's largely beside the point, which is that the standard
operators and functions on floating-point are not expected to yield
exact results, because they can't.
> When this (reasonable) interpretation is adopted, the problem of
> rounding to a fixed number of decimals is solvable (and it is indeed
> not different from any other computational problem). And if you don't
> adopt an interpretation like that, floating point arithmetic in general
> is "impossible".
But there's a fundamental difference between sqrt() and a function
that purports to round a floating-point value to a specified number
of decimal places.
For the sqrt() function, obtaining a close approximation to the
mathematical result is obviously a useful thing to do. There are
some contexts in which you might want to throw up your hands and
say "Sorry, it's not possible to compute sqrt(2.0) exactly" -- but
obtaining a close approximation is both useful and expected.
For the rounding function, sure, you could write a function that,
given arguments (3.14159, 2) would return the closest floating-point
approximation of the mathematical value 3.14. But it's not at all
clear either that this would be useful, or that it's really what the
original poster wants.
Either (A) the OP really wants the exact result, and wasn't aware
that it's impossible, or (B) the OP wants an approximate result, and
wasn't aware that it's not particularly useful, or (C) the OP wants
an approximate result, and knows that it's actually useful to him
for some reason that none of us can figure out and he hasn't chosen
to share with us.
My guess is that (A) is the most likely scenario. I speculate that
the OP wants to take a floating-point value with lots of significant
digits after the decimal point, convert it to a value with fewer
significant digits after the decimal point (e.g., 3.14159 --> 3.14),
and use that value for textual output. If so, he has almost certainly
asked the wrong question; what he really do is pass the full-precision
value directly to printf, and use printf's own facilities to round
the textual output, not the number. (Substitite sprintf or fprintf
as needed.)
Some posters in this thread have been wasting a great deal of time
trying to provide incomplete and incorrect answers to what the OP
asked, rather than trying to determine what the OP really needs.
Yes, I think it's a matter of interpreting the problem. Leaving
aside my blind speculation above, the only way to do this is for the
original poster to *tell us* how we should interpret the problem.
Perhaps he's daunted by the volume of replies, but there should be
enough information buried in this mega-thread for him to figure out
how to ask the right question, or at least to discuss how to figure
out how to ask the right question. Until and less the OP returns
and helps us out, all we're doing here is correcting each other's
errors (which is important, but doesn't help the OP nearly as much
as avoiding those errors in the first place would have).