gpt4 book ai didi

java - Math.nextUp(arg) 总是与 arg + Math.ulp(arg) 相同?

转载 作者:行者123 更新时间:2023-11-30 07:49:15 28 4
gpt4 key购买 nike

Math.nextUp(arg) 始终与 arg + Math.ulp(arg) 相同,还是我遗漏了什么?

System.out.println( 0.5 + Math.ulp(0.5));         // 0.5000000000000001 
System.out.println( Math.nextUp(0.5)); // EXACTLY same
System.out.println( Math.nextAfter(0.5, 1000)); // EXACTLY same

-1、-2、-0.5 是迄今为止发现的上述“规则”中唯一的排除项 (?)。此外,差异总是只打印最右边的小数位:

    System.out.println(Math.nextUp(-0.5));     // -0.49999999999999994
System.out.println(-0.5 + Math.ulp(-0.5)); // -0.4999999999999999

System.out.println(Math.nextUp(-1.0)); // -0.9999999999999999
System.out.println(-1.0 + Math.ulp(-1.0)); // -0.9999999999999998

System.out.println(Math.nextUp(-2.0)); // -1.9999999999999998
System.out.println(-2.0 + Math.ulp(-2.0)); // -1.9999999999999996

public static double ulp(double d)

Returns the size of an ulp of the argument. An ulp, unit in the last place, of a double value is the positive distance between this floating-point value and the double value next larger in magnitude.

ulp of a specific real number value is the distance between the two floating-point values bracketing that numerical value.

public static double nextUp(double d)

Returns the floating-point value adjacent to d in the direction of positive infinity.

根据 API,Math.nextUp(arg)arg + Math.ulp(arg) 对于任何 arg 应该是等价的(除了一些特殊值,例如 NaN , 无穷大, MAX_VALUE, MIN_VALUE)...

最佳答案

没有。考虑-1。 -1 的 ULP 是 2−52。区间 (−2, −1] 中的数字间隔为 2−52。但是,区间 (−1, −½] 中的数字间隔为 2−53分开。所以从 −1 开始的下一个数字是 −1+2−53,而不是 −1+2−52

关于java - Math.nextUp(arg) 总是与 arg + Math.ulp(arg) 相同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48625232/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com