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


Java Java.lang.StrictMath.hypot()用法及代碼示例


描述

這個java.lang.StrictMath.hypot() 方法返回sqrt(x2 + y2)沒有中間溢出或下溢。它包括某些情況 -

  • 如果任一參數為無窮大,則結果為正無窮大。
  • 如果任一參數為 NaN 且兩個參數都不是無限的,則結果為 NaN。

聲明

以下是聲明java.lang.StrictMath.hypot()方法

public static double hypot(double x, double y)

參數

  • x- 這是要使用的值。

  • y- 這是要使用的值。

返回值

此方法返回 sqrt(x2+ y2) 沒有中間溢出或下溢。

異常

NA

示例

下麵的例子展示了 java.lang.StrictMath.hypot() 方法的用法。

package com.tutorialspoint;

import java.lang.*;

public class StrictMathDemo {

   public static void main(String[] args) {

      double d1 = 3, d2 = 4, d3 = 6;
   
      // returns sqrt(x2 + y2)
      double hypotValue = StrictMath.hypot(d1, d2); 
      System.out.println("hypotenuse value of d1 and d2 = " + hypotValue);
        
      hypotValue = StrictMath.hypot(d2, d3); 
      System.out.println("hypotenuse value of d2 and d3 = " + hypotValue);
   }
}

讓我們編譯並運行上麵的程序,這將產生以下結果 -

hypotenuse value of d1 and d2 = 5.0
hypotenuse value of d2 and d3 = 7.211102550927978

相關用法


注:本文由純淨天空篩選整理自 Java.lang.StrictMath.hypot() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。