Re: sin (M_PI)



Adam <adam@xxxxxxxxxxxxxx> wrote:
I am using the following code:
printf("%g", sin(M_PI))
and getting 1.22461e-16 instead of zero.
Does anyone have any idea why, and what I can do about it?

Pi is an irrational number, i.e. you can't write it down
exactly without giving an infinite number of digits (what-
ever number system you use). Computers can't deal with
irrational numbers very well (i.e. at all) since in a
computer you can only store a finite number of digits.
Thus what you use for pi is never the true value of pi
but an approximation. So you can't expect more that an
approximation of the value of sin of pi when you use an
approximation of pi to start with.

Whenever you use floating point numbers you must consider
that the computer only stores a limited number of (binary)
digits. In many cases the number of digits that can be
stored is less than the true number of digits of the
floating point number - rather obvious for irrational
numbers like pi, but even a simple looking number like 0.1
becomes a number with infinitely many digits when expressed
in binary (like 1/3 has an infinite number of digits when
written in decimal). Thus in many (if not most) cases you
do calculations with approximations only. And that's not
the end of it. E.g. function like sin are computed using
(clever) approximate algorithms (which tend to be a limi-
ted number of summands of an infinite sum). So you do ap-
proximate calculations of functions on approximate values.
And the result is, of course, that you end up with more or
less good approximate results. And when you think of it,
1e-16 is a rather good approximation for a 0 when you con-
sider the range [-1:1] of the sin function.

Thus if you do floating point calculations you have to be
aware of the approximate nature of such computations. And
you have to be aware that small errors due to the approxi-
mations can sum up considerably - if you aren't careful
you can end up with completely bogus results.

Regards, Jens
--
\ Jens Thoms Toerring ___ jt@xxxxxxxxxxx
\__________________________ http://toerring.de
.



Relevant Pages

  • Re: Excellent accuracy in the HP50G GAMMA function--how do they do it?
    ... Stirling's approximation, truncated at the fourth or fifth term, is ... digits accuracy (in a calculating environment that allows for several ... more guard digits). ... the code internall that keeps roundoff error down to size so it is ...
    (comp.sys.hp48)
  • Re: Bit fiddling calculating fraction
    ... a)Finding a best rational approximation, with a certain maximum numerator ... and denominator, to a constant that is irrational. ...
    (comp.lang.c)
  • Re: int64 or double
    ... Integers are exact. ... Home of Chuck's Poker Libraries for Delphi, ... Note that while a Double can have in excessive 300 digits of "precision", ... really only an approximation, of which the first 16 digits or so are ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Pi Day Exercise
    ... 22/7 only needs 3 digits, while 355/113 needs a prodigious act of memory on SIX whole digits. ... In a sense, that is an arbitrary denominator, one chosen just because of our predilection for base 10 numbers. ... When you instead choose a denominator designed to give you a good approximation -- even if you use fewer digits -- then chances are good you can find a better approximation than that arbitrary one. ... Math in alternate bases is time consuming in the Real World. ...
    (rec.arts.sf.science)
  • Re: FAQ 4.2 Why is int() broken?
    ... To express 1/10 in binary you need an infinite ... number of digits, just like you need an infinite number of digits ... Even among different types of computers floating point calculations are ...
    (comp.lang.perl.misc)