Qua*_*noi 12 language-agnostic math floating-point rounding ieee-754
假设我执行此操作:
(X / const) * const
Run Code Online (Sandbox Code Playgroud)
使用双精度参数定义IEEE 754-2008,首先划分,然后乘法.
const在范围内0 < ABS(const) < 1.
假设操作成功(没有溢出),X这个操作的不同参数是否保证返回不同的结果?
换句话说,还有什么X1,X2并0 < ABS(const) < 1让X1 <> X2,但(X1 / const) * const = (X2 / const) * const?
是.
public class TestDoubleDivision
{
public static void main(String[] args)
{
final Random random = new Random();
int i = 0;
while (i < 10)
{
final double c = random.nextDouble();
final double x1 = 10.0 * random.nextDouble();
final double x2 = nextDouble(x1);
if (x1 / c * c == x2 / c * c)
{
System.out.printf("x1 = %.20f, x2 = %.20f, c = %.20f\n", x1, x2, c);
i++;
}
}
}
private static double nextDouble(double d1)
{
return Double.longBitsToDouble(Double.doubleToLongBits(d1) + 1);
}
}
版画
x1 = 5.77383813703796800000, x2 = 5.77383813703796900000, c = 0.15897456707659440000 x1 = 2.97635611350670850000, x2 = 2.97635611350670900000, c = 0.15347615678619309000 x1 = 7.98634439050267450000, x2 = 7.98634439050267500000, c = 0.83202322046715640000 x1 = 0.11618686267768408000, x2 = 0.11618686267768409000, c = 0.09302449134082225000 x1 = 0.98646731978098480000, x2 = 0.98646731978098490000, c = 0.40549842805620606000 x1 = 3.95828649870362700000, x2 = 3.95828649870362750000, c = 0.75526917984495820000 x1 = 1.65404856207794440000, x2 = 1.65404856207794460000, c = 0.14500102367827516000 x1 = 5.72713430182017500000, x2 = 5.72713430182017550000, c = 0.68241935505532810000 x1 = 3.71143195248990980000, x2 = 3.71143195248991000000, c = 0.21294683305890750000 x1 = 5.66441726170857800000, x2 = 5.66441726170857900000, c = 0.69355199625947250000