diff --git a/Sources/AngouriMath/Core/Entity/Continuous/Entity.Continuous.Integer.Definition.cs b/Sources/AngouriMath/Core/Entity/Continuous/Entity.Continuous.Integer.Definition.cs index 2a7bb9229..e3e4c6ccd 100644 --- a/Sources/AngouriMath/Core/Entity/Continuous/Entity.Continuous.Integer.Definition.cs +++ b/Sources/AngouriMath/Core/Entity/Continuous/Entity.Continuous.Integer.Definition.cs @@ -100,21 +100,6 @@ public void Deconstruct(out int? value) => /// public override Real Abs() => Create(EInteger.Abs()); - internal static bool TryParse(string s, - [System.Diagnostics.CodeAnalysis.NotNullWhen(true)] out Integer? dst) - { - try - { - dst = EInteger.FromString(s); - return true; - } - catch - { - dst = null; - return false; - } - } - /// /// Performs integer division of the /// number by the given number diff --git a/Sources/AngouriMath/Core/Entity/Continuous/Entity.Continuous.Rational.Definition.cs b/Sources/AngouriMath/Core/Entity/Continuous/Entity.Continuous.Rational.Definition.cs index d5ef802d9..6bdc30ed7 100644 --- a/Sources/AngouriMath/Core/Entity/Continuous/Entity.Continuous.Rational.Definition.cs +++ b/Sources/AngouriMath/Core/Entity/Continuous/Entity.Continuous.Rational.Definition.cs @@ -482,21 +482,6 @@ private static bool TryReadAsDoubleDouble(EDecimal num, out double high, out dou return !double.IsInfinity(high) && !double.IsNaN(high); } - internal static bool TryParse(string s, - [System.Diagnostics.CodeAnalysis.NotNullWhen(true)] out Rational? dst) - { - try - { - dst = ERational.FromString(s); - return true; - } - catch - { - dst = null; - return false; - } - } - // Comparison here is on the value and answers a bool, where the same operators // on Entity build an inequality node instead. diff --git a/Sources/AngouriMath/Core/Entity/Continuous/Entity.Continuous.Real.Definition.cs b/Sources/AngouriMath/Core/Entity/Continuous/Entity.Continuous.Real.Definition.cs index 78ceb05f8..822c44781 100644 --- a/Sources/AngouriMath/Core/Entity/Continuous/Entity.Continuous.Real.Definition.cs +++ b/Sources/AngouriMath/Core/Entity/Continuous/Entity.Continuous.Real.Definition.cs @@ -94,12 +94,14 @@ public static Real Create(EDecimal value) internal static bool TryParse(string s, [System.Diagnostics.CodeAnalysis.NotNullWhen(true)] out Real? dst) { + // PeterO.Numbers has no TryParse; FromString throws FormatException for a string + // that is not a number, and nothing else is caught. try { dst = EDecimal.FromString(s); return true; } - catch + catch (System.FormatException) { dst = null; return false; diff --git a/Sources/AngouriMath/Core/Entity/Continuous/Number/Casts.cs b/Sources/AngouriMath/Core/Entity/Continuous/Number/Casts.cs index f0b570278..fc64af2e3 100644 --- a/Sources/AngouriMath/Core/Entity/Continuous/Number/Casts.cs +++ b/Sources/AngouriMath/Core/Entity/Continuous/Number/Casts.cs @@ -91,17 +91,11 @@ public static explicit operator BigInteger(Number num) if (num is not Real re) throw new NumberCastException(typeof(int), num.GetType()); - int plannedPrecision; - try - { - plannedPrecision = MathS.Settings.DecimalPrecisionContext.Value.Precision.ToInt32Checked(); - } - catch (OverflowException) - { + var precision = MathS.Settings.DecimalPrecisionContext.Value.Precision; + if (!precision.CanFitInInt32()) throw new InvalidNumberException( - $"The decimal precision setting is {MathS.Settings.DecimalPrecisionContext.Value.Precision}, " - + "which is too high to fit in an int32"); - } + $"The decimal precision setting is {precision}, which is too high to fit in an int32"); + var plannedPrecision = precision.ToInt32Unchecked(); return re.EDecimal.ToSizedEInteger(plannedPrecision * 3).ToBigInteger(); } diff --git a/Sources/AngouriMath/Core/Entity/Omni/Entity.Matrix.cs b/Sources/AngouriMath/Core/Entity/Omni/Entity.Matrix.cs index 5f783057c..31025fcf2 100644 --- a/Sources/AngouriMath/Core/Entity/Omni/Entity.Matrix.cs +++ b/Sources/AngouriMath/Core/Entity/Omni/Entity.Matrix.cs @@ -368,17 +368,10 @@ public Entity AsScalar() /// public static Matrix operator *(Matrix m1, Matrix m2) { - try - { - var simplified = new Matrix(GenTensor.MatrixMultiply(m1.InnerMatrix, m2.InnerMatrix)).InnerSimplified; - return ToMatrix(simplified); - } - catch (InvalidShapeException) - { + if (m1.ColumnCount != m2.RowCount) throw new InvalidMatrixOperationException( - $"Cannot multiply matrices of shapes {m1.InnerMatrix.Shape} and {m2.InnerMatrix.Shape}" - ); - } + $"Cannot multiply matrices of shapes {m1.InnerMatrix.Shape} and {m2.InnerMatrix.Shape}"); + return ToMatrix(new Matrix(GenTensor.MatrixMultiply(m1.InnerMatrix, m2.InnerMatrix)).InnerSimplified); } /// diff --git a/Sources/AngouriMath/Functions/Evaluation/Evaluation.Continuous/Evaluation.Continuous.Arithmetics.Classes.cs b/Sources/AngouriMath/Functions/Evaluation/Evaluation.Continuous/Evaluation.Continuous.Arithmetics.Classes.cs index fc1166a34..719f303bc 100644 --- a/Sources/AngouriMath/Functions/Evaluation/Evaluation.Continuous/Evaluation.Continuous.Arithmetics.Classes.cs +++ b/Sources/AngouriMath/Functions/Evaluation/Evaluation.Continuous/Evaluation.Continuous.Arithmetics.Classes.cs @@ -256,16 +256,9 @@ private bool IsEvenRoot private static bool TryPower(Matrix m, int exp, out Entity res) { - res = 0; - try - { - res = m.Pow(exp); - return true; - } - catch (InvalidMatrixOperationException) - { - return false; - } + // Only a square matrix has powers; Pow throws for any other. + res = m.IsSquare ? m.Pow(exp) : 0; + return m.IsSquare; } // Re(x) = x/2 * (1 + 1/sgn(x)^2) diff --git a/Sources/AngouriMath/Functions/InternalAMExtensions.cs b/Sources/AngouriMath/Functions/InternalAMExtensions.cs index 2d9892f19..22671fa11 100644 --- a/Sources/AngouriMath/Functions/InternalAMExtensions.cs +++ b/Sources/AngouriMath/Functions/InternalAMExtensions.cs @@ -1179,13 +1179,14 @@ public static EDecimal Factorial(this EDecimal x, EContext mathContext) return x; if (!x.IsFinite) return EDecimal.NaN; - try + // An integer that fits in an int32 is answered exactly; ToInt32IfExact would throw + // for anything else, and the approximation below takes that. + if (x.IsInteger() && x.CompareTo(EDecimal.FromInt32(int.MinValue)) >= 0 && x.CompareTo(EDecimal.FromInt32(int.MaxValue)) <= 0) { - var @int = x.ToInt32IfExact(); + var @int = x.ToInt32Unchecked(); if (@int < 0) return EDecimal.NaN; // Will become ±∞ if we don't insert this line return EDecimal.FromEInteger(Factorial(@int)).RoundToPrecision(mathContext); } - catch { } // EDecimal does not fit in an int32 if (!mathContext.Precision.CanFitInInt32()) throw new WrongNumberOfArgumentsException($"The precision of the {nameof(mathContext)} is outside the int32 range");