所以我有 BigDecimal 持有价值 0.99 我调用:
- Float.toHexString(rationalNumber.floatValue()) 我得到0x1.fae148p-1
- Double.toHexString(rationalNumber.doubleValue()) 我得到0x1.fae147ae147aep-1
我在想,因为我们代表的是一个像 0.99 这样的小数字,所以无论如何我们都应该得到相同的十六进制值。同意?
代码(未通过测试):
@Test public void testDelta() {
BigDecimal rationalNumber = new BigDecimal("0.99").setScale(2,BigDecimal.ROUND_DOWN);
String hexFromFloat = Float.toHexString(rationalNumber.floatValue());
String hexFromDouble = Double.toHexString(rationalNumber.doubleValue());
String hexFromFloatMsg = rationalNumber.floatValue() + " = " + hexFromFloat;
String hexFromDoubleMsg = rationalNumber.doubleValue() + " = " + hexFromDouble;
Assert.assertEquals(hexFromFloatMsg + ", " + hexFromDoubleMsg, hexFromDouble, hexFromFloat);
}
输出:
org.junit.ComparisonFailure: 0.99 = 0x1.fae148p-1, 0.99 = 0x1.fae147ae147aep-1
Expected :0x1.fae147ae147aep-1
Actual :0x1.fae148p-1