2

I am trying to establish some concise overview of what options for precise caluclations we have in JAVA+SQL. So far I have found following options:

  1. use doubles accepting their drawbacks, no go.
  2. use BigDecimals
    • using them in complicated formulas is problematic for me
  3. use String.format/Decimal.format to round doubles
    • do i need to round each variable in formula or just result to get BigDecimal precision?
    • how can this be tweaked?
  4. use computed fields option in SQL.
    • drawback is that I'd need dynamic SQL to pull data from different tables + calculate fields on other calculated fields and that would get messy

any other options?

Problem statement: I need precise financial calculations that would involve using very big (billions) and very small numbers (0.0000004321), and also dividing values that are very similar to each other, so for sure I need precision of BigDecimal.

On the other side, I want to retain ease of use that doubles have in functions (i work on arrays from decimal SQL data), so calculations like: (a[i] - b[i])/b[i] etc. etc. that are further used in other calculations. and I'd like to have users to be able to desing their own formulas as they need them (using common math statements)

i am keen to use "formatting" solution for String.format, but this makes code not very readable ( using String.format() for each variable...).

Many thanks for suggestion of how to deal with the stuff.

2
  • This might help ... stackoverflow.com/questions/8636228/…
    – Maxx
    Apr 26, 2013 at 10:39
  • You requirements are broken by design, go back to the analysis phase. Any financial stuff where you have no fixed, mandatory and exhaustive definition when and how to apply rounding is guaranteed to be perceived as calculating wrong by the first or second guy to use it.
    – Durandal
    Apr 26, 2013 at 13:39

4 Answers 4

2

There is nothing you can do to avoid floating point erros in float and double. No free cheese here - use BigDecimal.

2
  • @Eugene, ok thanks for that remark, but let's challenge BigDecimals then. What would be needed to get example code below to work? // i know this is not right syntax but I would like to have it this way ;) BigDecimal a = 1234.59053232; BigDecimal b = 1.000054530; BigDecimal c; c = a/b; //or any other formula with 'human' friendly syntax? and on the output get accurate value of c with given precision of let's say 4 digits?
    – mtx
    Apr 26, 2013 at 12:48
  • @mtx you can 'hide' this manipulations on a BigDecimal. Common math statements could be mapped to an ENUM, and provide operations like : DIVIDE,SQUARE, etc. You can't have human syntax on a non-human BigDecimal type. Also in your particular example, the divide operation would look like this a.divide(b,4,RoundingMode.HALF_UP)
    – Eugene
    Apr 26, 2013 at 13:00
2

From Effective Java (2nd ED):

Item 48: Avoid float and double if exact answers are required

  • Float and double do not provide exact results and should not be used where exact results are required. The float and double types are particularly ill-suited for monetary claculations because is impossible to represent 0.1 (or any other negative power of ten) as a float or double exactly.

  • The right way to solve this problem is to ouse BigDecimal, int, or long for monetary calculations.

...

An alternative is to use int or long and to keep track of the decimal point yourself.

0

There is no way to get BigDecimal precision on a double. doubles have double precision.

If you want to guarantee precise results use BigDecimal.

You could create your own variant using a long to store the integer part and an int to store the fractional part - but why reinvent the wheel.

Any time use doubles you stand to stuffer from double precision issues. If you use them in a single place you might as well use them everywhere.

Even if you only use them to represent data from the database then will round the data to double precision and you will lose information.

0

If I understand your question, you want to use Data Types with more precision than the native Java ones without loosing the simple mathematical syntax (e.g. / + * - and so on). As you cannot overload operators in Java, I think this is not possible.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.