[erlang-questions] In Erlang , {1.1*1.1.} show me the answer is 1.2100000000000002, why?
Richard A. O'Keefe
ok@REDACTED
Mon Oct 27 23:23:44 CET 2014
On 26/10/2014, at 10:02 PM, Loïc Hoguin wrote:
> Floats are an inaccurate number format.
That is a very confusing way to put it.
Floating point NUMBERS are exact.
Floating point OPERATIONS round.
This includes the operation "convert from decimal to binary".
> See http://en.wikipedia.org/wiki/Floating_point#Accuracy_problems
A number is *precise* if it is given to many digits.
A number is *accurate* if it is correct to many of those
digits.
3.151592653589 is a precise but inaccurate approximation to pi.
3.14 is an imprecise but more accurate one.
IEEE floating point arithmetic is, if used with care,
more accurate than we can physically measure for most problems.
> Decimal32/decimal64 are the accurate equivalent, though quite new and as of now rarely found natively in programming languages.
Decimal floating point arithmetic is no more accurate than
binary floating point arithmetic. Decimal floating point
arithmetic can accurately represent a wider range of
numbers stated in decimal, but it is still incapable of
representing 1/n where n is not a factor of a power of 10.
Furthermore, if you take
X = 1.0,
Y = X + 1.0e-15,
Z = Y*Y
then even in decimal64 arithmetic, Z will only approximate
the mathematically exact answer.
Decimal floats were available early. The IBM 650 had
decimal floats with 8 digits of significand and 2 digits
of exponent. The Burroughs Medium Systems machines had
decimal floats with up to 99 digits of significand.
Decimal floats were not abandoned simply for speed; they had
some numerical problems binary floats are free of. The new
standard partly mitigates those, but only partly.
There is a standard way to access decimal floating point
arithmetic in C. I have no experience with it, because gcc
doesn't support it unless you have hardware for it.
More information about the erlang-questions
mailing list