0

I have some ArrayList and I want to convert the values of all element of those ArrayList into BigDecimal because double datatype produce unprecise result. I use temporary variable to store the result of the calculation the I assign the values of those temporary variable into the ArrayList. How do I get the precise value of singleSmoothing,doubleSmoothing, and finalForecast?

ArrayList<Double> price = new ArrayList(); 
ArrayList<Double> singleSmoothing = new ArrayList();
ArrayList<Double> doubleSmoothing = new ArrayList();
ArrayList<Double> finalForecast = new ArrayList()
double temporarySingle,temporaryDouble,finalTemporary = 0;
double alpha , beta =0;

for(i = 1; i < price.size(); i++){

temporarySingle = alpha * price.get(i) + (1 - alpha) * (singleSmoothing.get(i-1)+doubleSmoothing.get(i-1));

temporaryDouble = beta * (temporarySingle - singleSmoothing.get(i-1)) + (1 beta) * doubleSmoothing.get(i-1) ;

singleSmoothing.add(temporarySingle);
doubleSmoothing.add(temporaryDouble);
finalTemporary = singleSmoothing.get(i) + doubleSmoothing.get(i);
finalForecast.add(finalTemporary);

}        
  • 3
    Once your data has been stored as a double, you've probably already lost some precision. Are you reading these from a database (in which case a decimal can map to a BigDecimal), from XML/JSON/YAML (where they should be stored as strings and then mapped to BigDecimal). If you're reading from a binary source which is already storing doubles, you've already lost precision. I think you're asking the wrong question. – Alex Taylor May 04 '16 at 04:50
  • I agree, that if you need to use BigDecimal at the first place. If this is not possible, then instead of doing math with doubles, you should first convert to BigDecimal and then do the math. – nomadus May 04 '16 at 04:57
  • Concur. Because you're not doing any divisions, you can get fully exact values, *if* the initial value is also a `BigDecimal`. – Andreas May 04 '16 at 04:58
  • Of course, you also need to fix the compile error caused by `(1 beta)`. – Andreas May 04 '16 at 04:59
  • so what should I do to get the precise result? I have 108 record of data for your information. Sorry I am a noob – Syahrul Sagecode May 04 '16 at 05:00
  • @SyahrulSagecode Depends. Where does `price` come from? See first comment (the one by Alex Taylor). – Andreas May 04 '16 at 05:01
  • thanks @nomadus ! I think your answer can solve my problem – Syahrul Sagecode May 04 '16 at 05:02
  • from database @AlexTaylor. I retrive the values from database then I store them into ArrayList – Syahrul Sagecode May 04 '16 at 05:04
  • Using a ResultSet directly, you can use getBigDecimal ( https://docs.oracle.com/javase/7/docs/api/java/sql/ResultSet.html#getBigDecimal(java.lang.String) ). – Alex Taylor May 04 '16 at 05:29
  • If you get data from a database, don't use Double as intermediate. What you gain by using BigDecimal (multiple precision, exact representation of decimal values) is lost as soon as you use Double. Use BigDecimal from the start. – Rudy Velthuis May 04 '16 at 09:03

0 Answers0