java ulp_Java StrictMath ulp()用法及代码示例

ulp(double num)

ulp(double num)是java中StrictMath类的内置方法,用于获取给定参数的ulp的大小。 double值的ulp是该浮点值与下一个更大的double值之间的正距离。

一些特殊情况是:

如果参数为NaN,则结果为NaN。

如果参数为正或负无穷大,则结果为正无穷大。

如果参数为正数或负零,则结果为Double.MIN_VALUE。

如果参数为±Double.MAX_VALUE,则结果等于2^971。

用法:

public static double ulp(double num)

参数:该方法接受一个参数num,该参数num是要返回其ulp的双精度浮点值。

返回值:ulp方法返回参数ulp的大小。

例子:

Input: num = 5.7

Output: 8.881784197001252E-16

Input: num = 0.0

Output: 4.9E-324

以下示例程序旨在说明java.lang.StrictMath.ulp()方法:

示例1:

// Java praogram to illustrate the

// java.lang.StrictMath.ulp()

import java.lang.*;

public class GFG {

public static void main(String[] args)

{

double num1 = 9.7, num2 = 4.9;

// Get the size of an ulp of the argument

// using ulp() method

double ulp_Value = StrictMath.ulp(num1);

// Print the size of the ulp

System.out.println(" The Size of ulp of "

+ num1 + " = "

+ ulp_Value);

// Get the size of an ulp of the argument

// using ulp() method

ulp_Value = StrictMath.ulp(num2);

// Print the size of the ulp

System.out.println(" The Size of ulp of "

+ num2 + " = "

+ ulp_Value);

}

}

输出:

The Size of ulp of 9.7 = 1.7763568394002505E-15

The Size of ulp of 4.9 = 8.881784197001252E-16

ulp(float num)

ulp(float num)是Java中StrictMath类的内置方法,用于获取给定参数的ulp的大小。浮点值的ulp是此浮点值与下一个幅度较大的浮点值之间的正距离。

一些特殊情况是:

如果参数为NaN,则结果为NaN。

如果参数为正或负无穷大,则结果为正无穷大。

如果参数为正数或负零,则结果为Float.MIN_VALUE。

如果参数为±Float.MAX_VALUE,则结果等于2^104。

用法:

public static float ulp(float num)

参数:该方法接受一个浮点类型num浮点值的参数num。

返回值:ulp方法返回参数ulp的大小。

例子:

Input: num = 5.7

Output: 8.881784197001252E-16

Input: num = 0.0

Output: 4.9E-324

以下示例程序旨在说明java.lang.StrictMath.ulp()方法:

示例1:

// Java praogram to illustrate the

// java.lang.StrictMath.ulp()

import java.lang.*;

public class GFG {

public static void main(String[] args)

{

float num1 = 2.7f, num2 = -4.5f;

// Get the size of an ulp of the argument

// using ulp() method

float ulp_Value = StrictMath.ulp(num1);

// Print the size of the ulp

System.out.println(" The Size of ulp of "

+ num1 + " = "

+ ulp_Value);

// Get the size of an ulp of the argument

// using ulp() method

ulp_Value = StrictMath.ulp(num2);

// Print the size of the ulp

System.out.println(" The Size of ulp of "

+ num2 + " = "

+ ulp_Value);

}

}

输出:

The Size of ulp of 2.7 = 2.3841858E-7

The Size of ulp of -4.5 = 4.7683716E-7

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值