For example,if you put "expr 0.1*7" in the TCL8.5, you will get the result "0.700000000001". However,we use the OpenSees2.2 which based in TCL8.5. So I want to know how to solve this bug.
Is there any updates for the TCL8.5?
How to solve this bug in TCL8.5?
Moderators: silvia, selimgunay, Moderators
Re: How to solve this bug in TCL8.5?
there is no bug .. you are seeing the result of the fact that (1) computers work in binary and we like to work in base 10 and (2) that the computer only sets aside certain number of bits to store a number .. as a result you can't do precise math in base 10 on the computer! .. some decimal numbers (0.1 is one of them!) do not even have a non-repeating representation in binary if the computer did store as many bits as needed for each number. If you have precision requirements, where you care about the number being of known precision to exactly 15 decimal digits you will need to pick another representation for your number.
here is the main function for a c program that does the same thing as your script:
#include <stdio.h>
int main(int argc, char **argv) {
double a, b, c;
a = 0.1;
b = 7;
c = 0.7;
fprintf(stderr,"%f %.17f %.18f %.18f\n",a*b, a*b, a, b);
}
if you run it you get the following:
0.700000 0.70000000000000007 0.100000000000000006 7.000000000000000000
which is what you also are seeing.
here is the main function for a c program that does the same thing as your script:
#include <stdio.h>
int main(int argc, char **argv) {
double a, b, c;
a = 0.1;
b = 7;
c = 0.7;
fprintf(stderr,"%f %.17f %.18f %.18f\n",a*b, a*b, a, b);
}
if you run it you get the following:
0.700000 0.70000000000000007 0.100000000000000006 7.000000000000000000
which is what you also are seeing.