954,492 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Simple sin() problem

I hate to be spamming the forums with this, but I've been banging my head at the wall for a few hours now, and I can't really understand what is going on. Here's the full program I'm running:

#include <iostream>
#include "math.h"

int main() {
        std::cout << sin (3.141592653589793) << "\n";
}

The program is returning 1.22461e-16.
Is this simply because I'm not using enough digits of pi? Then why does cos work flawlessly? Any help is greatly appreciated.

Massena
Newbie Poster
5 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 

It's not about the digits in your pi. It's just the way sin() function works, not much you can do about it. If you really want the 0.0, write an if :P

Topi Ojala
Junior Poster in Training
66 posts since May 2009
Reputation Points: 13
Solved Threads: 20
 

1.22461e-16 is equal to 0.000000000000000122461, which is pretty close to 0 if you ask me. This is just caused by the precision of the double type and how the data is stored. You could also round the number to remove that problem.

William Hemsworth
Posting Virtuoso
1,591 posts since Mar 2008
Reputation Points: 1,429
Solved Threads: 129
 

It's in the nature of finite precision.
Theres not much you can do about it.

there a basicly two kinds of datatypes.
1. integral
2. float

integral being, int, char, byte etc
floats being, float, double

integral is always precise, floating values are more obscure

What numerical libraries do is to check for equality within a tolerance like

float one = 0;
float two = 0.0000001

 //wrong
if(one!=two)
  puts("not same");

//right
#define tole 0.000001
if(fabs(one-two)>tole)
  puts("not same")


If you need it depends on your context,
if you just want the value, then you shouldn't care.

But if your program has a branchpoint depending on this value,
you should go the "within tolerance" way.

on a sidenote.
floats are only precise to the 7th or 8th digit,
whereas double are to the 23thd

good luck

monkey_king
Junior Poster
160 posts since Aug 2008
Reputation Points: 70
Solved Threads: 9
 

May be you still remember (from a school math course) that no fractions which are equal to pi number.
Other post correction: double type provide ~16 decimal digits precision (53 bits of mantissa).

ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
 

And you should really include instead.

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

Your output is expected, and here's the result of the same when I tried it on Wolfram's Online Math Engine.

Here : The sin() function prototypes

amrith92
Junior Poster
188 posts since Jul 2008
Reputation Points: 130
Solved Threads: 23
 

Ah I see. It's just odd that the cosine of an approximation of pi does return -1, but it's probably just something about the way the math library works.
Anyhow, thanks all!

Massena
Newbie Poster
5 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 

>It's just odd that the cosine of an approximation of pi does return -1
Exactly!
As usually, math libraries (and FPU hardware) implement high precision (~13-15 decimal digits) polynomial approximations of common math functions.

ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You