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


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


描述

這個java.lang.StrictMath.ulp(float f) 方法返回參數的 ulp 的大小。浮點值的 ulp 是此浮點值與大小上次大的浮點值之間的正距離。請注意,對於非 NaN x,ulp(-x) == ulp(x).它包括這些情況 -

  • 如果參數為NaN,則結果為NaN。
  • 如果參數為正無窮大或負無窮大,則結果為正無窮大。
  • 如果參數為正零或負零,則結果為 Float.MIN_VALUE。
  • 如果參數是±Float.MAX_VALUE,則結果等於2104

聲明

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

public static float ulp(float f)

參數

f- 這是要返回其 ulp 的浮點值。

返回值

此方法返回參數的 ulp 的大小。

異常

NA

示例

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

package com.tutorialspoint;

import java.lang.*;

public class StrictMathDemo {

   public static void main(String[] args) {

      float f1 = 3.71f , f2 = -4.3f, f3 = 0.0f;
   
      // returns the size of an ulp of the argument      
      float ulpValue = StrictMath.ulp(f1); 
      System.out.println("Size of ulp of " + f1 + ":" + ulpValue);

      ulpValue = StrictMath.ulp(f2); 
      System.out.println("Size of ulp of " + f2 + ":" + ulpValue);

      ulpValue = StrictMath.ulp(f3); 
      System.out.println("Size of ulp of " + f3 + ":" + ulpValue);
   }
}

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

Size of ulp of 3.71 = 2.3841858E-7
Size of ulp of -4.3 = 4.7683716E-7
Size of ulp of 0.0 = 1.4E-45

相關用法


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