Skip to content

Throw DivideByZeroException from Divide<TResult> on a zero divisor [patch] - #534

Merged
matt-edmondson merged 1 commit into
mainfrom
fix/531-divide-zero-guard
Nov 4, 2026
Merged

matt-edmondson merged 1 commit into
mainfrom
fix/531-divide-zero-guard

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Fixes #531

What changed

  • Both SemanticQuantity<TSelf, TStorage>.Divide<TResult> overloads (by a quantity and by a scalar) now check TStorage.IsZero(divisor) and throw DivideByZeroException("Cannot divide by zero."). This is the same guard DivideToStorage and the / operator already use.
  • operator /(TSelf, TStorage) now calls Divide<TSelf> directly, because the guard lives there.
  • Added <exception> docs to both overloads.

Before this change, with double storage, Divide<Speed>(distance, Time.Create(0)) returned a Speed holding +∞ (or NaN for 0/0). With integer storage, it threw the runtime's own DivideByZeroException instead of the library's.

Tests

  • StaticDivide_ByZeroQuantity_ThrowsDivideByZeroException, StaticDivide_ByZeroScalar_ThrowsDivideByZeroException and StaticDivide_ByZero_WithIntegerStorage_ThrowsLibraryDivideByZeroException in SemanticQuantityTests.
  • I reverted the library change and confirmed all three fail without it: the double cases do not throw, and the int case throws with the runtime's message.
  • Full Semantics.Test on net10.0: 1965 passed, 14 skipped, 0 failed.

Generated by Claude Code

…atch]

SemanticQuantity<TSelf, TStorage>.Divide<TResult> returned Infinity or NaN
for a zero divisor with floating-point storage, and the runtime's own
DivideByZeroException with integer storage, while DivideToStorage and the
/ operator threw the library's DivideByZeroException. Guard both Divide
overloads with TStorage.IsZero, and let the / operator rely on that guard
instead of repeating it.

Fixes #531

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017vfJW4yWE2S9gofHiwsvs7
@sonarqubecloud

sonarqubecloud Bot commented Nov 1, 2026

Copy link
Copy Markdown

@matt-edmondson
matt-edmondson merged commit 54d51b0 into main Nov 4, 2026
16 checks passed
@matt-edmondson
matt-edmondson deleted the fix/531-divide-zero-guard branch November 4, 2026 00:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SemanticQuantity.Divide<TResult> returns Infinity/NaN on a zero divisor, while the / operator and DivideToStorage throw DivideByZeroException

1 participant