[erlang-questions] Strange float / tuple problem

Richard A. O'Keefe ok@REDACTED
Tue Jun 7 03:18:02 CEST 2016

On 5/06/16 2:07 AM, Donald Steven wrote:
> Thanks Matthias and Craig.  I see the issue and I've got a work 
> around. BTW, is there a way to truncate a float to a given # of 
> places, not just 0 -- not just a formatted output, but a true truncation?
I think you may need something more fundamental than a workaround.
For what it's worth, DBL_EPSILON for IEEE doubles is
Now DBL_EPSILON is the smallest value of e such that 1.0+e /= 1.0,
so the 4.4...e-16 value you reported is 2 units in the last place for a
number close to 1 or 1 unit in the last place for a number close to 2,
or to put it another way, one or two rounding errors.

We are definitely dealing with some sort of calculation here, not a
literal 0.0, and it would help a lot to show us that calculation.

Since IEEE arithmetic gives you the closest representable value for
the result of any one operation, I think you'll appreciate that truncation
is, as a general rule, going to INCREASE errors.  The best you can do
to truncate a floating point number to a given number of decimal places
is truncate(X*10**N)/10**N.
Needless to say, the result is NOT exact, because almost all decimal 
are not exactly representable in binary arithmetic.

Why do you think that truncation rather than rounding is right here?

Calculations where you expect exact answers should be done in integer
arithmetic, if possible.

More information about the erlang-questions mailing list