News for hackers. Stuff that matters.
Re:The Pitfalls of Comparing BigDecimals in Java by happytoexplain on Monday June 17, 2024 @06:53PM (#40712106)
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.
Re:The Pitfalls of Comparing BigDecimals in Java by Someone on Monday June 17, 2024 @07:23PM (#40712374)
> Why would the BigDecimal implementation of 'equals' not use semantic (numeric, in this case) equality?

https://stackoverflow.com/a/66180493:

“The general rule for equals is that two equal values should be substitutable for one another. That is, if performing a computation using one value gives some result, substituting an equals value into the same computation should give a result that equals the first result. This applies to objects that are values, such as String, Integer, BigDecimal, etc.

Now consider BigDecimal values 2.0 and 2.00. We know they are numerically equal, and that compareTo on them returns 0. But equals returns false. Why?

Here's an example where they are not substitutable:

  var a = new BigDecimal("2.0");
  var b = new BigDecimal("2.00");
  var three = new BigDecimal(3);

  a.divide(three, RoundingMode.HALF_UP)
  ==> 0.7

  b.divide(three, RoundingMode.HALF_UP)
  ==> 0.67
The results are clearly unequal, so the value of a is not substitutable for b. Therefore, a.equals(b) should be false.”
Re:The Pitfalls of Comparing BigDecimals in Java by lmm on Monday June 17, 2024 @06:56PM (#40712126)
> 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.

Re:The Pitfalls of Comparing BigDecimals in Java by happytoexplain on Monday June 17, 2024 @07:04PM (#40712199)
Oh really??? That would make sense, though it raises more questions with perhaps not so brief answers.
Re:The Pitfalls of Comparing BigDecimals in Java by lmm on Monday June 17, 2024 @07:13PM (#40712273)
3 / 2 = 1, 3.0 / 2 = 1.5. So 3.equals(3.0) is false, as this "equals" is meant to have programming semantics rather than mathematical ones.
Re:The Pitfalls of Comparing BigDecimals in Java by happytoexplain on Monday June 17, 2024 @07:17PM (#40712310)
It looks like you're comparing integer arithmetic and float arithmetic. Are you saying BigDecimal can represent both??
Re:The Pitfalls of Comparing BigDecimals in Java by lmm on Monday June 17, 2024 @07:21PM (#40712347)
That's a special case of decimal precision. 1/3 = 0, 1.0/3=0.3, 1.00/3=0.33 is probably a better example. (where by 1.0 I mean a BigDecimal with precision=2 - in fact the default is 40)
Re:The Pitfalls of Comparing BigDecimals in Java by happytoexplain on Monday June 17, 2024 @07:37PM (#40712476)
Oh, thanks, I think the phrase "programming semantics" threw me off, since I don't think of the precision behavior of ints and floats as "programming semantics", but rather "int and float semantics". In other words, it makes sense to me that integer 3/2 = 1 while float 3/2 = 1.5, because int can not represent 1.5, not because that's just how "programming" works.

So, extrapolating from that logic, since BigDecimal can represent 1.5, I would expect 3/2 = 1.5 no matter how 3 and 2 are internally represented. However, what I think I'm coming to understand, is that the precision of a BigDecimal is intentionally a semantic part of the value (duh, that's what you've been saying).

The only reason I can think of (as a math layman) for including the precision as a semantic part is that there are real world use cases for indicating precision and carrying it forward into results via rounding, like physical measurements. This is useful, but it also vexes me slightly because it means BigDecimal is not just for getting more precision than other numeric types, it is also for knowing/controlling the amount of precision, meaning there is no type dedicated only to the former. But I suppose the cases where you want more precision tend to also be the cases where you want to know/control the precision... It's just that this demonstrably doesn't occur to everybody using the API who might just be reaching for it because "I want more precision". I don't know how you would make it more obvious.

Then again, I am essentially guessing.

Re:The Pitfalls of Comparing BigDecimals in Java by lmm on Monday June 17, 2024 @08:09PM (#40712687)
> This is useful, but it also vexes me slightly because it means BigDecimal is not just for getting more precision than other numeric types, it is also for knowing/controlling the amount of precision, meaning there is no type dedicated only to the former. But I suppose the cases where you want more precision tend to also be the cases where you want to know/control the precision... It's just that this demonstrably doesn't occur to everybody using the API who might just be reaching for it because "I want more precision".

In practice someone who just "wants more precision" doesn't touch the precision and gets 40 significant figures of it, which will probably do what they want. Exact rationals have their own problems, and if you're not using exact rationals then however much precision you've got you'll always have rounding (as soon as you divide by anything other than 2 or 5).

Re:The Pitfalls of Comparing BigDecimals in Java by olliej on Monday June 17, 2024 @07:05PM (#40712212)
Why is it not?

The blog post states “BigDecimal is a class in Java used to represent floating-point numbers with arbitrary precision”

Which means that the order should not semantically matter.

That said even if the order necessarily does matter (infinite precision be alas unavailable), does not justify equality of different representations of the same value being incorrect.

Any justification for == not comparing the represented value applies equally to compareTo (which someone else suggested?)

More over operations on this type, if they can give precise/arbitrary representation should not matter if the input values are represented differently. If I perform the same operations on the same values, and in intermediate values are all representable, then the output should always be the same value.

If the actual intent of this type is “equals is an invalid way to compare values” the equals should throw when the values are the same but the representation is different.

Re:The Pitfalls of Comparing BigDecimals in Java by lmm on Monday June 17, 2024 @07:19PM (#40712329)
> The blog post states “BigDecimal is a class in Java used to represent floating-point numbers with arbitrary precision”

> Which means that the order should not semantically matter.

Arbitrary precision in the sense that you can set an arbitrary precision on your BigDecimal which then gets carried through. 1/3 = 0, 1.0/3=0.3, 1.00/3=0.33 etc.

> Any justification for == not comparing the represented value applies equally to compareTo (which someone else suggested?)

No it doesn't. equals (not the same as == in Java) is meant to mean equivalent in the program, liskov-substitutable for. compareTo is meant to compare ordering. It's ok to have values that are nonequal but compare the same for ordering purposes.

> If the actual intent of this type is “equals is an invalid way to compare values” the equals should throw when the values are the same but the representation is different.

equals is valid, it just compares something different from the mathematical value. This is the normal behaviour in programming (equals does not understand the domain, if your program has multiple representations for things that are equivalent in the domain then those representations will should still compare unequal unless they have completely equivalent behaviour in your program)

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.

Re:The Pitfalls of Comparing BigDecimals in Java by Igor_Wiwi on Monday June 17, 2024 @06:57PM (#40712136)
My suggestion would be to use compareTo where possible, and rely on equals only when you know for sure that it's implemented correctly
Re:The Pitfalls of Comparing BigDecimals in Java by olliej on Monday June 17, 2024 @07:06PM (#40712218)
Which implies the type is not compatible with the built in collections? Or have they moved aware from equals?
Re:The Pitfalls of Comparing BigDecimals in Java by happytoexplain on Monday June 17, 2024 @07:00PM (#40712169)
>only when you know for sure that it's implemented correctly

This is pretty disappointing advice for a standard/bundled API :(

Re:The Pitfalls of Comparing BigDecimals in Java by bsder on Monday June 17, 2024 @07:04PM (#40712202)
"0.0 + 0.05" and "0.00 + 0.05" are not the same so "0.0" and "0.00" should probably not be considered to be the same.
Re:The Pitfalls of Comparing BigDecimals in Java by happytoexplain on Monday June 17, 2024 @07:11PM (#40712255)
What are the results of those two examples?
Re:The Pitfalls of Comparing BigDecimals in Java by wging on Monday June 17, 2024 @07:40PM (#40712508)
Maybe GP was thinking of the optional rounding modes? With plain old add(), you get a BigDecimal 0.05 with scale 2 in both cases and the two examples are equal according to the equals() method.

    var a = new BigDecimal("0.0");
    var b = new BigDecimal("0.05");
    var result1 = a.add(b);

    var c = new BigDecimal("0.00");
    var result2 = c.add(b);
    System.out.println(result1); // 0.05
    System.out.println(result2); // 0.05
    System.out.println(result1.equals(result2)); // true
    System.out.println(result1.scale()); // 2
    System.out.println(result2.scale()); // 2
Re:The Pitfalls of Comparing BigDecimals in Java by bsder on Monday June 17, 2024 @08:30PM (#40712812)
Yeah, my bad, I was thinking about rounding.

I'm surprised that a.add(b) promotes to an extra decimal place.

That's a touch ... odd.

Re:The Pitfalls of Comparing BigDecimals in Java by wging on Monday June 17, 2024 @11:23PM (#40713788)
You could also think of it as declining to _lose_ a decimal place; a.add(b) ought to be the same as b.add(a), so there's no reason to prefer a's precision to b. You need to take the higher precision of the two for an exact result; I assume that's the rationale.

> The BigDecimal class gives its user complete control over rounding behavior. If no rounding mode is specified and the exact result cannot be represented, an exception is thrown ...

- https://docs.oracle.com/javase/8/docs/api/java/math/BigDecim...

Re:The Pitfalls of Comparing BigDecimals in Java by wging on Tuesday June 18, 2024 @12:57AM (#40714196)
https://docs.oracle.com/en/java/javase/21/docs/api/java.base... is probably a better link (though it's not that different)
Re:The Pitfalls of Comparing BigDecimals in Java by bsder on Tuesday June 18, 2024 @01:03AM (#40714211)
Thanks for the link.

Apparently, restricted behavior like I expected is reserved for if you use a MathContext.

> Instead, for the BigDecimal operations taking a MathContext parameter, if the MathContext has a nonzero precision, the set of possible representable values for the result is determined by the precision of the MathContext argument.

Very strange. I would have expected something that is going to be mostly used for currency to not do implicit things by default.

Re:The Pitfalls of Comparing BigDecimals in Java by shagie on Monday June 17, 2024 @09:26PM (#40713159)
https://docs.oracle.com/javase/8/docs/api/java/math/BigDecim...

> Note: care should be exercised if BigDecimal objects are used as keys in a SortedMap or elements in a SortedSet since BigDecimal's natural ordering is inconsistent with equals. See Comparable, SortedMap or SortedSet for more information.

https://docs.oracle.com/javase/8/docs/api/java/math/BigDecim...

> Compares this BigDecimal with the specified Object for equality. Unlike compareTo, this method considers two BigDecimal objects equal only if they are equal in value and scale (thus 2.0 is not equal to 2.00 when compared by this method).

equals remains object comparisons.

Consider the problem if `2.0` and `2.00` returned true for equals call and they were keys to a HashMap - you'd only be able to put one of them in there.

https://github.com/openjdk/jdk/blob/master/src/java.base/sha...