Skip to content

Commit 4674e66

Browse files
Keep evaluation's digits from 1e-100000 to 1e100000 (#2668) (#2674)
The default DecimalPrecisionContext had a hundred digits but exponents from -100 to 1000 only. An exact part below 10^-100 became 0 as a decimal and one past 10^1000 became +oo, so every digit built on it was wrong: sqrt(1/10^200 + 1/10^210) * 10^100 was 0, exp(3000) * exp(-3000) was NaN, and the exact roots Solve writes for an equation in 10^20 x evaluated to points with a residual of 1.36. The range is now -100000 to 100000, both ways, so that a number and its reciprocal are both held. With the range open, three precision limits that the flush had hidden showed, and are fixed with it: - the sine, cosine and arctangent ran in fixed point, whose guard digits are absolute, so below 10^-8 (10^-5 for the arctangent) they lost digits and at 10^-200 gave 0; they are now summed as series in floating point there; - the reduction of a sine's argument by 2 pi used pi to the working digits, so from 10^8 up it lost a digit per digit of the argument, and sin(10^500) had none right; it is now reduced with pi to as many more digits as the argument has, and an exact rational argument is written out to that many digits; - the Hermite reduction over two binomials of degree three or four was declined only because its check overflowed. Over the range, its answer to x^7 (a + b x^3)^(1/3)/(c + d x^3) is 23,030 characters, where the cube root's rules give 2,861. It is now declined for that pair, as the split by residues already declines it. Tests changed because their answers got better: the flushed gamma parts are asked for in a context ending at 1e-100, as the default did, with the default's values pinned beside them; catalannumber(2000.5), (1e600 + 1e600 i)^2 and three comparisons were +oo or stayed written, and are now their values, mpmath's. The rows still past the range moved past the new ends. Widening the exponents let finite numbers past 1e1000 reach algorithms that had only ever seen +oo there, and some of them work through the argument's digits: - e^x for a huge x was taken by PeterO's exponential, which works its way to the overflow through a power of e as large as x, and inside ExpDifference in a context with no exponent range. sinh(exp(10000)) took 38 s to be +oo, and limitcheck went from 28 s to 44.5 s. Past ln 10 times the context's ends, e^x is now +oo or 0 from x's size alone, and e^v (e^d - 1) is e^(v+d) or -e^v once d is past them. - erf, erfc, erfi, Ei, Si, Ci, Shi, Chi, the gamma function, a quotient of factorials, polygamma off the real line, and the complex paths of exp, the trigonometric functions and arcsin/arccos did not finish in 20 s at 10^5000, or lost every digit to their residual scales. sin(10^5000 + i) had no imaginary part, and arcsin(10^5000) was 0. They take a part past 10^1000 as the infinity it was, so their answers there are master's. - e to a complex power now goes through Exp, as e to a real power does: the polar form multiplied the imaginary part by ln e's hundred digits, so cosh(10^5000 i) was -0.86 where it is cos(10^5000), -0.73. - zeta past a double's range counted its terms through doubles and threw on a negative precision, which zeta(-10^900) did on master too. It is left as written past a million. Measured: limitcheck 27.7/28.0 s on master and 28.3/28.1 s here, with 2 more cases compared and the same 0 wrong; sinh(exp(10000)) 38,604 ms -> 131 ms in-process. Suite 21993 passed. A decimal argument is known to the working digits, so reducing it by 2 pi leaves precision - k of them for an argument near 10^k. Past 10^(precision - 16) the sine and cosine were mostly noise: cos(exp(300)) was 0.23 where it is -0.98. Widening the exponents let values past 10^1000 reach the same reduction, so cos(exp(10000)), which was NaN, became 0.31 where it is 0.92. SinAndCos is now NaN for a decimal past that bound. An exact rational is written out to the digits its reduction needs and goes through SinAndCosOfAnExactArgument at any size, so cos(10^150 + 1/3) and cos(10^150/3) are right. Si of such a decimal is +-pi/2, and Ei far up the imaginary axis is +-i pi, where the oscillating part is below the working digits. Suite 22012 passed; limitcheck 27.4/27.9 s on master, 27.7/28.1 s here. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent e1b3327 commit 4674e66

16 files changed

Lines changed: 528 additions & 21 deletions

‎BREAKING-CHANGES.md‎

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,67 @@ A not-equal written `<>` is read as `not (a = b)`, and printed that way.
4242

4343
## Unreleased — since 2.10.0
4444

45+
### Evaluation keeps its digits from 1e-100000 to 1e100000
46+
47+
**Silent: wrong numbers become right ones.** The decimals evaluation works in had a hundred digits, but exponents only
48+
from -100 to 1000. An exact part below `10^-100` was 0 as a decimal, and one past `10^1000` was `+oo`, so every digit
49+
built on such a part was wrong: `sqrt(1/10^200 + 1/10^210) * 10^100` was 0, and the exact roots `Solve` writes for an
50+
equation in `10^20 x` evaluated to points that are not roots. The exponents now run from -100000 to 100000. The sine,
51+
cosine and arctangent of a tiny argument are summed as series in floating point, where their fixed point had no digits
52+
left. The sine and cosine of a large argument are reduced by `2 pi` taken to as many more digits as the argument has
53+
before its point, and an exact rational argument is written out to that many digits too. Before, `sin(10^500)` had no
54+
right digit. A caller that sets its own `DecimalPrecisionContext` keeps that context's range. Measured on 2.10.0 and on
55+
this branch ([#2668](https://github.com/asc-community/AngouriMath/issues/2668)).
56+
57+
| `.Evaled` | Was (2.10.0) | Now |
58+
|---|---|---|
59+
| `sqrt(1/10^200 + 1/10^210) * 10^100` | `0` | `1.00000000004999999999875…` |
60+
| `sqrt(10^(-300)) * 10^150` | `0` | `1` |
61+
| `(1/10^200 + i/10^200) * 10^200` | `1` | `1 + i` |
62+
| `2^(-1000) * sqrt(2) * 2^1000` | `0` | `1.41421356237309504880…` |
63+
| `exp(-1000)` | `0` | `5.07595889754945676529…E-435` |
64+
| `exp(3000)` | `+oo` | `7.64620098905470488931…E+1302` |
65+
| `exp(3000) * exp(-3000)` | `NaN` | `1` |
66+
| `sin(1/10^200) * 10^200`, and `arctan` | `0` | `1` |
67+
| `sin(10^60)` | `0.83038976521934266466…8514636898040…`, wrong from the 49th digit | `…8514630714107…` |
68+
| `sin(10^500)` | `-0.27853856742087907707…` | `0.31102693090790028047…` |
69+
| `sin(2^1000)` | `0.29150829785727846625…` | `-0.15920170308624243824…` |
70+
| `cos(10^5000)` | `NaN` | `-0.72745055141283323318…` |
71+
| `gamma(316 * i)` | `0` | `3.55632285366604152450…E-217 + 1.28845172909637752912…E-217i` |
72+
| `catalannumber(2000.5)` | `+oo` | `1.66144399474248826784…E+1199` |
73+
| `(1e600 + 1e600 * i)^2` | `+ooi` | `2.000…E+1200i` |
74+
| `cotan(1 - 3000i)` | `i` | `3.1106026543…E-2606 + i` |
75+
| `binomial(10^5000, 1.5)` | `0.75225277806…`, where it is about `7.5E+7499` | stays written |
76+
| `zeta(-10^900)` | throws `ArgumentException` | stays written |
77+
78+
The range still ends: `exp(-240000)` is 0 and `(1 + i)^(10^6)` is `+oo`, and a comparison of two such sides stays written.
79+
An exponential past it is decided from the size of its argument, so `sinh(exp(10000))` is `+oo` at once. The special
80+
functions (`erf`, `erfc`, `erfi`, `Ei`, `Si`, `Ci`, `Shi`, `Chi`, the gamma function, and a quotient of factorials), and
81+
the complex paths of the exponential, the trigonometric functions and `arcsin`/`arccos`, take a part past `10^1000` as
82+
the infinity it was before. Their algorithms do work that grows with the argument's digits, and some lose every digit
83+
there. Their answers past `10^1000` are therefore what they were.
84+
85+
**Not silent: a periodic function of a decimal past its digits is `NaN`.** A decimal argument is known only to the
86+
working digits. Reduced by `2 pi`, an argument of size `10^k` keeps `precision - k` of them, so the sine, cosine,
87+
tangent and cotangent of a decimal past `10^(precision - 16)` was a number whose digits were all, or nearly all, noise.
88+
The same holds on the complex paths through them: `e` to an imaginary power, and a power by the polar form. Such a
89+
value is now `NaN`, which says the evaluation has none to give. An exact rational argument is reduced exactly, at any
90+
size. `Si` of such a decimal is `±pi/2`, and `Ei` far up the imaginary axis is `±i pi`, where the oscillating part is
91+
below the working digits. `Ci` of such a decimal is `NaN`. So is `sin(x)^2 + cos(x)^2` evaluated numerically at such
92+
an `x`, where it was `1` because the noise was consistent. Measured on 2.10.0 and on this branch; the true values are
93+
mpmath's at 4500 digits.
94+
95+
| `.Evaled` | Was (2.10.0) | Now | True |
96+
|---|---|---|---|
97+
| `cos(exp(300))` | `0.23214…` | `NaN` | `-0.98403…` |
98+
| `sin(exp(300))` | `0.97268…` | `NaN` | `0.17801…` |
99+
| `cos(sqrt(2)*10^200)` | `-0.76230…` | `NaN` | `0.018225…` |
100+
| `cos(pi*10^200)` | `-0.99338…` | `NaN` | `1.0` |
101+
| `tan(exp(500))` | `-0.55478…` | `NaN` | `0.52510…` |
102+
| `e^(i*exp(300))` | `0.23214…` | `NaN` | `-0.98403… + 0.17801…i` |
103+
| `Ci(exp(300))` | `0` | `NaN` | below `1e-130`, sign unknown |
104+
| `cos(10^150/3)` | `-0.44819…` | `0.80881903093981837173…` | `0.80881903093981837173…` |
105+
45106
### A number past the working digits is not taken for an exact one
46107

47108
**Not silent: wrong answers become exact ones.** At the default hundred digits, a product past about `10^50`
@@ -83,8 +144,9 @@ before v3, which decides exact comparisons exactly (#1019 §10). Two decimals th
83144
| `10^(-200) < 2 * 10^(-200)` | `False` | `True` |
84145
| `1000! * 2 < 1001!` | `False` | `True` |
85146
| `10^(590) >= 10^(590) + 10^(440)` | `True` | `False` |
86-
| `exp(-1000.5) > exp(-3002.5)` | `False` | stays written |
87-
| `exp(-2000) = exp(-2000.5)` | `True` | stays written |
147+
| `exp(-1000.5) > exp(-3002.5)` | `False` | `True`, both sides within the exponents since [#2668](https://github.com/asc-community/AngouriMath/issues/2668) |
148+
| `exp(-2000) = exp(-2000.5)` | `True` | `False`, likewise |
149+
| `exp(-240000) = exp(-240000.5)` | `True` | stays written |
88150
| `sqrt(5)^1000 = 5^500` | `False` | stays written |
89151
| `sqrt(2) < sqrt(2) + 10^(-150)` | `False` | stays written |
90152

‎Sources/AngouriMath/Convenience/MathS.cs‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6776,8 +6776,14 @@ internal static EDecimal DowncastingTolerance
67766776
/// <summary>
67776777
/// Settings for <see cref="EDecimal"/> precisions of <a href="https://github.com/peteroupc/Numbers">PeterO.Numbers</a>
67786778
/// </summary>
6779+
/// <remarks>
6780+
/// A hundred digits, at any magnitude from 1e-100000 to 1e100000. The exponents ended at -100 and
6781+
/// 1000, so that an exact part below 1e-100 was 0 as a decimal and every digit built on it was
6782+
/// wrong: <c>sqrt(1/10^200 + 1/10^210) * 10^100</c> was 0, and <c>exp(3000) * exp(-3000)</c> was
6783+
/// NaN. https://github.com/asc-community/AngouriMath/issues/2668
6784+
/// </remarks>
67796785
public static Setting<EContext> DecimalPrecisionContext { get; } =
6780-
new(new EContext(100, ERounding.HalfUp, -100, 1000, false));
6786+
new(new EContext(100, ERounding.HalfUp, -100000, 100000, false));
67816787

67826788
/// <summary>
67836789
/// Whether functions are being read as real-valued or complex-valued. It is a

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ partial record Number
4444
/// </remarks>
4545
public static Complex Erf(Complex z)
4646
{
47+
z = WithinTheSpecialFunctionsReach(z);
4748
var context = MathS.Settings.DecimalPrecisionContext.Value;
4849
var (re, im) = (z.RealPart.EDecimal, z.ImaginaryPart.EDecimal);
4950
if (re.IsNaN() || im.IsNaN())
@@ -73,6 +74,7 @@ public static Complex Erf(Complex z)
7374
/// </remarks>
7475
public static Complex Erfc(Complex z)
7576
{
77+
z = WithinTheSpecialFunctionsReach(z);
7678
var context = MathS.Settings.DecimalPrecisionContext.Value;
7779
var (re, im) = (z.RealPart.EDecimal, z.ImaginaryPart.EDecimal);
7880
if (re.IsNaN() || im.IsNaN())
@@ -120,6 +122,7 @@ public static Complex Erfc(Complex z)
120122
/// </remarks>
121123
public static Complex Erfi(Complex z)
122124
{
125+
z = WithinTheSpecialFunctionsReach(z);
123126
// On the real line, where it is real, it is summed as a real function, so that its
124127
// imaginary part is zero and not a rounding residue; it grows without bound.
125128
if (z.ImaginaryPart.EDecimal.IsZero && z.RealPart.EDecimal is var x && !x.IsNaN())

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ partial record Number
4848
/// </remarks>
4949
public static Complex Ei(Complex z)
5050
{
51+
z = WithinTheSpecialFunctionsReach(z);
5152
var context = MathS.Settings.DecimalPrecisionContext.Value;
5253
var (re, im) = (z.RealPart.EDecimal, z.ImaginaryPart.EDecimal);
5354
if (re.IsNaN() || im.IsNaN())
@@ -56,6 +57,17 @@ public static Complex Ei(Complex z)
5657
return im.IsZero && re.IsInfinity() ? (re.IsNegative ? Integer.Zero : Real.PositiveInfinity) : Real.NaN;
5758
if (re.IsZero && im.IsZero)
5859
return Real.NegativeInfinity;
60+
// Off the real line at an imaginary part from 10^(precision - 16), the cosine and the sine of which have
61+
// no digits to give, Ei is i pi sgn(Im z) beside e^z/z; where that is below the working digits it is the
62+
// value, Ei(10^130 i) among them, and otherwise there is none.
63+
// https://github.com/asc-community/AngouriMath/issues/2668
64+
if (!im.IsZero && im.CompareToTotalMagnitude(tenToTheEight) >= 0
65+
&& im.Exponent.Add(im.Precision()).CompareTo(context.Precision.Subtract(16)) > 0)
66+
{
67+
var digitsOfTheRest = re.ToDouble() / Math.Log(10) - ((double)im.Exponent.Add(im.Precision()).ToInt64Checked() - 1);
68+
if (digitsOfTheRest < -(context.Precision.ToInt32Checked() + 2))
69+
return Complex.Create(EDecimal.Zero, im.IsNegative ? EDecimal.PI(context).Negate() : EDecimal.PI(context));
70+
}
5971
var ei = Ei(re, im, context, extraDigits: 0);
6072
// A part is a residual only against what was added to make it, not against 1: Ei(-130) is about
6173
// -2.7 * 10^(-59). https://github.com/asc-community/AngouriMath/issues/1944

0 commit comments

Comments
 (0)