java - BigDecimal.add strange behavior -
method bigdecimal.add
takes long time when 1 argument has big exponent (9 digits), , second has exponent different length. i've waited more 5 minutes, , still going on , on.
here's code:
@test public void testaddbig() throws exception { mathcontext mc = new mathcontext(10, roundingmode.half_up); bigdecimal v1 = new bigdecimal("1e+100000000", mc); bigdecimal v2 = new bigdecimal("1", mc); system.out.println(v1.add(v2)); }
here's part of thread dump:
at java.math.biginteger.square(biginteger.java:1884) @ java.math.biginteger.squarekaratsuba(biginteger.java:1975) @ java.math.biginteger.square(biginteger.java:1888) @ java.math.biginteger.squaretoomcook3(biginteger.java:2011) @ java.math.biginteger.square(biginteger.java:1890) @ java.math.biginteger.squaretoomcook3(biginteger.java:2006) @ java.math.biginteger.square(biginteger.java:1890) @ java.math.biginteger.squaretoomcook3(biginteger.java:2012) @ java.math.biginteger.square(biginteger.java:1890) @ java.math.biginteger.squaretoomcook3(biginteger.java:2010) @ java.math.biginteger.square(biginteger.java:1890) @ java.math.biginteger.squaretoomcook3(biginteger.java:2006) @ java.math.biginteger.square(biginteger.java:1890) @ java.math.biginteger.squaretoomcook3(biginteger.java:2012) @ java.math.biginteger.square(biginteger.java:1890) @ java.math.biginteger.squaretoomcook3(biginteger.java:2011) @ java.math.biginteger.square(biginteger.java:1890) @ java.math.biginteger.pow(biginteger.java:2263) @ java.math.bigdecimal.bigtentothe(bigdecimal.java:3543) @ java.math.bigdecimal.bigmultiplypowerten(bigdecimal.java:4508) @ java.math.bigdecimal.add(bigdecimal.java:4443) @ java.math.bigdecimal.add(bigdecimal.java:1289)
what going on? bug?
well, answer simply. bignumbers working array of character keep precision @ maximum. since number composed of 100000000 digits, length of array.
100.000.000 chars = bytes
this 100mb if not mistaken. want math that, start lot of reading ;)
if open bigdecimal class, see there lot of checks since "letter" , not numbers.
bigdecimal usefull keep precision done @ cost, here memory , time of processing.
edit :
this problem if use instance in ways, constructor store value exponent value 1e+***. if print fine if ask numerical value, start failed.
to more precise, bigdecimal.bigtentothe(int)
receive exponent value (1000000000).
private static biginteger bigtentothe(int n) { ... char tenpow[] = new char[n + 1]; .... }
Comments
Post a Comment