[erlang-questions] Any way to correct the round off errors?

Lev Walkin vlm@REDACTED
Sun Sep 20 13:44:36 CEST 2009


G.S. wrote:
> Hello everyone,
> 
> When subtracting in Erlang: 0.92915-0.92945 we should get -0.0003 but Erlang
> gives: -2.9999999999996696e-4   (when doing 92915-92945 Erlang gives -30 so
> that's ok).
> 
> Anyway to make make it give -0.0003 ?, and in general make it give more
> accurate answers?

Your computer hardware cannot represent 0.0003.

[vlm@REDACTED:~]> cc -o c c.c && ./c
-0.000300
-0.00029999999999999997
[vlm@REDACTED:~]> cat c.c
#include <stdio.h>

int main() {
	double d = -0.0003;
	printf("%f\n", d);
	printf("%.20f\n", d);
}
[vlm@REDACTED:~]>


So, Erlang does not give you less accurate numbers.
Erlang just gives you a differently formatted decimal
representation of a binary number which can't be 0.0003.

-- 
vlm


More information about the erlang-questions mailing list