News for hackers. Stuff that matters.
Re:The Pitfalls of Comparing BigDecimals in Java by zeroimpl on Monday June 17, 2024 @08:24PM (#40712763)
> The blog post states “BigDecimal is a class in Java used to represent floating-point numbers with arbitrary precision”

That doesn’t mean BigDecimal is used exclusively for that purpose… If you ask an engineer if 2.5 and 2.50 are the same, they will say no. It’s clear from the implementation that BigDecimal was meant for more than just representing arbitrary precision floating points, which is why there is a pitfall if that’s all you use it for.

Btw that’s the reason it’s got Decimal in the class name - it’s modeling base10 strings, not numbers. If the strings are different it shouldn’t be equal.

Re:The Pitfalls of Comparing BigDecimals in Java by tonynator on Monday June 17, 2024 @11:41PM (#40713855)
>If you ask an engineer if 2.5 and 2.50 are the same, they will say no They will say it depends on the context. If you don't look into the implementation, the class name and maybe a short summary provides that context and clearly misleads people
Re:The Pitfalls of Comparing BigDecimals in Java by zeroimpl on Tuesday June 18, 2024 @02:44AM (#40714739)
Right, but in this case the short summary being posted is poor. In the context of a language like Java, floating point is synonymous with IEEE base-2 floats. BigDecimal is not used to store arbitrary precision floats, but rather fixed precision decimals. Otherwise it would be called BigFloat.

Also the scale parameter really isn’t an implementation detail. Virtually every non-trivial method defined in the class requires you to understand the scale parameter in order to get the result you want.