Repository navigation
Refuse zero, negative and non-finite input to QFactor [patch] - #460
Merged
Merged
Conversation
A zero or negative Q, bandwidth or frequency gave a nonsensical Q, an octave round trip that flipped the sign, or an OverflowException far from the cause. The constructor, the with-expression setter, Create, FromBandwidth and FromBandwidthInOctaves now throw ArgumentException. Fixes #442 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01BT7W7buDP2wr6DWYZ2xKeQ
|
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 #442
Summary
QFactor<T>checked none of its inputs. It now throwsArgumentExceptionwhen an input is zero, negative or not finite, which is the library's convention and howVector0Guards.EnsurePositivebehaves:Create, and theValueinit accessor. Because the accessor validates,q with { Value = 0 }can't produce an invalid Q either.FromBandwidth:centerFrequencyandbandwidth.FromBandwidthInOctaves:octaves, plus a positive bandwidth so narrow that2^Nrounds to 1 in double precision. That case used to give Q = +∞ fordoubleand anOverflowExceptionfordecimal.Positive inputs give the same results as before.
Before this change,
FromBandwidthInOctaves(-1)returned Q = −1.414, whichToBandwidthInOctavesread back as +1 octave.FromBandwidthInOctaves(0),FromBandwidth(f, 0)andCreate(0)gave +∞ or threwOverflowException/DivideByZeroExceptionfordecimal.Tests
QFactor_OctaveBandwidth_RoundTripsAcrossRange:FromBandwidthInOctaves(n).ToBandwidthInOctaves() ≈ nfor n from 0.05 to 4 in 0.05 steps (double), and n = 2 for decimal.QFactor_FromBandwidthInOctaves_NonPositiveOrNonFinite_Throws: 0, −1, NaN, +∞ and 1e-300 octaves.QFactor_InvalidInputs_Throw_ForDouble/_ForDecimal: every input listed in the issue, plus the constructor and awithexpression.QFactor.cschange reverted, 7 of the new tests fail. With it in place, the fullSemantics.Testsuite passes (1699 passed, 8 skipped).Semantics.Quantitiesbuilds with the local Sonar analyzers and no new warnings.Review notes
Valuenow uses the C# 14fieldkeyword so its init accessor can validate. The SDK setsLangVersiontolatest, and the repository pins the .NET 10 SDK.default(QFactor<T>)is still Q = 0, because a struct can't prevent that.Generated by Claude Code