I’m coding a calculator in C#.
textBoxResult is a text box where I display the number
recount is the function which takes the angle in degrees and returns in radians
I take the angle from texBoxInput
public double recount(int number)
{
double wyjscie = 0.0;
double Number = number;
wyjscie = Number * (Math.PI / 180);
return wyjscie;
}
//function which is called out when user presses the button:
textBoxResult.Text = Math.Round(Math.Tan(recount(Convert.ToInt32(texBoxInput.Text))),2).ToString();
As you can see I was trying to round this number when using Math.Tan, but still Math.Tan gives me that tan from 90 degrees is 1,63317787283838E+16 .
I have been trying to find the answer but failed. I can’t figure it out how to display correct result.
Basically, it looks like this is expected behavior from
Math.Tan. 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 pi is irrational and the limitations of the double precision data type)However, working backwards from this result I am seeing that
Math.Atan(// your result);andMath.Atan(double.PositiveInfinity)both return 90 degrees, suggesting this is expected?Here’s my test:
Which gives the output of:
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.
The “correct result” for any of the trig functions will be limited to the precision of
double, which is 15 significant figures, so if you need more than that, you will need to find a library that supports more precise mathematics.Since
Math.Tan(Math.PI/2)seems to provide an undesirable response you could do something like this: