Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我如何找到java浮点值y,它大于浮点值x,中间没有可表示的数字?_Java_Floating Point - Fatal编程技术网

我如何找到java浮点值y,它大于浮点值x,中间没有可表示的数字?

我如何找到java浮点值y,它大于浮点值x,中间没有可表示的数字?,java,floating-point,Java,Floating Point,对于整数,这项任务很简单 private int x; private int y private int checksOut; public TestCase(int input, int checksOut) { if (input == Integer.MAX_VALUE) { throw new IllegalArgumentException("can't increment MAX_VALUE"); } this.x = input;

对于整数,这项任务很简单

private int x;
private int y
private int checksOut;

public TestCase(int input, int checksOut) {
    if (input == Integer.MAX_VALUE) {
        throw new IllegalArgumentException("can't increment MAX_VALUE");
    }
    this.x = input;
    this.y = input +1;
    this.checksOut = checksOut;
}

@Test
public void test() {
    boolean biggerThanY = y <= checksOut;
    boolean smallerThanX = x >= checksOut;
    assertTrue(biggerThanY || smallerThanX);
}
private int x;
私家侦探
私人支票;
公共测试用例(int-input,int-checksOut){
if(输入==整数.MAX_值){
抛出新的IllegalArgumentException(“无法增加最大值”);
}
这个.x=输入;
y=输入+1;
this.checksOut=checksOut;
}
@试验
公开无效测试(){
布尔biggerThanY=y=checksOut;
资产真实性(biggerThanY | | smallerThanX);
}
如何使用基本浮点而不是整数重写此测试,以便我找不到任何未通过测试的输入和检查值对?

您使用
Math.ulp(float)
(或者对于
double
使用
Math.ulp(double)

从Javadoc:

返回参数的ulp大小。双精度值的ulp(最后一位的单位)是该浮点值与次大的双精度值之间的正距离。请注意,对于非NaN x,ulp(-x)=ulp(x)

您可以使用
Math.ulp(float)
(或者对于
double
使用
Math.ulp(double)

从Javadoc:

返回参数的ulp大小。双精度值的ulp(最后一位的单位)是该浮点值与次大的双精度值之间的正距离。请注意,对于非NaN x,ulp(-x)=ulp(x)


该方法返回“在正无穷方向上与f相邻的浮点值”。

该方法返回“在正无穷方向上与f相邻的浮点值”。

float
对于介于两个大
int
值之间的值,精度不够。您可以尝试使用
double
float
对于介于两个大
int
值之间的值来说精度不够。你可以试试双倍。