Click here to Skip to main content
15,892,737 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My code:
C#
using System;

namespace PolarToCart
{
	class Program
	{
		static void Main(string[] args)
		{
			double r;
			double θ;
			double ϕ;

			string result = "";

			r = 1.0;
			θ = 2.0;
			ϕ = 3.0;

			result = Convert.ToString(
					r * Math.Sin(θ) * Math.Cos(ϕ)
				);

			Console.WriteLine(result);
			Console.ReadKey();
		}
	}
}

Gives me the result:

-0.900197629735517

However, the correct result, when it's calculated on my calculator, (and other calculators), is:

0.03485166816

I've no idea what's going on here. Does somebody have any insights?

Thank you!

What I have tried:

I've tried to experiment with parenthesizes, and my math-wiz better half, (who has had some programming experience), is also stumped. Some googling was naturally tried
Posted
Updated 25-Feb-16 8:37am

The arguments to the trigonometric functions are in radians. If your input is in degrees, you must divide by 2 * Pi:
C#
result = Convert.ToString(
            r * Math.Sin(θ / (2 * Math.PI)) * Math.Cos(ϕ / (2 * Math.PI))
        );

[EDIT]
Used thew wrong factor.
C#
result = Convert.ToString(
    r * Math.Sin(Math.PI / 180 * θ) * Math.Cos(Math.PI / 180 * ϕ)
);

[/EDIT]
 
Share this answer
 
v2
Comments
Frank R. Haugen 25-Feb-16 14:33pm    
Facepalm!, (actually a double, since the GF also felt like an idiot).

though your answer is correct, the solution is:
r * Math.Sin((Math.PI / 180) * θ) * Math.Cos((Math.PI / 180) * ϕ)

I never use radians in the math I do, (not that I do math that much), so no wonder the result was wacky.

Thank you!
Jochen Arndt 25-Feb-16 14:39pm    
Off course!
Sorry for the wrong factor. I was in a hurry when answering (had to return to the cooker).
And I'd agree with it: https://www.wolframalpha.com/input/?i=1.0+*+Sin(2.0)+*+Cos(3.0)[^]
Wolfram alpha agrees.
As does my calculator (Sharp EL-5020)

Are you sure your calculators aren't assuming that θ and ϕ are in degrees, rather than radians, like my calculator, Wolfram Alpha, and .NET?
 
Share this answer
 
v3
Comments
Frank R. Haugen 25-Feb-16 14:35pm    
Though technically not an answer, you found the flaw, I mixed up radians/degrees
The fixed code:
C#
using System;

namespace PolarToCart
{
	class Program
	{
		public double ConvertToRadians(double angle)
		{
			return (Math.PI / 180) * angle;
		}

		static void Main(string[] args)
		{
			double r;
			double θ;
			double ϕ;

			string result = "";

			r = 1.0;
			θ = 2.0;
			ϕ = 3.0;

			result = Convert.ToString(

				r * Math.Sin((Math.PI / 180) * θ) * Math.Cos((Math.PI / 180) * ϕ)
				);

			Console.WriteLine(result);

			Console.ReadKey();
		}
	}
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900