Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -100,21 +100,6 @@ public void Deconstruct(out int? value) =>
/// <inheritdoc/>
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;
}
}

/// <summary>
/// Performs integer division of the
/// number by the given number
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
14 changes: 4 additions & 10 deletions Sources/AngouriMath/Core/Entity/Continuous/Number/Casts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
13 changes: 3 additions & 10 deletions Sources/AngouriMath/Core/Entity/Omni/Entity.Matrix.cs
Original file line number Diff line number Diff line change
Expand Up @@ -368,17 +368,10 @@ public Entity AsScalar()
/// </summary>
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);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 4 additions & 3 deletions Sources/AngouriMath/Functions/InternalAMExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Loading