views:

144

answers:

4

Hello! 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  A: 

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.

tloach
+2  A: 

Calculators use arbitrary precision math libraries. Those can be configured to have much higher precision that double.

If you want to print exactly zero, use width specifier

printf (%12.4d, number);
buratinas
Can @~buratinas provide supporting evidence for the claim that calculators use arbitrary precision math libraries ? I think this is just plain wrong but I'm open to evidence that I am wrong.
High Performance Mark
Depends on the calculator. For instance, Mathematica calculates in arbitrary precision (as stated by its documentation e.g. the Numeric Precision tutorial). I'm sure there are other calculators that do the same, as well as some that use high-precision (but not arbitrary precision) libraries and perhaps some that just do regular floating-point math.
David Zaslavsky
@David: Depends on the definition of 'calculator' then. I think calling Mathematica (or Maple or GMP etc) a calculator is very odd, I call my HP48SX a calculator.
High Performance Mark
@Mark: Well, the question is asking about software calculators, and Mathematica is just a generalization of the same concept. Regardless of how you define "calculator," though, it's likely that different ones will use different precisions.
David Zaslavsky
@High Performance Mark: Default calculator on ubuntu uses arbitrary precision math library thus I chose to generalize the idea.
buratinas
+1  A: 

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
mouviciel
BCD is not really more accurate than binary; the advantage is that it mimics pencil-and-paper calculations, and therefore you get the exact same accuracy or inaccuracy.
David Thornley
With binary, there is one source of inaccurracy more than BCD: this is the conversion of the result from base-2 to base-10.
mouviciel
A: 

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) :)

Luke404