Skip to content

Quirk: BigDecimal equals vs compareTo - #105

Merged
mperor merged 2 commits into
masterfrom
DAILY/92-bigdecimal-equals-vs-compareto
Nov 4, 2026
Merged

mperor merged 2 commits into
masterfrom
DAILY/92-bigdecimal-equals-vs-compareto

Conversation

@mperor

@mperor mperor commented Oct 28, 2026 •

Copy link
Copy Markdown
Owner

What you'll learn

  • BigDecimal.equals() compares both unscaled value and scale, so "2.0" and "2.00" are not equal
  • BigDecimal.compareTo() only compares numeric value, so it treats them as equal
  • this mismatch means HashSet (uses equals/hashCode) keeps both, while TreeSet (uses compareTo) keeps only one
  • new BigDecimal(double) exposes the exact binary floating-point value, while BigDecimal.valueOf(double) goes through Double.toString for the expected decimal; stripTrailingZeros() normalizes scale so equals agrees with compareTo

Why it matters

Mixing equals/hashCode with compareTo-based collections is a classic source of subtle bugs — deduplication that silently "half-works" depending on which Set implementation you pick, or BigDecimal constants that look equal but aren't.

Read more

BigDecimal#equals Javadoc

Quiz

Given Set<BigDecimal> set = new TreeSet<>(); set.add(new BigDecimal("2.0")); set.add(new BigDecimal("2.00"));, what is set.size()?

Answer 1 — `TreeSet` orders and deduplicates using `compareTo`, which ignores scale, so the second `add` is treated as a duplicate and discarded. A `HashSet` in the same scenario would keep both (size 2), since it uses `equals`/`hashCode`, which do consider scale.

Note on local verification: this environment only had JDK 21 installed, and the Gradle build requires JDK 25 via toolchain; JDK 25 auto-provisioning was blocked by network policy here, so I couldn't run ./gradlew locally. I compiled and ran the test manually against JDK 21 beforehand to check the logic, and CI (JDK 25) has now confirmed all tests pass.

Closes #92

@mperor mperor added the daily Queued for the daily learning PR label Oct 28, 2026
@github-actions

github-actions Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

Test Results

296 tests  +4   293 ✅ +4   5s ⏱️ -2s
 83 suites +1     3 💤 ±0 
 83 files   +1     0 ❌ ±0 

Results for commit ef05228. ± Comparison against base commit b44fe65.

♻️ This comment has been updated with latest results.

@mperor
mperor marked this pull request as ready for review October 28, 2026 07:16
@github-actions

Copy link
Copy Markdown

@mperor ⏰ This daily topic is still waiting for your review.

6 similar comments
@github-actions

Copy link
Copy Markdown

@mperor ⏰ This daily topic is still waiting for your review.

@github-actions

Copy link
Copy Markdown

@mperor ⏰ This daily topic is still waiting for your review.

@github-actions

Copy link
Copy Markdown

@mperor ⏰ This daily topic is still waiting for your review.

@github-actions

github-actions Bot commented Nov 1, 2026

Copy link
Copy Markdown

@mperor ⏰ This daily topic is still waiting for your review.

@github-actions

github-actions Bot commented Nov 2, 2026

Copy link
Copy Markdown

@mperor ⏰ This daily topic is still waiting for your review.

@github-actions

github-actions Bot commented Nov 3, 2026

Copy link
Copy Markdown

@mperor ⏰ This daily topic is still waiting for your review.

@mperor
mperor merged commit 1cb6506 into master Nov 4, 2026
2 checks passed
@mperor
mperor deleted the DAILY/92-bigdecimal-equals-vs-compareto branch November 4, 2026 15:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

daily Queued for the daily learning PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Quirk: BigDecimal equals vs compareTo

2 participants