java - new BigDecimal(double) vs new BigDecimal(String) -
this question has answer here:
when bigdecimal used input of double , bigdecimal input of string different results seem appear.
bigdecimal = new bigdecimal(0.333333333); bigdecimal b = new bigdecimal(0.666666666); bigdecimal c = new bigdecimal("0.333333333"); bigdecimal d = new bigdecimal("0.666666666"); bigdecimal x = a.multiply(b); bigdecimal y = c.multiply(d); system.out.println(x); system.out.println(y); x outputs as
0.222222221777777790569747304508155316795087227497352441864147715340493949298661391367204487323760986328125 while y is
0.222222221777777778 am wrong in saying because of double imprecision? since bigdecimal, shouldn't same?
am wrong in saying because of double imprecision?
you absolutely right, because of double's imprecision.
but since
bigdecimal, shouldn't same?
no, shouldn't. error introduced moment create new bigdecimal(0.333333333), because 0.333333333 constant has error embedded in it. @ point there nothing can fix representation error: proverbial horse out of barn then, it's late close doors.
when pass string, on other hand, decimal representation matches string exactly, different result.
Comments
Post a Comment