Iris said:
I have this problem:
I would like to add two doubles: -0.07 + 0.0175 and te result, instead of
be -0.0525, is -0.052500000000000005. I do not know what I have this error
and I would like avoid it or truncate the double. How can I do it?
Thank you
This subject comes up every week or two on the Java newsgroups and
equivalently frequently in every other language newsgroup that I've
followed. While I understand that binary representations of
floating point numbers are more efficient, I wonder if this continual
misunderstanding of floating point suggests that the
default floating point representation for data should be decimal rather
than binary.
This isn't going to happen any time soon, but in a world where only a tiny
fraction of applications really need to worry about the efficiency of
floating point computations, maybe it would be better to have a
a default numeric model in the computer that is closer to what we use outside
of it. Of course it still won't be perfect. There will still be the
"How come 10 - 10./3 * 3 != 0?" messages, but my sense is that
that is less confusing to users than the fact that most of the short decimals
they have been using all of their lives are not representable (exactly)
by computers.
PL/1 catered to this as I recall. One could ask for float binary,
or float decimal numbers -- though I'm not sure if that wasn't just
different ways to describing the precision of the underlying numbers. I doubt that
we want to ever preclude binary floating point numbers, but maybe future
languages should consider adopting decimal floating point numbers as
a first-class data type. (Rather than, e.g., the fairly clumsy classes
that Java provides.)
Regards,
Tom McGlynn