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
39 changes: 39 additions & 0 deletions BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,45 @@ integer. Exact large integers are unchanged
| `"integral(exp(x), x, 0, 300)".Simplify()` | `1942426395…4199999…9` | `e ^ 300 - 1` |
| `"2^340 * sqrt(2)".Evaled` | an `Integer` | `3.167477390…E+102`, a `Real` |

### A complex number with exact parts keeps them exact

**Silent: wrong numbers become right ones.** A sum, difference, product or quotient of two complex numbers, and so a
whole power of one, was made through the decimals of their parts and rounded back onto a rational only within half the
working digits, which a part past `10^50` is not: `(10^50 + 1/3 + i) - 10^50` was `0.333...3 + i`, and
`(10^60 + i) (10^60 - i)` was `10^120` where it is `10^120 + 1`. A quotient near a small rational was rounded onto it:
`(10^50 - 1/3 - i/7) / (10^50 + 1/7 - 10^50 i)` was `1/2 + 1/2 i`. Where every part of both is a rational, the
arithmetic is now exact, as a real one's is. And an exact `10^84 i` was a decimal, past the digits a cosine needs, so
its exponential was `NaN`; it is exact now, and the functions of an exact number past the working digits have values.
A decimal there, `10^84 sqrt(2) i`, has none, as before. Measured on master (34d481139) and on this branch; the true
values are mpmath's at 700 to 750 digits ([#2685](https://github.com/asc-community/AngouriMath/issues/2685)).

| `.Evaled` | Was (master) | Now |
|---|---|---|
| `(10^50 + 1/3 + i) - 10^50` | `0.3333333333333333333333333333333333333333333333333 + i` | `1/3 + i` |
| `(10^60 + i) * (10^60 - i)` | `1.000…000E+120` | `10^120 + 1`, as an integer |
| `(10^60 + i) / (10^60 - i)` | `1 + 2E-60i` | `(10^120 - 1)/(10^120 + 1) + 2 10^60/(10^120 + 1) i`, as rationals |
| `(10^29 + i) / (10^29 - i)` | `1 + 1.9999…98E-29i` | `(10^58 - 1)/(10^58 + 1) + 2 10^29/(10^58 + 1) i`, as rationals |
| `(10^50 - 1/3 - i / 7) / (10^50 + 1/7 - 10^50 * i)` | `1/2 + 1/2 * i` | the exact quotient, `1/2 - 9.5…E-52 + (1/2 - 3.1…E-51) i` |
| `(10^50 + 1/3 + i) ^ 2` | `1.0000…0666…E+100 + 200000000000000000000000000000000000000000000000000.666…i` | `(10^50 + 1/3)^2 - 1 + 2 (10^50 + 1/3) i`, as rationals |
| `(0.3 + 0.4i)^(-9)` | `-241.716953088 - 451.350102016i` | `-472103424/1953125 - 881543168/1953125 * i`, the same number |
| `(1 + i)^(10^6)` | `+oo` | `2^500000`, as an integer, as `2^(10^6)` is one |
| `exp(10^84 * i)` | `NaN` | `0.7550644793… + 0.6556505411…i` |
| `cos(10^84 + i)` | `NaN` | `1.1651253760… - 0.7705212985…i` |
| `tan(10^150 + i)` | `NaN` | `0.1995074892… + 1.2276265478…i` |
| `sinh(10^100 * i)` | `NaN` | `-0.3723761236…i` |
| `Ci(10^100 + i)` | `NaN` | `-5.7460638528…E-101 - 1.0906829626…E-100i` |
| `Si(10^100 + i)` | `NaN` | `1.5707963267… - 4.3761686501…E-101i` |
| `Ci(10^200 + i)` | `NaN` | `1.4955097442…E-200 - 2.8955450281…E-201i` |

A value whose parts were decimals now prints them as fractions, which past `10^50` have as many digits as the numbers
they are made from. Of 5300 sums, differences, products, quotients and powers of parts like `10^k ± 1/3`, `10^k i` and
`i/7` for `k` from 1 to 300, master had 664 exact, 310 of them rounded onto a wrong rational, and 4636 decimals, 262 of
them wrong past a relative `1e-90`; this branch has all 5300 exact and right. Of 1830 values of `cos`, `sin`, `tan`,
`exp`, `cosh`, `sinh`, `ln` and `sqrt` of `10^k + i`, `10^k i` and `10^k + i/3`, and of `Ci`, `Si` and `Ei` of
`10^k ± i`, for `k` from 0 to 300, master had 734 `NaN`; this branch has 557 of them right to a relative `1e-99` of each
part, and none that was right on master changed. The 177 left `NaN` are `cos`, `sin` and `tan` of
`10^k i` from `k = 10`, `NaN` on master too, where `cosh(10^k)` is past the exponents.

### A comparison of exact numbers is exact, and one the decimals cannot decide stays written

**Not silent: wrong `True` and `False` answers change.** A comparison of two evaluated numbers went through their
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,15 @@ public static Complex Create(Real real, Real imaginary) =>
/// </summary>
public static Complex Create(EDecimal real, EDecimal imaginary) => Create(real, imaginary, EDecimal.One, EDecimal.One, fromMachineNumbers: false);

/// <summary>
/// The number with exact parts <paramref name="real"/> and <paramref name="imaginary"/>, which is the
/// rational <paramref name="real"/> where <paramref name="imaginary"/> is 0. Exact parts are kept as they
/// are, at any size: <see cref="Create(Real, Real)"/> writes them out to the working digits.
/// https://github.com/asc-community/AngouriMath/issues/2685
/// </summary>
internal static Complex FromExactParts(ERational real, ERational imaginary)
=> imaginary.IsZero ? Rational.Create(real) : new Complex(Rational.Create(real), Rational.Create(imaginary));

/// <summary>
/// A number an arithmetic operation produced, each part zero only as a residual of the cancellation that
/// produced it, as <see cref="Real.CreateAtScale(EDecimal, EDecimal)"/> reads one.
Expand Down
37 changes: 34 additions & 3 deletions Sources/AngouriMath/Core/Entity/Continuous/Number/Operators.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,9 @@ internal static T OpSum<T>(T a, T b) where T : Number =>
(a, b) => Integer.Create(CtxAdd(a.EInteger, b.EInteger)),
(a, b) => Rational.Create(CtxAdd(a.ERational, b.ERational)),
(a, b) => Real.CreateAtScale(CtxAdd(a.EDecimal, b.EDecimal), CancelledScale(a.EDecimal, b.EDecimal)),
(a, b) => Complex.CreateAtScales(CtxAdd(a.RealPart.EDecimal, b.RealPart.EDecimal),
(a, b) => ExactParts(a, b) is { } p
? Complex.FromExactParts(p.Ar.Add(p.Br), p.Ai.Add(p.Bi))
: Complex.CreateAtScales(CtxAdd(a.RealPart.EDecimal, b.RealPart.EDecimal),
CtxAdd(a.ImaginaryPart.EDecimal, b.ImaginaryPart.EDecimal),
CancelledScale(a.RealPart.EDecimal, b.RealPart.EDecimal),
CancelledScale(a.ImaginaryPart.EDecimal, b.ImaginaryPart.EDecimal))
Expand All @@ -126,7 +128,9 @@ internal static T OpSub<T>(T a, T b) where T : Number =>
(a, b) => Integer.Create(CtxSubtract(a.EInteger, b.EInteger)),
(a, b) => Rational.Create(CtxSubtract(a.ERational, b.ERational)),
(a, b) => Real.CreateAtScale(CtxSubtract(a.EDecimal, b.EDecimal), CancelledScaleOfADifference(a.EDecimal, b.EDecimal)),
(a, b) => Complex.CreateAtScales(CtxSubtract(a.RealPart.EDecimal, b.RealPart.EDecimal),
(a, b) => ExactParts(a, b) is { } p
? Complex.FromExactParts(p.Ar.Subtract(p.Br), p.Ai.Subtract(p.Bi))
: Complex.CreateAtScales(CtxSubtract(a.RealPart.EDecimal, b.RealPart.EDecimal),
CtxSubtract(a.ImaginaryPart.EDecimal, b.ImaginaryPart.EDecimal),
CancelledScaleOfADifference(a.RealPart.EDecimal, b.RealPart.EDecimal),
CancelledScaleOfADifference(a.ImaginaryPart.EDecimal, b.ImaginaryPart.EDecimal))
Expand All @@ -139,6 +143,8 @@ internal static T OpMul<T>(T a, T b) where T : Number =>
(a, b) => Real.CreateAtScale(CtxMultiply(a.EDecimal, b.EDecimal), EDecimal.Zero),
(a, b) =>
{
if (ExactParts(a, b) is { } p)
return Complex.FromExactParts(p.Ar.Multiply(p.Br).Subtract(p.Ai.Multiply(p.Bi)), p.Ar.Multiply(p.Bi).Add(p.Ai.Multiply(p.Br)));
// A whole zero against a whole infinity is 0 * oo, which is NaN for a real
// and is NaN here: the reading below, that a zero *part* times an infinite
// part is zero, is for one part against another and read 0 * (i * oo) as
Expand All @@ -158,6 +164,20 @@ static EDecimal ModifiedMultiply(EDecimal a, EDecimal b) =>
}
);

/// <summary>
/// The four parts of <paramref name="a"/> and <paramref name="b"/> where every one is a rational, and null
/// otherwise. Two numbers whose parts are exact have an exact sum, difference, product and quotient, and
/// they are made from the parts as a real one is: through the decimals, a part past ten to the half of the
/// working digits was no longer rounded back onto the rational it is, so that <c>(10^50 + 1/3 + i) - 10^50</c>
/// was <c>0.333...3 + i</c>, <c>(10^60 + i) (10^60 - i)</c> was <c>10^120</c> where it is <c>10^120 + 1</c>, and
/// <c>10^84 i</c> was a decimal, whose exponential has no value past the digits of its argument.
/// https://github.com/asc-community/AngouriMath/issues/2685
/// </summary>
private static (ERational Ar, ERational Ai, ERational Br, ERational Bi)? ExactParts(Complex a, Complex b)
=> a.RealPart is Rational { ERational: var ar } && a.ImaginaryPart is Rational { ERational: var ai }
&& b.RealPart is Rational { ERational: var br } && b.ImaginaryPart is Rational { ERational: var bi }
? (ar, ai, br, bi) : null;

/// <summary>
/// Whether both parts are finite, read from the parts: <see cref="Entity.IsFinite"/> is a cache made on
/// an entity's first read of it, and asked of every power built, and of every exact value an evaluation
Expand Down Expand Up @@ -192,6 +212,14 @@ internal static Complex OpDiv<T>(T a, T b) where T : Number =>
(a, b) => Real.CreateAtScale(CtxDivide(a.EDecimal, b.EDecimal), EDecimal.Zero),
(a, b) =>
{
if (ExactParts(a, b) is { } p)
{
var squaredModulus = p.Br.Multiply(p.Br).Add(p.Bi.Multiply(p.Bi));
if (squaredModulus.IsZero)
return Real.NaN;
return Complex.FromExactParts(p.Ar.Multiply(p.Br).Add(p.Ai.Multiply(p.Bi)).Divide(squaredModulus),
p.Ai.Multiply(p.Br).Subtract(p.Ar.Multiply(p.Bi)).Divide(squaredModulus));
}
/*
* (a + ib) / (c + id) = (a + ib) * (1 / (c + id))
* 1 / (c + id) = (c2 + d2) / (c + id) / (c2 + d2) = (c - id) / (c2 + d2)
Expand Down Expand Up @@ -1338,7 +1366,10 @@ internal static Complex ExpDifference(Complex v, Complex d)
using (MathS.Settings.DowncastingEnabled.Set(false))
using (MathS.Settings.DecimalPrecisionContext.Set(work))
{
power = Exp(Complex.Create(x, y));
// An exact d as it is: written out as decimals, 2 * 10^100 i was past the working digits, whose
// exponential has no value, and sinh(10^100 i) was NaN where it is i sin(10^100).
// https://github.com/asc-community/AngouriMath/issues/2685
power = Exp(d.IsExact ? d : Complex.Create(x, y));
lower = Exp(v);
product = lower * Complex.CreateAtScales(power.RealPart.EDecimal.Subtract(EDecimal.One, work), power.ImaginaryPart.EDecimal, EDecimal.Zero, EDecimal.Zero);
}
Expand Down
70 changes: 35 additions & 35 deletions Sources/Tests/DotnetBenchmark/performance-baseline.json
Original file line number Diff line number Diff line change
@@ -1,94 +1,94 @@
{
"comment": "Allocated bytes and mean nanoseconds per operation for the popular use cases of https://github.com/asc-community/AngouriMath/issues/746. Checked by PerformanceGate.cs; see Sources/AngouriMath/Docs/WhatsNew/version_performance_control.md for when updating it is legitimate.",
"benchmark": "CommonFunctionsInterVersion",
"commit": "6a300d9e0972febc6121b5262a2ab53e7a0c5875",
"measuredOn": "2026-11-29",
"commit": "3c40ee59ee7daa6f5206aecff9a4168ff009c0b3",
"measuredOn": "2026-12-03",
"runtime": ".NET 10.0.15",
"machine": "Ubuntu 26.04.1 LTS, X64, 4 logical cores",
"cases": {
"CompileEasy": {
"allocatedBytes": 11610,
"meanNanoseconds": 371164.9079402043
"allocatedBytes": 11626,
"meanNanoseconds": 344126.05332728795
},
"CompileHard": {
"allocatedBytes": 20002,
"meanNanoseconds": 449587.0790264423
"allocatedBytes": 20020,
"meanNanoseconds": 412609.3494140625
},
"Derivate": {
"allocatedBytes": 24363,
"meanNanoseconds": 10749.053087087777
"allocatedBytes": 24563,
"meanNanoseconds": 11312.487815455386
},
"EvalEasy": {
"allocatedBytes": 0,
"meanNanoseconds": 4.677396980779512
"meanNanoseconds": 4.936430067888328
},
"EvalPolynomialFresh": {
"allocatedBytes": 11768,
"meanNanoseconds": 6405.616791788737
"meanNanoseconds": 6707.986476135254
},
"EvalPolynomialFresh15Digits": {
"allocatedBytes": 11344,
"meanNanoseconds": 6738.561392211914
"meanNanoseconds": 6906.78533063616
},
"EvalTranscendentalFresh": {
"allocatedBytes": 34128,
"meanNanoseconds": 24908.826869084285
"allocatedBytes": 34192,
"meanNanoseconds": 25228.853013258715
},
"EvalTrig": {
"allocatedBytes": 69368,
"meanNanoseconds": 50154.798732212614
"meanNanoseconds": 50369.36917317708
},
"EvalTrigPrecise": {
"allocatedBytes": 336082,
"meanNanoseconds": 647531.088297526
"meanNanoseconds": 617807.6335100447
},
"ParseEasy": {
"allocatedBytes": 18200,
"meanNanoseconds": 8551.643139648437
"meanNanoseconds": 8853.872195434571
},
"ParseHard": {
"allocatedBytes": 58944,
"meanNanoseconds": 36917.50787760417
"allocatedBytes": 58824,
"meanNanoseconds": 38975.81295776367
},
"RunEasy": {
"allocatedBytes": 0,
"meanNanoseconds": 23.513746988773345
"meanNanoseconds": 29.61389327843984
},
"RunHard": {
"allocatedBytes": 0,
"meanNanoseconds": 322.97689793904624
"meanNanoseconds": 377.49524835745495
},
"RunMedium": {
"allocatedBytes": 0,
"meanNanoseconds": 200.21510960261028
"meanNanoseconds": 224.6864652633667
},
"SimplifyEasy": {
"allocatedBytes": 28120,
"meanNanoseconds": 40918.72411237444
"allocatedBytes": 28121,
"meanNanoseconds": 42829.61381748744
},
"SimplifyHard": {
"allocatedBytes": 134601944,
"meanNanoseconds": 103344955.84615384
"allocatedBytes": 134913208,
"meanNanoseconds": 117878599.10714285
},
"SolveEasy": {
"allocatedBytes": 419507,
"meanNanoseconds": 300131.19301060267
"allocatedBytes": 420399,
"meanNanoseconds": 325486.75798152044
},
"SolveEasyMedium": {
"allocatedBytes": 65798,
"meanNanoseconds": 28011.18859354655
"allocatedBytes": 65717,
"meanNanoseconds": 29347.054384358726
},
"SolveHard": {
"allocatedBytes": 9183808,
"meanNanoseconds": 103956216.07142857
"allocatedBytes": 6209376,
"meanNanoseconds": 96410912.92307693
},
"SolveMedium": {
"allocatedBytes": 452169,
"meanNanoseconds": 412951.9587965745
"allocatedBytes": 456039,
"meanNanoseconds": 412897.28899739584
},
"SolveMediumHard": {
"allocatedBytes": 1404280,
"meanNanoseconds": 11842235.96763393
"allocatedBytes": 1408652,
"meanNanoseconds": 13213132.880580356
}
},
"ungated": {
Expand Down
Loading
Loading