jacob said:
Richard said:
jacob navia said: [...]
Of course I agree with your short description of these people.
And one of their "word games" was precisely to say that it is
impossible to write:
double rounto(double x, unsigned places);
No, of course you can *write* it (duh). It just won't do what you
claim it does, that's all.
I claim that this will deliver the best approximation to the
rounding to n decimal places, and I have never claimed otherwise.
If you do not agree, produce a better function.
[code snipped]
Provide a better function to do what?
jacob, you persist in missing the point, even as you implicitly
acknowledge it.
The OP did *not* ask for a function to provide *the best approximation
of* a double value rounded to n decimal places. The OP asked for a
function to provide *a double rounded to n decimal places*. You have
simply assumed that, since what he literally asked for is impossible, he
must really be asking for something that's similar but possible.
How do you know what the OP really wants?
When someone asks a question with incompletely specified requirements,
as has happened here, we can ask for clarification (as we've done, but
the OP has so far failed to offer it), or we can make reasonable
assumptions. You've assumed that a close approximation is good enough.
You *might* be correct, but your solution, though it may meet the OP's
requirements when coupled with your assumption, is IMHO not particularly
useful. (Think about it for a moment. Given a function such that
roundto(3.14159, 2) yields *approximately* 3.14, most likely something
like 3.140000000000000124344978758017532527446746826171875, would *you*
have any real use for such a thing? You might provide it in your
library, but would you really use it in your own code?)
What I and most others here have assumed instead is that what the OP
really wants is something *useful*. Given this assumption (which I
acknowledge is just an assumption), the OP needs to step back a bit and
re-think the problem. He probably really wants a *textual*
representation of "3.14", which is exact, rather than a floating-point
representation that can only be approximate, and whose only likely
purpose is to produce the exact textual representation anyway. The
point I suspect he's missing is that the original floating-point value
(an approximation of 3.14159) is just as useful as an approximation of
3.14 for the purpose of producing the string or output "3.14".
If you choose to make an assumption about what the OP really wants,
that's fine. You might even be correct. What I find annoying is your
stubborn insistence that no other assumption is possible.
There's another possibility that I don't recall anyone mentioning.
Perhaps this is a homework assignment, and perhaps it's the instructor
who fails to realize that decimal rounding of binary floating-point
values is not useful.