Skip to content
Tcl Source Code View Ticket
Login

View Ticket

expr 52 * 0.001 = 0.052000000000000005

Closed / Invalidb8e9a9a97f0d8be15bd549239ec89aa58c11f3f4Created 8 years agoUpdated 8 years ago
Descriptionplain2019-03-08 23:00:38
It seems tcl needs to consider how to handle math correctly. See the math example below. It will mess up the results in most of the cases, as long as the user does some simple math.

We know it is a floating point number problem for computer and many computer languages. But it is still good to have a better solution than "set tcl_precision 12".

52 * 0.001 = 0.052000000000000005
0.52 * 0.01 = 0.005200000000000001
0.1 + 0.2 = 0.30000000000000004

I can reproduce this issue in Tcl 8.6.8 for Windows and Tcl 8.5.9 for Windows.

(bin) 1 % expr 1 + 3
4
(bin) 2 % expr 52 + 3
55
(bin) 3 % expr 52 * 0.01
0.52
(bin) 4 % expr 52 * 0.001
0.052000000000000005
(bin) 5 % expr 0.52 * 0.01
0.005200000000000001


(bin) 8 % expr 0.1 + 0.2
0.30000000000000004
anonymous Ticket [b8e9a9a97f]: 3 changes [History diff]2019-03-08 23:23:49
anonymous Ticket [b8e9a9a97f]: 3 changes [History diff]2019-03-08 23:34:41
anonymous Ticket [b8e9a9a97f]: 3 changes [History diff]2019-03-08 23:47:17
bllplain2019-03-11 06:35:57
Seems to be right.
format is your friend.

bll-tecra:bll$ cat t.c
#include <stdio.h>
#include <stdlib.h>

main () {
  double a = 0.1;
  double b = 0.2;
  printf ("%.20g\n", a + b);
}

bll-tecra:bll$ ./t
0.30000000000000004441
jan.nijtmansplain2019-03-12 20:42:26
Normal behavior, so closing as "invalid"
Log in (anonymous is fine) to add a comment.