java - BigDecimal multiplication returns 0? -
i'm bit of newbie @ java , have multiply 52 numbers each other ranging anywhere 0 2000. tried using *= without bigdecimal result gives me 0.0.
here code:
bigdecimal productofstock1 = bigdecimal.zero; for(int k = 1; k <= n; k++){ for(int = 1; <= n; i++){ if (i == 1){ stockprice[k][i] = stockzero*math.pow(e, form + sigma*(randomno.nextgaussian())); } else { stockprice[k][i] = stockprice[k][i-1]*math.pow(e, form + sigma*(randomno.nextgaussian())); } //sumofstock += stockprice[k][i]; //productofstock *= stockprice[k][i]; productofstock1 = productofstock1.multiply(bigdecimal.valueof(stockprice[k][i])); system.out.println(/*"stock @ [" + + "] n = " + n + " , path number " + k + " " + */stockprice[k][i]); } } system.out.println(productofstock1);
this gives me 0e-637 instead of big number supposed give me. appreciated.
bigdecimal productofstock1 = bigdecimal.zero;
you need initialize 1
, because
0 * x = 0
(except x= 1/0 :) )
Comments
Post a Comment