How to write string (float) to file?
jeff at ccvcorp.com
Wed Aug 29 20:34:21 CEST 2001
Grant Edwards wrote:
> In article <G89j7.54140$c8.24141594 at news1.denver1.co.home.com>, Rainer Deyke wrote:
> >This looks like a "just plain wrong" result to me. If you don't care about
> >the least significant digit, this might be tolerable for you.
> You seem to be telling the OP that he's wrong, float(a)/b isn't good enough,
> and he shouldn't be using floating point?
This is exactly what Rainer's point seems to be--that floating-point arithmetic does
not exactly represent decimal numbers. Well, fine, it doesn't. The question is...
does it *matter* in this case? I'd say probably not. In most of the examples that
have been pointed out, the difference is a matter of 1 in the least significant digit.
I would say that, *any* time that any rounding or truncation is going on, there is
uncertainty in the l.s.digit, therefor this should not be much of a surprise--you're
already throwing away most of your accuracy anyhow. As for the final example... yes,
if you use a large enough number your floating-point precision creeps up into higher
and higher positions. That's part of floating-point, and it *is* something to be aware
of, but it's hardly a good reason to never use floats. (Note that the first operand
was *well* above MaxInt, and must have been implicitly converted into an unbounded
Long, then converted to a float -- 1e+040, to be precise, so to speak.) The O.P. gave
no indication that he'd be using numbers of this magnitude, however, and I think that
in most cases that one *would* be using such numbers, the precision of one's input
would be questionable as well... and if not, then the need for exceptional precision
would be specified as part of the problem. It wasn't, in this case--the O.P. was
giving examples using 2 or 3 digits, so it's likely that his actual problem involves
numbers of similar magnitude. Given the problem description, it's hard to imagine how
one could *not* see '%.2f' as "close enough".
More information about the Python-list