iota to array

H. S. Teoh hsteoh at quickfur.ath.cx
Tue Feb 27 00:04:59 UTC 2018


On Mon, Feb 26, 2018 at 11:34:06PM +0000, psychoticRabbit via Digitalmars-d-learn wrote:
[...]
> and what's going on here btw?
> 
> assert( 1 == 1.000000000000000001 );  // assertion error in DMD but not in
> LDC
> assert( 1 == 1.0000000000000000001 );  // no assertion error??
> 
> (compiled in 64bit mode)

A 64-bit double can only hold about 14-15 decimal digits of precision.
Anything past that, and there's a chance your "different" numbers are
represented by exactly the same bits and the computer can't tell the
difference.

If you want arbitrary precision you'll have to use an arbitrary
precision library like GMP.  It will be a lot slower than built-in
floats or doubles, though.

In general, real numbers have an infinite number of digits, and so would
require infinite memory and infinite time to perform any computations at
all.  What we have now is a reasonably practical compromise. :-D  But
that also means there will be some inherent inexactness because we're
trying to represent an infinite number of digits in a small, finite
space.

(There *are* exact representations for certain subsets of irrationals
that allow fast computation that does not lose precision. But generally,
they are only useful for specific applications where you know beforehand
what form(s) your numbers will take. For general arithmetic, you have to
compromise between speed and accuracy.)


T

-- 
In theory, software is implemented according to the design that has been carefully worked out beforehand. In practice, design documents are written after the fact to describe the sorry mess that has gone on before.


More information about the Digitalmars-d-learn mailing list