Refactor exact-zero guards and update mathematical notations across multiple classes to enhance clarity and prevent division by zero errors.

This commit is contained in:
Miha Kralj
2026-02-16 22:30:30 -08:00
parent b63b9730e1
commit fa56e2b76d
10 changed files with 38 additions and 54 deletions
+4 -6
View File
@@ -13,8 +13,8 @@ namespace QuanTAlib;
/// making it useful for timing entries and exits.
///
/// Formula:
/// num = Σ(count * price[count-1]) for count = 1 to length
/// den = Σ(price[count-1]) for count = 1 to length
/// num = Σ(count * price[count-1]) for count = 1 to length
/// den = Σ(price[count-1]) for count = 1 to length
/// CG = (num / den) - (length + 1) / 2
///
/// Properties:
@@ -180,8 +180,7 @@ public sealed class Cg : AbstractBase
private double CalculateCg()
{
int n = _buffer.Count;
// skipcq: CS-R1077 - Exact-zero guard: _sum is cumulative price sum; zero means empty buffer, division by zero below
if (n == 0 || _sum == 0)
if (n == 0 || _sum == 0) // skipcq: CS-R1077 - Exact-zero guard: _sum is cumulative price sum; zero means empty buffer, division by zero below
{
return 0;
}
@@ -300,8 +299,7 @@ public sealed class Cg : AbstractBase
sum += price;
}
// skipcq: CS-R1077 - Exact-zero guard: sum is cumulative price sum; zero means empty buffer, division by zero below
if (sum == 0)
if (sum == 0) // skipcq: CS-R1077 - Exact-zero guard: sum is cumulative price sum; zero means empty buffer, division by zero below
{
output[i] = 0;
continue;
+2 -3
View File
@@ -18,7 +18,7 @@ namespace QuanTAlib;
/// 3. Homodyne mixing: multiply I/Q with their 1-bar delayed values
/// 4. Re = I*I[1] + Q*Q[1], Im = I*Q[1] - Q*I[1]
/// 5. Angle = atan2(Im, Re) gives instantaneous phase change
/// 6. Period = 2π / angle with clamping and smoothing
/// 6. Period = 2Ï€ / angle with clamping and smoothing
///
/// Properties:
/// - Returns smoothed dominant cycle period
@@ -321,8 +321,7 @@ public sealed class Homod : AbstractBase
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static double Atan2(double y, double x)
{
// skipcq: CS-R1077 - Exact-zero guard: both quadrature components zero means no signal; atan2(0,0) is undefined
if (y == 0.0 && x == 0.0)
if (y == 0.0 && x == 0.0) // skipcq: CS-R1077 - Exact-zero guard: both quadrature components zero means no signal; atan2(0,0) is undefined
{
return 0.0; // Return 0 instead of error for robustness
}
+2 -4
View File
@@ -206,12 +206,10 @@ public sealed class HtSine : AbstractBase
prevI2 = i2;
double tempReal1 = period;
// skipcq: CS-R1077 - Exact-zero guard: atan(im/re) requires nonzero denominator; zero im/re means no signal energy
if (im != 0.0 && re != 0.0)
if (im != 0.0 && re != 0.0) // skipcq: CS-R1077 - Exact-zero guard: atan(im/re) needs nonzero; zero means no signal
{
double angle = Math.Atan(im / re);
// skipcq: CS-R1077 - Exact-zero guard: angle == 0 means period is undefined (division by angle below)
if (angle != 0.0)
if (angle != 0.0) // skipcq: CS-R1077 - Exact-zero guard: angle == 0 means period undefined (div by angle)
{
period = (2.0 * Math.PI) / angle;
}