當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java Math sin()用法及代碼示例


java.lang.Math.sin()返回介於0.0和pi之間的角度的三角正弦。如果參數為NaN或無窮大,則結果為NaN。如果自變量為零,則結果為零,其符號與自變量相同。返回的值將在-1和1之間。

用法:

public static double sin(double a)
Parameter:
a:the value whose sine is to be returned.

返回:
此方法返回參數的正弦值。


範例1:展示java.lang.Math.sin()方法的用法。

// 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:當參數為NaN或無窮大時,顯示java.lang.Math.sin()方法的用法。

// Java program to demonstrate working 
// of java.lang.Math.cos() method infinity case 
import java.lang.Math; 
  
public class GFG { 
      
    // Driver code 
    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


相關用法


注:本文由純淨天空篩選整理自barykrg大神的英文原創作品 Java Math sin() method with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。