Search code examples
javaapache-commons-math

methods for undertaking complex calculations in Java


I have to implement some relatively complex formulas in a Java application. The inputs to these formulas are include up to 30 or so variables. Some of these variables in their natural form are integers, some are reals with a precision of around 3 to 4 decimal places. I am using commons-math for the functions, but I am wondering about precision and convenience:

Would it be best to convert all variables integers to Double prior to passing to the formula methods. This way there is a consistency within the formulas and secondly can I safely use Double for 3 to 4 dp precision? Is there a "best practice" for implementing complex math in Java?


Solution

  • There is no need to convert variables Integers to Double. Initialize all the variables as Double because if you are having a part in formula where it is just dividing 2 Integers and is added to the result of 30 variables, then it will lose the values for decimal places. So it best to use Double for mathematical purposes where Decimal places are significant to produce a correct answer.