问题
I am developing an android app. for mathematical calculations. There I have some buttons like sin,cos,tan which should show their respective trigonometric values, for that I am using corresponding methods of java.lang.Math class and that works fine. But after some tests I came to know that, it shows some results as follows
sin(90)=0.89399........ //I want it to show 1
cos(90)=-0.44807........ //I want it to show 0
tan(90)=-1.995200...... //I want it to show Infinity as sin(90)/cos(90)=1/0=Infinity
Like the above examples there are many which show results like this. What should I do this for getting the results as mentioned above. Please help me.
回答1:
Your problem is that java uses radians and not degrees. You have to convert the angle to rad first.
Double rad = degrees /180 * pi;
回答2:
First, trigonometric functions do not work with degrees that you are using. You have to send 3.1415926/2 instead of 90. In this case sin
will be much closer to 1 and cos much closer to 0.
But the numbers will still be floating point. If you want to round them for presentation use DecimalFormat
class or String.format()
method.
回答3:
Javadoc to the rescue:
Parameters: a - an angle, in radians.
(emphasis mine)
Convert your degrees into radians first. 360 degrees = 2 * pi.
回答4:
Take a look at Math.round().
Also, last time I checked, whenever you devide by 0 you do not get infinity. Division by 0 is undefined.
Also, Java uses radians for trigonometric functions, so be sure you are using radians and not degrees or gradians.
来源:https://stackoverflow.com/questions/9096364/how-to-get-user-friendly-values-for-trigonometric-functions