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
15 changes: 15 additions & 0 deletions Sources/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -1099,3 +1099,18 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed

[Tests/UnitTests/Common/DotAndCrossNodesTest.cs]
file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n

[AngouriMath/Core/Entity/Continuous/Number/MachineNumbers.cs]
file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n

[Tests/UnitTests/Core/NumberCreationAsOf270.cs]
file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n

[Tests/UnitTests/Core/AMachineNumberIsMadeAsBeforeTest.cs]
file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n

[Tests/UnitTests/Core/ARealMadeWithoutDowncastingTest.cs]
file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n

[Tests/UnitTests/Core/ANumberEvaluatesToItselfTest.cs]
file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n
4 changes: 2 additions & 2 deletions Sources/AngouriMath/Convenience/MathS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4795,7 +4795,7 @@ public static class Numbers
/// </code>
/// </example>
public static Complex Create(NumericsComplex value) =>
Complex.FromMachineNumbers(EDecimal.FromDouble(value.Real), EDecimal.FromDouble(value.Imaginary));
Complex.FromMachineNumbers(value.Real, value.Imaginary);

/// <summary>Creates an instance of <see cref="Integer"/> from a <see cref="long"/></summary>
/// <param name="value">A value of type <see cref="long"/> (signed 64-bit integer)</param>
Expand Down Expand Up @@ -5128,7 +5128,7 @@ public static Rational CreateRational(EInteger numerator, EInteger denominator)
/// EDecimal i9 = ((Entity.Number.Real)FromString("1 + 5").EvalNumerical()).EDecimal;
/// </code>
/// </example>
public static Real Create(double value) => Real.FromMachineNumber(EDecimal.FromDouble(value));
public static Real Create(double value) => Real.FromMachineNumber(EDecimal.FromDouble(value), value);

/// <summary>
/// Creates an instance of <see cref="Complex"/> from two <see cref="EDecimal"/>s
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,22 +107,36 @@ internal static Complex CreateAtScales(EDecimal real, EDecimal imaginary, EDecim
/// A number from a pair of <see cref="double"/>s, each part rounded as
/// <see cref="Real.FromMachineNumber(EDecimal)"/> rounds it.
/// </summary>
internal static Complex FromMachineNumbers(EDecimal real, EDecimal imaginary) => Create(real, imaginary, EDecimal.One, EDecimal.One, fromMachineNumbers: true);
internal static Complex FromMachineNumbers(EDecimal real, EDecimal imaginary) => FromMachineNumbers(real, imaginary, double.NaN, double.NaN);

private static Complex Create(EDecimal real, EDecimal imaginary, EDecimal realScale, EDecimal imaginaryScale, bool fromMachineNumbers)
/// <summary>
/// <see cref="FromMachineNumbers(EDecimal, EDecimal)"/> of a pair of <see cref="double"/>s.
/// </summary>
internal static Complex FromMachineNumbers(double real, double imaginary)
=> FromMachineNumbers(EDecimal.FromDouble(real), EDecimal.FromDouble(imaginary), real, imaginary);

/// <summary>
/// <see cref="FromMachineNumbers(EDecimal, EDecimal)"/>, each part's exact value also given as a
/// <see cref="double"/>, or <see cref="double.NaN"/>, for <see cref="Real.FromMachineNumber(EDecimal, double)"/>.
/// </summary>
internal static Complex FromMachineNumbers(EDecimal real, EDecimal imaginary, double realMachine, double imaginaryMachine)
=> Create(real, imaginary, EDecimal.One, EDecimal.One, fromMachineNumbers: true, realMachine, imaginaryMachine);

private static Complex Create(EDecimal real, EDecimal imaginary, EDecimal realScale, EDecimal imaginaryScale, bool fromMachineNumbers,
double realMachine = double.NaN, double imaginaryMachine = double.NaN)
{
if (!MathS.Settings.DowncastingEnabled)
return new Complex(real, imaginary);
Real Part(EDecimal part, EDecimal scale) => fromMachineNumbers ? Real.FromMachineNumber(part) : Real.CreateAtScale(part, scale);
Real Part(EDecimal part, EDecimal scale, double machine) => fromMachineNumbers ? Real.FromMachineNumber(part, machine) : Real.CreateAtScale(part, scale);
if (imaginary.IsZero)
return Part(real, realScale);
return Part(real, realScale, realMachine);
if (real.IsNaN() || imaginary.IsNaN())
return Real.NaN;
if (imaginary.IsFinite && !imaginaryScale.IsZero && imaginary.Abs().LessThan(
ReferenceEquals(imaginaryScale, EDecimal.One) ? MathS.Settings.DowncastingTolerance : MathS.Settings.DowncastingTolerance.Multiply(imaginaryScale.Abs())))
return Part(real, realScale);
return Part(real, realScale, realMachine);
else
return new Complex(Part(real, realScale), Part(imaginary, imaginaryScale));
return new Complex(Part(real, realScale, realMachine), Part(imaginary, imaginaryScale, imaginaryMachine));
}

/// <summary>
Expand Down Expand Up @@ -328,15 +342,15 @@ public static implicit operator Complex(EDecimal value)
/// as the value that literal actually holds and not as one tenth.
/// </summary>
public static implicit operator Complex(float value)
=> FromMachineNumbers(EDecimal.FromSingle(value), EDecimal.Zero);
=> FromMachineNumbers(EDecimal.FromSingle(value), EDecimal.Zero, value, 0);

/// <summary>
/// The value as a <see cref="Complex"/> with no imaginary part, read as the
/// binary <see langword="double"/> it is rather than as the decimal it was
/// written as.
/// </summary>
public static implicit operator Complex(double value)
=> FromMachineNumbers(EDecimal.FromDouble(value), EDecimal.Zero);
=> FromMachineNumbers(EDecimal.FromDouble(value), EDecimal.Zero, value, 0);

/// <summary>
/// The value as a <see cref="Complex"/> with no imaginary part. A
Expand All @@ -351,7 +365,7 @@ public static implicit operator Complex(decimal value)
/// <see langword="double"/> parts and their precision.
/// </summary>
public static implicit operator Complex(System.Numerics.Complex value) =>
FromMachineNumbers(EDecimal.FromDouble(value.Real), EDecimal.FromDouble(value.Imaginary));
FromMachineNumbers(value.Real, value.Imaginary);

/// <summary>The pair read as real and imaginary parts.</summary>
public static implicit operator Complex((int re, int im) v) => Create(v.re, v.im);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,13 @@ public abstract partial record Number : ContinuousNode
/// The value as a <see cref="Real"/>, read as the binary <see langword="float"/> it
/// is — so <c>0.1f</c> becomes the value that literal holds, not one tenth.
/// </summary>
public static implicit operator Number(float value) => Real.FromMachineNumber(EDecimal.FromSingle(value));
public static implicit operator Number(float value) => Real.FromMachineNumber(EDecimal.FromSingle(value), value);

/// <summary>
/// The value as a <see cref="Real"/>, read as the binary <see langword="double"/> it
/// is rather than as the decimal it was written as.
/// </summary>
public static implicit operator Number(double value) => Real.FromMachineNumber(EDecimal.FromDouble(value));
public static implicit operator Number(double value) => Real.FromMachineNumber(EDecimal.FromDouble(value), value);

/// <summary>
/// The value as a <see cref="Real"/>. A <see langword="decimal"/> is decimal
Expand All @@ -108,7 +108,7 @@ public abstract partial record Number : ContinuousNode
/// <see langword="double"/> parts and their precision.
/// </summary>
public static implicit operator Number(System.Numerics.Complex value)
=> Complex.FromMachineNumbers(EDecimal.FromDouble(value.Real), EDecimal.FromDouble(value.Imaginary));
=> Complex.FromMachineNumbers(value.Real, value.Imaginary);
/// <summary>The big integer as an <see cref="Integer"/>, exactly and at any size.</summary>
/// <remarks>
/// Read as the two's-complement bytes it is, which is what
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,30 @@ private protected Real() : base(null, null) { }
/// </remarks>
public static Real Create(EDecimal value) => Create(value, fromMachineNumber: false, EDecimal.One);

/// <summary>
/// A <see cref="Real"/> holding exactly <paramref name="value"/>, never rounded onto an
/// <see cref="Integer"/> or a <see cref="Rational"/>, whatever
/// <see cref="MathS.Settings.DowncastingEnabled"/> says: what <see cref="Create(EDecimal)"/> answers
/// with the downcasting off, without opening a scope to turn it off.
/// </summary>
/// <example>
/// <code>
/// using System;
/// using AngouriMath;
/// using PeterO.Numbers;
///
/// var half = EDecimal.FromString("0.5");
/// Console.WriteLine(Entity.Number.Real.Create(half).GetType().Name);
/// Console.WriteLine(Entity.Number.Real.CreateWithoutDowncasting(half).GetType().Name);
/// </code>
/// Prints
/// <code>
/// Rational
/// Real
/// </code>
/// </example>
public static Real CreateWithoutDowncasting(EDecimal value) => new Real(value);

/// <summary>
/// A value an arithmetic operation produced, which is a residual of cancellation, and zero, only below
/// half the working digits of <paramref name="cancelledScale"/>, the size of the terms that cancelled;
Expand Down Expand Up @@ -327,13 +351,13 @@ internal static bool TryParse(string s,
/// The value as a <see cref="Real"/>, read as the binary <see langword="float"/>
/// it is — so <c>0.1f</c> becomes the value that literal holds, not one tenth.
/// </summary>
public static implicit operator Real(float value) => FromMachineNumber(EDecimal.FromSingle(value));
public static implicit operator Real(float value) => FromMachineNumber(EDecimal.FromSingle(value), value);

/// <summary>
/// The value as a <see cref="Real"/>, read as the binary
/// <see langword="double"/> it is rather than as the decimal it was written as.
/// </summary>
public static implicit operator Real(double value) => FromMachineNumber(EDecimal.FromDouble(value));
public static implicit operator Real(double value) => FromMachineNumber(EDecimal.FromDouble(value), value);

/// <summary>
/// The value as a <see cref="Real"/>. A <see langword="decimal"/> is decimal
Expand Down
Loading
Loading