Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Basically, it looks like this is expected behavior from <code>Math.Tan</code>. I don't know other languages very well, so I'm not sure if this is normal for floating point Math or specific to the C# implementation. (NOTE: Afterwards, I found that Google's online calculator returns the same suggesting it expected behavior for floating point trig functions, probably related to the fact that <em>pi</em> is irrational and the limitations of the double precision data type)</p> <p>However, working backwards from this result I am seeing that <code>Math.Atan(// your result);</code> and <code>Math.Atan(double.PositiveInfinity)</code> both return 90 degrees, suggesting this is expected?</p> <p>Here's my test:</p> <pre><code>var deg = 90.0; var rads = deg * (Math.PI / 180); var result = Math.Tan(rads); if (Double.IsInfinity(result)) Console.WriteLine("Tan of 90 degrees is Infinity"); else if (Double.IsNaN(result)) Console.WriteLine("Tan of 90 degrees is Undefined"); else Console.WriteLine("Tan of 90 degrees is {0}", result); Console.WriteLine("Arc Tan of {0} is {1} degrees", double.PositiveInfinity, Math.Atan(double.PositiveInfinity) * 180 / Math.PI); Console.WriteLine("Arc Tan of {0} is {1} degrees", result, Math.Atan(result) * 180 / Math.PI); </code></pre> <p>Which gives the output of:</p> <pre><code>Tan of 90 degrees is 1.63317787283838E+16 Arc Tan of Infinity is 90 degrees Arc Tan of 1.63317787283838E+16 is 90 degrees </code></pre> <p>So my guess is unless someone can come in and provide a workaround, you might have to program around this as an edge case to get the correct result.</p> <p>The "correct result" for any of the trig functions will be limited to the precision of <code>double</code>, which is 15 significant figures, so if you need more than that, you will need to find a library that supports more precise mathematics.</p> <p>Since <code>Math.Tan(Math.PI/2)</code> seems to provide an undesirable response you could do something like this:</p> <pre><code>public double ComputeTangent(double angleRads) { if (angleRads == Math.PI/2) return double.PositiveInfinity if (angleRads == - Math.PI/2) return double.NegativeInfinity return Math.Tan(angleRads); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload