Java Math sin() 方法和示例
Java.lang.Math.sin()返回介于 0.0 和 pi 之间的角度的三角正弦值。如果参数为 NaN 或无穷大,则结果为 NaN。如果参数为零,则结果为零,其符号与参数相同。返回的值将介于 -1 和 1 之间。
句法 :
public static double sin(double a) ;
参数:要返回其正弦值的值。
返回类型:此方法返回参数的正弦值。
执行:
在这里,我们将提出 2 个示例,其中我们将简单地展示Java.lang 包的Math.sin()方法的工作原理 方法和次要是第一个特定示例的边缘情况,其中参数是 NaN 或无穷大。
示例 1
Java
// 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));
}
}
Java
// 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);
}
}
输出:
0.49999999999999994
0.7071067811865475
0.8660254037844386
1.0
示例 2
Java
// 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