Skip to content

Refuse zero, negative and non-finite input to QFactor [patch] - #460

Merged
matt-edmondson merged 1 commit into
mainfrom
fix/442-qfactor-positive
Oct 18, 2026
Merged

matt-edmondson merged 1 commit into
mainfrom
fix/442-qfactor-positive

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Fixes #442

Summary

QFactor<T> checked none of its inputs. It now throws ArgumentException when an input is zero, negative or not finite, which is the library's convention and how Vector0Guards.EnsurePositive behaves:

  • Q itself: the primary constructor, Create, and the Value init accessor. Because the accessor validates, q with { Value = 0 } can't produce an invalid Q either.
  • FromBandwidth: centerFrequency and bandwidth.
  • FromBandwidthInOctaves: octaves, plus a positive bandwidth so narrow that 2^N rounds to 1 in double precision. That case used to give Q = +∞ for double and an OverflowException for decimal.

Positive inputs give the same results as before.

Before this change, FromBandwidthInOctaves(-1) returned Q = −1.414, which ToBandwidthInOctaves read back as +1 octave. FromBandwidthInOctaves(0), FromBandwidth(f, 0) and Create(0) gave +∞ or threw OverflowException / DivideByZeroException for decimal.

Tests

  • QFactor_OctaveBandwidth_RoundTripsAcrossRange: FromBandwidthInOctaves(n).ToBandwidthInOctaves() ≈ n for 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 a with expression.
  • With the QFactor.cs change reverted, 7 of the new tests fail. With it in place, the full Semantics.Test suite passes (1699 passed, 8 skipped). Semantics.Quantities builds with the local Sonar analyzers and no new warnings.

Review notes

  • Value now uses the C# 14 field keyword so its init accessor can validate. The SDK sets LangVersion to latest, 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

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
@sonarqubecloud

Copy link
Copy Markdown

@matt-edmondson
matt-edmondson merged commit 5f2d2ab into main Oct 18, 2026
16 checks passed
@matt-edmondson
matt-edmondson deleted the fix/442-qfactor-positive branch October 18, 2026 10:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant