James Kuyper said:

Richard Heathfield wrote:

<snip>

>If you try to use it to round 0.33 to one decimal place, e.g. like this:

f = round(f * 10) / 10;

then you'll get 0.30000000000000000001 or 0.29999999999999998 or

something like that. What you won't get is *precisely* 0.3 in f.

For the same reason, reciprocal(5.0) can't give you precisely 0.2. So?

Why is one inaccuracy acceptable, and the other is not? Or are you

suggesting that they're both unacceptable?

This was well answered elseperson elsethread. Let me see if I can find it.

Ah, here we go: Message-ID: <fi**********@aioe.org>

in which Keith writes:

"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."

--

Richard Heathfield <http://www.cpax.org.uk>

Email: -http://www. +rjh@

Google users: <http://www.cpax.org.uk/prg/writings/googly.php>

"Usenet is a strange place" - dmr 29 July 1999