[HN Gopher] The Pitfalls of Comparing BigDecimals in Java
___________________________________________________________________
The Pitfalls of Comparing BigDecimals in Java
Author : Igor_Wiwi
Score : 10 points
Date : 2024-06-17 22:06 UTC (53 minutes ago)
(HTM) web link (igorstechnoclub.com)
(TXT) w3m dump (igorstechnoclub.com)
| dgfitz wrote:
| I use bigInt and bigDec when I wrote a pokemonGo bot way back
| when the cracked api released in like... 2016?
|
| I didn't bump into the particular issue, but not being much of a
| Java person, they were very helpful. My bot was pretty consistent
| at getting lvl30 in a night and usually something fun like a
| snorlax or charizard.
|
| When they broke the api I stopped playing, writing the code for a
| bit was more fun (for me) than playing as intended.
|
| As an aside, are there games like that, which require writing
| code? I imagine the market isn't big for those, but I sure had a
| blast for a month or two.
| gleenn wrote:
| Out of curiosity, why did that game require BigDecimals?
| agluszak wrote:
| Interesting. I'd say that it's one of the places where using a
| non-trivial, non-structural comparison method would make sense,
| as opposed to the infamous URL equals method.
| gleenn wrote:
| Kinda makes me think that implementation is probably not very
| performant or optimized for the obvious case. A big pet peeve of
| mine is when languages do weird and very unintuitive things with
| equality. Like in Javascript and PHP you end up needing these
| crazy charts to determine if something == something else.
| Personally I think Clojure gets this really well and has some of
| the fewest surprises.
| csjh wrote:
| No need to remember crazy charts to determine equality in
| Javascript, just blanketly use `===`
| auntienomen wrote:
| This seems like incredibly bad design. Is there any justification
| or use case where someone would want the abstraction to leak like
| this?
| happytoexplain wrote:
| Why would the BigDecimal implementation of 'equals' not use
| semantic (numeric, in this case) equality? Is 'equals' always
| meant to be memberwise (or perhaps makes performance guarantees),
| and if so, shouldn't there be another function/operator that is
| always meant to be semantic? The article doesn't acknowledge
| these questions.
| lmm wrote:
| > Why would the BigDecimal implementation of 'equals' not use
| semantic (numeric, in this case) equality?
|
| A BigDecimal with different precision is not really
| semantically equivalent - it will lead to different results
| when you use it in calculations.
| Igor_Wiwi wrote:
| My suggestion would be to use compareTo where possible, and
| rely on equals only when you know for sure that it's
| implemented correctly
___________________________________________________________________
(page generated 2024-06-17 23:00 UTC)