Skip to content

Commit 68e231d

Browse files
Exceptions from other libraries are checked for before the call (#1561)
Four places caught an exception from code the library does not own, where a check before the call says the same thing: - the factorial of an exact integer read the value with `ToInt32IfExact` inside a bare `catch`; it tests `IsInteger()` and the int32 range first; - the conversion to `BigInteger` caught `ToInt32Checked`'s `OverflowException` on the precision; it tests `CanFitInInt32` first, as the rest of the library does; - a matrix product caught GenericTensor's `InvalidShapeException`; it compares the shapes first, as `+` and `-` already do; - a matrix power caught `Pow`'s exception for a non-square matrix; it tests `IsSquare` first. `Integer.TryParse` and `Rational.TryParse`, internal and with no callers, are deleted. `Real.TryParse` still catches, since PeterO.Numbers has no parser that does not throw, but only `FormatException` now. Closes #1548. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent e9ad9f2 commit 68e231d

7 files changed

Lines changed: 17 additions & 64 deletions

File tree

‎Sources/AngouriMath/Core/Entity/Continuous/Entity.Continuous.Integer.Definition.cs‎

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -100,21 +100,6 @@ public void Deconstruct(out int? value) =>
100100
/// <inheritdoc/>
101101
public override Real Abs() => Create(EInteger.Abs());
102102

103-
internal static bool TryParse(string s,
104-
[System.Diagnostics.CodeAnalysis.NotNullWhen(true)] out Integer? dst)
105-
{
106-
try
107-
{
108-
dst = EInteger.FromString(s);
109-
return true;
110-
}
111-
catch
112-
{
113-
dst = null;
114-
return false;
115-
}
116-
}
117-
118103
/// <summary>
119104
/// Performs integer division of the
120105
/// number by the given number

‎Sources/AngouriMath/Core/Entity/Continuous/Entity.Continuous.Rational.Definition.cs‎

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -482,21 +482,6 @@ private static bool TryReadAsDoubleDouble(EDecimal num, out double high, out dou
482482
return !double.IsInfinity(high) && !double.IsNaN(high);
483483
}
484484

485-
internal static bool TryParse(string s,
486-
[System.Diagnostics.CodeAnalysis.NotNullWhen(true)] out Rational? dst)
487-
{
488-
try
489-
{
490-
dst = ERational.FromString(s);
491-
return true;
492-
}
493-
catch
494-
{
495-
dst = null;
496-
return false;
497-
}
498-
}
499-
500485
// Comparison here is on the value and answers a bool, where the same operators
501486
// on Entity build an inequality node instead.
502487

‎Sources/AngouriMath/Core/Entity/Continuous/Entity.Continuous.Real.Definition.cs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,14 @@ public static Real Create(EDecimal value)
9494
internal static bool TryParse(string s,
9595
[System.Diagnostics.CodeAnalysis.NotNullWhen(true)] out Real? dst)
9696
{
97+
// PeterO.Numbers has no TryParse; FromString throws FormatException for a string
98+
// that is not a number, and nothing else is caught.
9799
try
98100
{
99101
dst = EDecimal.FromString(s);
100102
return true;
101103
}
102-
catch
104+
catch (System.FormatException)
103105
{
104106
dst = null;
105107
return false;

‎Sources/AngouriMath/Core/Entity/Continuous/Number/Casts.cs‎

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -91,17 +91,11 @@ public static explicit operator BigInteger(Number num)
9191
if (num is not Real re)
9292
throw new NumberCastException(typeof(int), num.GetType());
9393

94-
int plannedPrecision;
95-
try
96-
{
97-
plannedPrecision = MathS.Settings.DecimalPrecisionContext.Value.Precision.ToInt32Checked();
98-
}
99-
catch (OverflowException)
100-
{
94+
var precision = MathS.Settings.DecimalPrecisionContext.Value.Precision;
95+
if (!precision.CanFitInInt32())
10196
throw new InvalidNumberException(
102-
$"The decimal precision setting is {MathS.Settings.DecimalPrecisionContext.Value.Precision}, "
103-
+ "which is too high to fit in an int32");
104-
}
97+
$"The decimal precision setting is {precision}, which is too high to fit in an int32");
98+
var plannedPrecision = precision.ToInt32Unchecked();
10599

106100
return re.EDecimal.ToSizedEInteger(plannedPrecision * 3).ToBigInteger();
107101
}

‎Sources/AngouriMath/Core/Entity/Omni/Entity.Matrix.cs‎

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -368,17 +368,10 @@ public Entity AsScalar()
368368
/// </summary>
369369
public static Matrix operator *(Matrix m1, Matrix m2)
370370
{
371-
try
372-
{
373-
var simplified = new Matrix(GenTensor.MatrixMultiply(m1.InnerMatrix, m2.InnerMatrix)).InnerSimplified;
374-
return ToMatrix(simplified);
375-
}
376-
catch (InvalidShapeException)
377-
{
371+
if (m1.ColumnCount != m2.RowCount)
378372
throw new InvalidMatrixOperationException(
379-
$"Cannot multiply matrices of shapes {m1.InnerMatrix.Shape} and {m2.InnerMatrix.Shape}"
380-
);
381-
}
373+
$"Cannot multiply matrices of shapes {m1.InnerMatrix.Shape} and {m2.InnerMatrix.Shape}");
374+
return ToMatrix(new Matrix(GenTensor.MatrixMultiply(m1.InnerMatrix, m2.InnerMatrix)).InnerSimplified);
382375
}
383376

384377
/// <summary>

‎Sources/AngouriMath/Functions/Evaluation/Evaluation.Continuous/Evaluation.Continuous.Arithmetics.Classes.cs‎

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -256,16 +256,9 @@ private bool IsEvenRoot
256256

257257
private static bool TryPower(Matrix m, int exp, out Entity res)
258258
{
259-
res = 0;
260-
try
261-
{
262-
res = m.Pow(exp);
263-
return true;
264-
}
265-
catch (InvalidMatrixOperationException)
266-
{
267-
return false;
268-
}
259+
// Only a square matrix has powers; Pow throws for any other.
260+
res = m.IsSquare ? m.Pow(exp) : 0;
261+
return m.IsSquare;
269262
}
270263

271264
// Re(x) = x/2 * (1 + 1/sgn(x)^2)

‎Sources/AngouriMath/Functions/InternalAMExtensions.cs‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1179,13 +1179,14 @@ public static EDecimal Factorial(this EDecimal x, EContext mathContext)
11791179
return x;
11801180
if (!x.IsFinite)
11811181
return EDecimal.NaN;
1182-
try
1182+
// An integer that fits in an int32 is answered exactly; ToInt32IfExact would throw
1183+
// for anything else, and the approximation below takes that.
1184+
if (x.IsInteger() && x.CompareTo(EDecimal.FromInt32(int.MinValue)) >= 0 && x.CompareTo(EDecimal.FromInt32(int.MaxValue)) <= 0)
11831185
{
1184-
var @int = x.ToInt32IfExact();
1186+
var @int = x.ToInt32Unchecked();
11851187
if (@int < 0) return EDecimal.NaN; // Will become ±∞ if we don't insert this line
11861188
return EDecimal.FromEInteger(Factorial(@int)).RoundToPrecision(mathContext);
11871189
}
1188-
catch { } // EDecimal does not fit in an int32
11891190

11901191
if (!mathContext.Precision.CanFitInInt32())
11911192
throw new WrongNumberOfArgumentsException($"The precision of the {nameof(mathContext)} is outside the int32 range");

0 commit comments

Comments
 (0)