Repository navigation
Throw DivideByZeroException from Divide<TResult> on a zero divisor [patch] - #534
Merged
Merged
Conversation
…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
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Fixes #531
What changed
SemanticQuantity<TSelf, TStorage>.Divide<TResult>overloads (by a quantity and by a scalar) now checkTStorage.IsZero(divisor)and throwDivideByZeroException("Cannot divide by zero."). This is the same guardDivideToStorageand the/operator already use.operator /(TSelf, TStorage)now callsDivide<TSelf>directly, because the guard lives there.<exception>docs to both overloads.Before this change, with
doublestorage,Divide<Speed>(distance, Time.Create(0))returned aSpeedholding+∞(orNaNfor0/0). With integer storage, it threw the runtime's ownDivideByZeroExceptioninstead of the library's.Tests
StaticDivide_ByZeroQuantity_ThrowsDivideByZeroException,StaticDivide_ByZeroScalar_ThrowsDivideByZeroExceptionandStaticDivide_ByZero_WithIntegerStorage_ThrowsLibraryDivideByZeroExceptioninSemanticQuantityTests.Semantics.Teston net10.0: 1965 passed, 14 skipped, 0 failed.Generated by Claude Code