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


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


java.lang.Math.tan()返回角度的三角正切。

  • 如果參數為NaN或無窮大,則返回的結果為NaN。
  • 如果自變量為零,則結果為零,其符號與自變量相同。
  • 用法:

public static double tan(double angle)
Parameters:
The function has one mandatory parameter angle which is in radians. 

返回值:
該函數返回角度的三角正切。


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

// Java program to demonstrate working 
// of java.lang.Math.tan() 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.tan(b)); 
  
        a = 45; 
          
        // converting values to radians 
        b = Math.toRadians(a); 
  
        System.out.println(Math.tan(b)); 
  
        a = 60; 
          
        // converting values to radians 
        b = Math.toRadians(a); 
  
        System.out.println(Math.tan(b)); 
  
        a = 0; 
          
        // converting values to radians 
        b = Math.toRadians(a); 
  
        System.out.println(Math.tan(b)); 
    } 
}

輸出:

0.5773502691896257
0.9999999999999999
1.7320508075688767
0.0

範例2:在參數為NAN或無窮大時顯示java.lang.Math.tan()方法的用法。

// Java program to demonstrate working 
// of java.lang.Math.tan() method infinity case 
import java.lang.Math; 
  
public class GFG { 
    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.tan(negativeInfinity); 
        System.out.println(result); 
  
        // Here argument is positive infinity,  
        // output will also be NaN 
        result = Math.tan(positiveInfinity); 
        System.out.println(result); 
  
        // Here argument is NaN, output will be NaN 
        result = Math.tan(nan); 
        System.out.println(result); 
    } 
}

輸出:

NaN
NaN
NaN


相關用法


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