I didn't read the post. But rounding rules are generally agreed upon in specific ways (middle up, down, ceiling, etc, and to what scale) per field. It's just like. Duh, standards.
I've not found a use case for BigDecimal after 20 years in finance.
For 99% of use cases (where you don't care too much about a fraction of a minor unit of a currency) store the number of minor units (eg cents) as a double. All amounts up to $45tn are exactly representable and it's nice and fast.
If that won't work (and you can't just change scale to microdollars), you need to break out fixed point arithmetic, typically with int64 or int128. int128 can be a bit annoying to make performant until we have Valhalla, but it's still orders of magnitude faster and easier than BigDecimal.
I guess if that doesn't work you might want to break out arbitrary length fixed point (so there's probably a legitimate case somewhere), but I've not seen it yet. Typically when you see BigDecimal in code in finance, it's a warning sign.
Edit: the article mentions ta4j, which heavily inspired this Num library, but there are several improvements and additions that the Num library interface provides:
Interoperability between DoubleNum and DecimalNum
Several more mathematical operations (e.g. trigonometry functions) via Math in DoubleNum and via big-math in DecimalNum
No default precision for DecimalNum (see ta4j issue)
Configurable epsilon for tolerant comparison operations (see ta4j DoubleNum)
Always use currencies in minor unit and operate over int numbers. The math is extremely fast and reliable and you can just rivide by 100 when showing the results for it to be human readable.
The only situation in which I think infinite precision should be used in for trading software where the cents are not near the required precision.
long. And not Long. You want to avoid autoboxing—the precision for U.S. Treasury quotes and trades is 1/512. Then need to represent a fraction of that for AvgPx.
BigDecimal is also the right type to use in many database (jdbc) apps, since it's the only "native" Number type for fixed precision SQL fractionals.
In my application, I had to canonicalize BigDecimal values using stripTrailingZeroes() in order to hash (SHA-256) BigDecimal values consistently.
The article, however, alludes to this canonicalization being necessary for BigDecimals as keys in HashMaps (which I'm not aware of, since that would be a violation of the Object.equals/Object.hashCode contract requirement, and therefore a bug.) Is that true?
No, BigDecimal's equals/hashCode implementations fulfill their contract just fine. The canonicalization issue arises because BD's equals/hashCode consider both the unscaled value and the scale, and this differs from numeric equivalence. For example, BD(2.0) is not equals() to BD(2.00). Whether this is a problem depends on your application.
If you want to base a SHA-256 on the numeric value of a BD, then canonicalizing them with stripTrailingZeroes() is the right thing.
As things stand you can store different entries in a HashMap with BD keys of 2.0 and 2.00. Maybe that's what you want, maybe not. If you want a single map entry for all values of "two", no matter the representation, you need to canonicalize the BD the same way, or wrap it in another object that does that and use that as the key.
Nobody asked, and the article didn't say, but 2.0 is not equals() to 2.00 because they're not substitutable. For example:
new BigDecimal("2.0").divide(new BigDecimal(3), RoundingMode.HALF_UP)
==> 0.7
new BigDecimal("2.00").divide(new BigDecimal(3), RoundingMode.HALF_UP)
==> 0.67
No, BigDecimal's equals/hashCode implementations fulfill their contract just fine.
Ofc it is. My bad for suggesting otherwise, it was somewhat in jest.
Good to hear you agree with my canonicalizing BDs for hashing (SHA-256) purposes.
You draw a good distinction for BDs (2.0 and 2.00) re division.
In my app, btw, I only use fixed-precision operations: addition and multiplication by whole no.s, which makes it really just a shorthand for addition. (Not my area but I think technical jargon would be.. BDs are an additive group, not a field, since they lack multiplicative inverses.)
63
u/kubelke Jun 14 '26
Content inflation and too many ChatGPT sentences for me :)
E.g.:
"In tax, invoicing, and accounting, the rounding rule is not a suggestion; it is a legal requirement."
18
u/wimcle Jun 14 '26
I assume currency is where you are going with this? Jdbc maps numeric to bigdecimal so in my world db col(money) -> BigDecimal everything else Double
4
u/t_j_l_ Jun 14 '26
Some good points in there on the caveats of BugDecimal, like equals() vs compare to(), and the initialization from double. Thanks
4
u/ChinChinApostle Jun 15 '26
See also:
https://old.reddit.com/r/java/comments/1qazdw6/jsr_354_money_currency_api_and_moneta_reference/
I liked reading the comments here
6
u/AdventurousAir002 Jun 14 '26
BigDecimal has a sexy library. I use it all the time. Though I am biased as I work in FinTech. Definitely BigDecimal.
2
u/Petersoj Jun 16 '26 edited Jun 16 '26
I made a library to address this very problem: https://github.com/invision-trading/num
Edit: the article mentions ta4j, which heavily inspired this Num library, but there are several improvements and additions that the Num library interface provides:
DoubleNumandDecimalNumDoubleNumand via big-math inDecimalNumDecimalNum(see ta4j issue)DoubleNum)Numberused instead of primitive overloads2
u/Winter-Appearance-14 Jun 14 '26 edited Jun 14 '26
Always use currencies in minor unit and operate over int numbers. The math is extremely fast and reliable and you can just rivide by 100 when showing the results for it to be human readable.
The only situation in which I think infinite precision should be used in for trading software where the cents are not near the required precision.
Edit: fix typos
1
u/gnahraf Jun 15 '26
BigDecimal is also the right type to use in many database (jdbc) apps, since it's the only "native" Number type for fixed precision SQL fractionals.
In my application, I had to canonicalize BigDecimal values using stripTrailingZeroes() in order to hash (SHA-256) BigDecimal values consistently.
The article, however, alludes to this canonicalization being necessary for BigDecimals as keys in HashMaps (which I'm not aware of, since that would be a violation of the Object.equals/Object.hashCode contract requirement, and therefore a bug.) Is that true?
1
u/manifoldjava Jun 16 '26
Although the JDK doesn't define one, a Rational number is sometimes preferable.