How do calculators work with precision?

拥有回忆 提交于 2021-01-02 05:48:21

问题


I wonder how calculators work with precision. For example the value of sin(M_PI) is not exactly zero when computed in double precision:

#include <math.h>
#include <stdio.h>

int main() {
    double x = sin(M_PI);
    printf("%.20f\n", x); // 0.00000000000000012246
    return 0;
}

Now I would certainly want to print zero when user enters sin(π). I can easily round somewhere on 1e–15 to make this particular case work, but that’s a hack, not a solution. When I start to round like this and the user enters something like 1e–20, they get a zero back (because of the rounding). The same thing happens when the user enters 1/10 and hits the = key repeatedly — when he reaches the rounding treshold, he gets zero.

And yet some calculators return plain zero for sin(π) and at the same time they can work with expressions such as (1e–20)/10 comfortably. Where’s the trick?


回答1:


Desktop Calculators use arbitrary precision math libraries. Those can be configured to have much higher precision that double. Handheld calculators (tradition dedicated and mobile phones) use fixed precision math libraries.

If you want to print exactly zero, use width specifier

printf (%12.4d, number);



回答2:


Some answers can be found on this Calculator Precision page.

Among solutions are:

  • work in BCD
  • use lookup tables
  • use hidden digits, so that displayed digits are accurate



回答3:


The trick is probably, as already said, that calculators will use arbitrary precision math libraries or lookup tables.

I'd also add that your code snippet works that way due to using floating point arithmetics, which as you probably know is not true math in the sense it's not precise - 1.0 + 0.1 != 1.1 (it's actually 1.1000000000000001) :)




回答4:


They may be using a lookup table to speed up their trig formulas. In that case the special numbers that work out nicely would probably just be in the table.



来源:https://stackoverflow.com/questions/2713649/how-do-calculators-work-with-precision

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!