Using the sin function in a program?

I've been trying to figure out how to get this program to run. It seems simple enough, I just have to take a specified ladder height of 20 feet and multiply it by sin(85) to get the ladders height at that angle.I keep getting this error code: more than one instance of overload function "sin" matches the argument list.In my textbook I'm told to use the cmath header, but what else am I missing?



# include <iostream>
# include <cmath>
using namespace std;

int main ()
{double l = 20;
double a = sin(85);
cout << " The height of 20 ft ladder at 85 degrees is " << l * a << " ft." << endl;
system ("pause");
return 0;
}


sin takes a float, double, or long double as a parameter, you compiler doesn't know which to cast (change) 85 to. http://www.cplusplus.com/reference/clibrary/cmath/sin/

double a = sin((double)85); Should fix it with an explicit cast
My teacher never taught us about float or long double. We have never even used the sin function in a program. But I still want to learn how to use it. Do I need to convert double a to degrees or is it read as degrees by the program?
Last edited on
No, it takes radians. Always refer to the function documentation before writing code. You'll need to convert your 85 degrees to radians. I personally like to have a couple of functions handy for just that purpose. Something like:

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

#ifndef M_PI
#define M_PI (4.0 * std::atan2(1.0, 1.0))
#endif

template <typename N> inline
N deg2rad( N d )
  {
  return M_PI * d / 180.0;
  }

template <typename N> inline
N rad2deg( N r )
  {
  return 180.0 * r / M_PI;
  }

Also pay attention to the domain and range of the functions you use.

Hope this helps.
It might sound dumb, but I have never seen code like that in class. I don't know if it's because it is a beginners class, or because that's how my teacher likes to do it. Here's what the program looks like after Intrexa's help.
1
2
3
4
5
6
7
8
9
10
11
# include <iostream>
# include <cmath>
using namespace std;

int main ()
{double l = 20;
double a =(sin(double(85)) * 180) / 3.14;
cout << " The height of 20 ft ladder at 85 degrees is  " << l * a << " ft." << endl;
system ("pause");
return 0;
}



I've never had to use sin in a program, so it's really kicking my butt.
Last edited on
just make your a variable
 
double a = sin(85 * (3.1415 / 180));


everything should function fine like this. all of the geometric functions in the math header take input as radians, and the formula to convert from a radian to degrees is

degrees = radian * (PI / 180)

simple :)
Last edited on
Thanks a lot man. Do you know why it didn't work the way I had it written? I followed the example from the tutorial, but in all honesty, I haven't even heard of most of the terms they use.
Last edited on
you just messed up your order of operations :o as i stated previously the formula for converting radians to degrees is to multiply by pi divided by 180. what you did was multiply by 180 and then divide by pi. very different things :o
That seems like a huge oversight on my part. Thanks for the help Ascii.
Topic archived. No new replies allowed.