In Java, 1 << 100
is 16
, because 1
is a 32-bit int
value, so only the lower 5 bit of 100
is used.
100 = 0x64 => 0x04
, and 1 << 4 = 16
.
If you want full 101 bit precision, use BigInteger
and BigDecimal
for the calculations:
BigInteger bigInt = BigInteger.ONE.shiftLeft(100);
BigDecimal bigDec = new BigDecimal(bigInt).multiply(BigDecimal.valueOf(0.1));
double d = bigDec.doubleValue();
System.out.println("bigInt = " + bigInt);
System.out.println("bigDec = " + bigDec);
System.out.println("d = " + d);
Output
bigInt = 1267650600228229401496703205376
bigDec = 126765060022822940149670320537.6
d = 1.2676506002282295E29
Or maybe you don't use BigDecimal
:
BigInteger bigInt = BigInteger.ONE.shiftLeft(100);
double d = bigInt.doubleValue() * 0.1;
System.out.println("bigInt = " + bigInt);
System.out.println("d = " + d);
Output
bigInt = 1267650600228229401496703205376
d = 1.2676506002282295E29
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…