Omnimaga
General Discussion => Technology and Development => Computer Programming => Topic started by: BlakPilar on October 08, 2011, 05:42:13 pm
-
First off, I wasn't sure whether to put this in Math and Science or here, but I'm putting it here.
In .NET, Math.Tan returns the tangent of some number in radians. However, I need the tangent of that number in degrees. I wrote myself a little method, but for some reason, it's not producing the correct number. I take the degrees in radians, multiply them by pi, then divide by 180.
When I put in 30.0, I should get 0.577350269... (according to the calculator) but I'm getting 0.523598775... Am I doing something wrong, or is it an error by the computer?
Full method:
public double RadToDeg(double rad) {
double deg = ((rad * Math.PI) / (double)180);
return deg;
}
EDIT: I also tried moving around the parenthesis so it was "rad * (Math.PI / (double)180)," but there was no change.
-
is this c++?
-
C#
-
I don't know much about c#, so the only suggestion I could give you is maybe to float the numbers?
probably won't do anything, but I guess it's worth a try. :)
-
Doubles are more precise floating-point numbers (doubles = 15 sig digs, floats = 7 sig digs), so not sure if that would help.
Edit: Nope, doesn't change it.
-
I'm pretty sure the problem is that your conversion factor is inverted. Degrees = radians * 180 / pi.
-
Yeah, but when I do that I get 1718.873385... ???
-
is your calc in the right mode?
(just a guess)
-
I'm pretty sure the problem is that your conversion factor is inverted. Degrees = radians * 180 / pi.
Actually, he seems to be converting from degrees to radians (30 to π/6). I'm guessing his routine is just marked wrong.
-
@parser, check the category :P this is computer languages help.
@Deep, no, I want radians to degrees, but the result is over 1000 when I do it the right way.
-
But you're saying you want to type in 30 and get 0.577, which is degrees. You want to convert degrees to radians, then put it in Math.Tan.
-
@parser, check the category :P this is computer languages help.
yeah but you said that your answer on calc differed from your comp.
-
@parser, check the category :P this is computer languages help.
yeah but you said that your answer on calc differed from your comp.
The calculator is probably right. He's having trouble writing a math function in C#.
-
@parser, check the category :P this is computer languages help.
yeah but you said that your answer on calc differed from your comp.
The calculator is probably right. He's having trouble writing a math function in C#.
sorry my bad.
-
@parser, check the category :P this is computer languages help.
yeah but you said that your answer on calc differed from your comp.
Like Deep said lol. I did tan(30(degree symbol)) to be sure :P
But I got it now.
-
Actually, you have to convert 30 degrees in radians, then give the result to Math.tan. Seems you were doing it wrong.
-
Yeah, I realized it after some talk in IRC lol. 'Tis all good now :)
-
So what was the problem?
-
I had the method names switched (I also had a degrees to radians one), and I did "MyFunction(Math.Tan())" instead of "Math.Tan(MyFunction())."