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

Tra*_*ell 0 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;
    this.y = input +1;
    this.checksOut = checksOut;
}

@Test
public void test() {
    boolean biggerThanY = y <= checksOut;
    boolean smallerThanX = x >= checksOut;
    assertTrue(biggerThanY || smallerThanX);
}
Run Code Online (Sandbox Code Playgroud)

如何使用原始浮点数而不是整数重写此测试,以便找不到任何未通过测试的输入checksOut值?

Pat*_*han 5

方法java.lang.Math.nextUp(float f)返回"在正无穷大方向上与f相邻的浮点值".