Subtraction of floating point numbers in python and c doesn't behave the same, when I try "0.3 - 0.1", c tells me 0.2 and python tells me 0.199999...98.
I wrote the following c code:
// a.c
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
double a, b;
a = strtod(argv[1], NULL);
b = strtod(argv[2], NULL);
printf("%lf, %lf, %lf\n", a, b, a - b);
return 0;
}
and i calculate 0.3 - 0.1
> gcc a.c -O3
> ./a.out 0.3 0.1
0.300000 0.100000 0.200000
looks good so far.
but when i calculate in python:
# idle
>>> 0.3 - 0.1
0.19999999999999998
why? Why do the two behave differently. Is there any way to improve Python? Or the authors of Python have tried their best?