[minor] Keep Vector0 magnitudes non-negative through unary minus and scalar * and / - #343
Merged
Merged
Conversation
…scalar * and / The generator already withheld binary '-' from V0 forms, but still emitted unary negation and unguarded scalar multiplication and division, so -Temperature.FromKelvin(300) was -300 K and Speed * -3.0 was negative. V0 forms (bases and overloads) no longer declare unary '-', and their scalar '*' and '/' run the result through Vector0Guards.EnsureNonNegative, throwing ArgumentException on a negative scalar. V1 forms are unchanged. Breaking: code that negated a V0 quantity no longer compiles, and scaling one by a negative number now throws. Also corrects complete-library-guide.md, which claimed Speed.Create(-1) throws. Fixes #285 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_015i2yeKwKRWtHhdc6NPySjW
|
This was referenced Sep 30, 2026
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 #285
What was wrong
The generator already withheld binary
-from V0 forms. It still emitted unary-and unguarded scalar*and/, so a single operator could take a magnitude below zero:-Temperature<double>.FromKelvin(300)→ −300 KSpeed<double>.FromMeterPerSecond(2) * -3.0→ −6 m/sChange (
QuantitiesGenerator.AddValueTypeCore)operator -. That matches how binary-is already handled.operator *(q, T),operator *(T, q)andoperator /(q, T)now pass the result throughVector0Guards.EnsureNonNegative, so a negative scalar throwsArgumentException. Divide-by-zero still throwsDivideByZeroExceptionas before.docs/complete-library-guide.mdclaimed thatSpeed.Create(-1)throws. It doesn't:Createis the unguarded primitive. The guide now points at the guarded factory and the operator rules.Semantics.Quantities/Generatedmakes up most of the 156-file diff: one hunk per V0 type.Breaking: code that negates a V0 quantity no longer compiles, and scaling a V0 by a negative number now throws. Hence
[minor].Tests
Vector0InvariantTestsgains 7 tests:Temperature,Speed,Distance) have noop_UnaryNegation.*and/by a negative scalar throw, on both a base and an overload.Semantics.Testsuite on Linux: 1367 passed, 0 failed, 8 skipped (Windows-only path tests).Semantics.slnbuilds with no errors, so nothing else in the repo relied on V0 negation.This PR is independent of #342 (#283, which also touches
QuantitiesGenerator.cs, in a different region).🤖 Generated with Claude Code
https://claude.ai/code/session_015i2yeKwKRWtHhdc6NPySjW
Generated by Claude Code