I wonder how calculators work with precision. For example, the value is sin(M_PI)not zero when calculated exactly double:
#include <math.h>
#include <stdio.h>
int main() {
double x = sin(M_PI);
printf("%.20f\n", x);
return 0;
}
Now, of course, I want to print zero when the user enters sin (π). I can easily get around somewhere on 1e-15 so that this particular case works, but it's a hack, not a solution. When I start spinning like this and the user enters something like 1e-20, they get a null result (due to rounding). The same thing happens when the user enters 1/10 and presses the key several times =- when he reaches rounding, he gets zero.
Still, some calculators return zero for sin (π), and at the same time, they can conveniently work with expressions (1e-20) / 10. Is there a trick?