Java Math sin()方法及实例
java.lang.Math.sin() 返回0.0和π之间的角度的三角正弦。如果参数是NaN或无穷大,那么结果就是NaN。如果参数是0,那么结果是一个与参数相同符号的0。返回的值将在-1和1之间。
语法:
public static double sin(double a) ;
参数 :要返回的正弦值。
返回类型 :该方法返回参数的正弦值。
实现
在这里,我们将提出2个例子,其中我们将简单地展示java.lang包方法中的Math.sin()方法 的工作原理,其次是第一个例子中参数为NaN或无穷大时的具体例子的边缘情况。
例子1
// Java program to demonstrate working
// of java.lang.Math.sin() method
import java.lang.Math;
class Gfg {
// driver code
public static void main(String args[])
{
double a = 30;
// converting values to radians
double b = Math.toRadians(a);
System.out.println(Math.sin(b));
a = 45;
// converting values to radians
b = Math.toRadians(a);
System.out.println(Math.sin(b));
a = 60;
// converting values to radians
b = Math.toRadians(a);
System.out.println(Math.sin(b));
a = 90;
// converting values to radians
b = Math.toRadians(a);
System.out.println(Math.sin(b));
}
}
输出
0.49999999999999994
0.7071067811865475
0.8660254037844386
1.0
例2
// Java program to demonstrate working of Math.cos() method
// of java.lang package considering infinity case
// Importing classes from java.lang package
import java.lang.Math;
public class GFG {
// Main driver method
public static void main(String[] args)
{
double positiveInfinity = Double.POSITIVE_INFINITY;
double negativeInfinity = Double.NEGATIVE_INFINITY;
double nan = Double.NaN;
double result;
// Here argument is negative infinity,
// output will be NaN
result = Math.sin(negativeInfinity);
System.out.println(result);
// Here argument is positive infinity,
// output will also be NaN
result = Math.sin(positiveInfinity);
System.out.println(result);
// Here argument is NaN, output will be NaN
result = Math.sin(nan);
System.out.println(result);
}
}
输出
NaN
NaN
NaN