Omnimaga

General Discussion => Technology and Development => Computer Programming => Topic started by: BlakPilar on October 08, 2011, 05:42:13 pm

Title: Is it my fault, or the computer's?
Post 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:
Code: [Select]
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.
Title: Re: Is it my fault, or the computer's?
Post by: parserp on October 08, 2011, 05:56:04 pm
is this c++?
Title: Re: Is it my fault, or the computer's?
Post by: BlakPilar on October 08, 2011, 06:00:48 pm
C#
Title: Re: Is it my fault, or the computer's?
Post by: parserp on October 08, 2011, 06:02:09 pm
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. :)
Title: Re: Is it my fault, or the computer's?
Post by: BlakPilar on October 08, 2011, 06:04:32 pm
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.
Title: Re: Is it my fault, or the computer's?
Post by: Runer112 on October 08, 2011, 06:05:36 pm
I'm pretty sure the problem is that your conversion factor is inverted. Degrees = radians * 180 / pi.
Title: Re: Is it my fault, or the computer's?
Post by: BlakPilar on October 08, 2011, 06:07:16 pm
Yeah, but when I do that I get 1718.873385... ???
Title: Re: Is it my fault, or the computer's?
Post by: parserp on October 08, 2011, 06:08:50 pm
is your calc in the right mode?
(just a guess)
Title: Re: Is it my fault, or the computer's?
Post by: Deep Toaster on October 08, 2011, 06:10:09 pm
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.
Title: Re: Is it my fault, or the computer's?
Post by: BlakPilar on October 08, 2011, 06:11:56 pm
@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.
Title: Re: Is it my fault, or the computer's?
Post by: Deep Toaster on October 08, 2011, 06:13:04 pm
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.
Title: Re: Is it my fault, or the computer's?
Post by: parserp on October 08, 2011, 06:13:15 pm
@parser, check the category :P this is computer languages help.
yeah but you said that your answer on calc differed from your comp.
Title: Re: Is it my fault, or the computer's?
Post by: Deep Toaster on October 08, 2011, 06:18:54 pm
@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#.
Title: Re: Is it my fault, or the computer's?
Post by: parserp on October 08, 2011, 06:20:01 pm
@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.
Title: Re: Is it my fault, or the computer's?
Post by: BlakPilar on October 08, 2011, 06:23:04 pm
@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.
Title: Re: Is it my fault, or the computer's?
Post by: Juju on October 08, 2011, 06:28:59 pm
Actually, you have to convert 30 degrees in radians, then give the result to Math.tan. Seems you were doing it wrong.
Title: Re: Is it my fault, or the computer's?
Post by: BlakPilar on October 08, 2011, 06:31:19 pm
Yeah, I realized it after some talk in IRC lol. 'Tis all good now :)
Title: Re: Is it my fault, or the computer's?
Post by: Deep Toaster on October 09, 2011, 01:30:01 am
So what was the problem?
Title: Re: Is it my fault, or the computer's?
Post by: BlakPilar on October 09, 2011, 10:53:19 am
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())."