sin() - unexpected results?

Hey,
I'm working on a project where I'll be writing many PCM .wav files. Each .wav file is of a sine wave with varying frequency.
I'm using the sin() function found in <cmath> to emulate the sine waves, however, I've been getting unexpected results, so I've written a small test program to confirm:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <cmath>

const double pi = 3.1415926535;


int main() {
	double param=180.0;
	double result=sin(param*pi/180);

	std::cout << "The sine of " << param << " degrees is " << result << "." << std::endl;
	std::cin.get();
	return 0;
}


My problem is this: When the sine should be 0 (abs(cosine)=1, you get the idea), it gives me trash values like -3.5917e-010. I know it's something tiny I'm missing, probably because I'm a silly goose.

Thanks for any help.
You need to watch your domain for input values. The trigonometric functions don't necessarily adjust for stuff outside of 0..pi, IIRC. [edit]Well, that domain for the sine function, anyway.
Last edited on
> When the sine should be 0 (...), it gives me trash values like -3.5917e-010
There isn't so much difference.
Read about machine precision.
Ah yes, it was just me being a silleh goose. It was precision related.
Thank you Duoas and ne555.
Duoas wrote:
You need to watch your domain for input values. The trigonometric functions don't necessarily adjust for stuff outside of 0..pi, IIRC.


Are you sure about that? I find that very hard to believe.

sin/cos loop to form a sine wave... and sin(4*pi) should == sin(0). If C++ does not guarantee that, I would be very, very surprised.
Disch is right.

sin & cos will take any real value & produce a result.

But the inverse of these need values in the interval [-1,+1].

Maybe that was what Duoas was thinking of ?
Topic archived. No new replies allowed.