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
66 changes: 64 additions & 2 deletions BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,67 @@ A not-equal written `<>` is read as `not (a = b)`, and printed that way.

## Unreleased — since 2.10.0

### Evaluation keeps its digits from 1e-100000 to 1e100000

**Silent: wrong numbers become right ones.** The decimals evaluation works in had a hundred digits, but exponents only
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
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
equation in `10^20 x` evaluated to points that are not roots. The exponents now run from -100000 to 100000. The sine,
cosine and arctangent of a tiny argument are summed as series in floating point, where their fixed point had no digits
left. The sine and cosine of a large argument are reduced by `2 pi` taken to as many more digits as the argument has
before its point, and an exact rational argument is written out to that many digits too. Before, `sin(10^500)` had no
right digit. A caller that sets its own `DecimalPrecisionContext` keeps that context's range. Measured on 2.10.0 and on
this branch ([#2668](https://github.com/asc-community/AngouriMath/issues/2668)).

| `.Evaled` | Was (2.10.0) | Now |
|---|---|---|
| `sqrt(1/10^200 + 1/10^210) * 10^100` | `0` | `1.00000000004999999999875…` |
| `sqrt(10^(-300)) * 10^150` | `0` | `1` |
| `(1/10^200 + i/10^200) * 10^200` | `1` | `1 + i` |
| `2^(-1000) * sqrt(2) * 2^1000` | `0` | `1.41421356237309504880…` |
| `exp(-1000)` | `0` | `5.07595889754945676529…E-435` |
| `exp(3000)` | `+oo` | `7.64620098905470488931…E+1302` |
| `exp(3000) * exp(-3000)` | `NaN` | `1` |
| `sin(1/10^200) * 10^200`, and `arctan` | `0` | `1` |
| `sin(10^60)` | `0.83038976521934266466…8514636898040…`, wrong from the 49th digit | `…8514630714107…` |
| `sin(10^500)` | `-0.27853856742087907707…` | `0.31102693090790028047…` |
| `sin(2^1000)` | `0.29150829785727846625…` | `-0.15920170308624243824…` |
| `cos(10^5000)` | `NaN` | `-0.72745055141283323318…` |
| `gamma(316 * i)` | `0` | `3.55632285366604152450…E-217 + 1.28845172909637752912…E-217i` |
| `catalannumber(2000.5)` | `+oo` | `1.66144399474248826784…E+1199` |
| `(1e600 + 1e600 * i)^2` | `+ooi` | `2.000…E+1200i` |
| `cotan(1 - 3000i)` | `i` | `3.1106026543…E-2606 + i` |
| `binomial(10^5000, 1.5)` | `0.75225277806…`, where it is about `7.5E+7499` | stays written |
| `zeta(-10^900)` | throws `ArgumentException` | stays written |

The range still ends: `exp(-240000)` is 0 and `(1 + i)^(10^6)` is `+oo`, and a comparison of two such sides stays written.
An exponential past it is decided from the size of its argument, so `sinh(exp(10000))` is `+oo` at once. The special
functions (`erf`, `erfc`, `erfi`, `Ei`, `Si`, `Ci`, `Shi`, `Chi`, the gamma function, and a quotient of factorials), and
the complex paths of the exponential, the trigonometric functions and `arcsin`/`arccos`, take a part past `10^1000` as
the infinity it was before. Their algorithms do work that grows with the argument's digits, and some lose every digit
there. Their answers past `10^1000` are therefore what they were.

**Not silent: a periodic function of a decimal past its digits is `NaN`.** A decimal argument is known only to the
working digits. Reduced by `2 pi`, an argument of size `10^k` keeps `precision - k` of them, so the sine, cosine,
tangent and cotangent of a decimal past `10^(precision - 16)` was a number whose digits were all, or nearly all, noise.
The same holds on the complex paths through them: `e` to an imaginary power, and a power by the polar form. Such a
value is now `NaN`, which says the evaluation has none to give. An exact rational argument is reduced exactly, at any
size. `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. `Ci` of such a decimal is `NaN`. So is `sin(x)^2 + cos(x)^2` evaluated numerically at such
an `x`, where it was `1` because the noise was consistent. Measured on 2.10.0 and on this branch; the true values are
mpmath's at 4500 digits.

| `.Evaled` | Was (2.10.0) | Now | True |
|---|---|---|---|
| `cos(exp(300))` | `0.23214…` | `NaN` | `-0.98403…` |
| `sin(exp(300))` | `0.97268…` | `NaN` | `0.17801…` |
| `cos(sqrt(2)*10^200)` | `-0.76230…` | `NaN` | `0.018225…` |
| `cos(pi*10^200)` | `-0.99338…` | `NaN` | `1.0` |
| `tan(exp(500))` | `-0.55478…` | `NaN` | `0.52510…` |
| `e^(i*exp(300))` | `0.23214…` | `NaN` | `-0.98403… + 0.17801…i` |
| `Ci(exp(300))` | `0` | `NaN` | below `1e-130`, sign unknown |
| `cos(10^150/3)` | `-0.44819…` | `0.80881903093981837173…` | `0.80881903093981837173…` |

### A number past the working digits is not taken for an exact one

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

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

/// <summary>
/// Whether functions are being read as real-valued or complex-valued. It is a
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ partial record Number
/// </remarks>
public static Complex Erf(Complex z)
{
z = WithinTheSpecialFunctionsReach(z);
var context = MathS.Settings.DecimalPrecisionContext.Value;
var (re, im) = (z.RealPart.EDecimal, z.ImaginaryPart.EDecimal);
if (re.IsNaN() || im.IsNaN())
Expand Down Expand Up @@ -73,6 +74,7 @@ public static Complex Erf(Complex z)
/// </remarks>
public static Complex Erfc(Complex z)
{
z = WithinTheSpecialFunctionsReach(z);
var context = MathS.Settings.DecimalPrecisionContext.Value;
var (re, im) = (z.RealPart.EDecimal, z.ImaginaryPart.EDecimal);
if (re.IsNaN() || im.IsNaN())
Expand Down Expand Up @@ -120,6 +122,7 @@ public static Complex Erfc(Complex z)
/// </remarks>
public static Complex Erfi(Complex z)
{
z = WithinTheSpecialFunctionsReach(z);
// On the real line, where it is real, it is summed as a real function, so that its
// imaginary part is zero and not a rounding residue; it grows without bound.
if (z.ImaginaryPart.EDecimal.IsZero && z.RealPart.EDecimal is var x && !x.IsNaN())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ partial record Number
/// </remarks>
public static Complex Ei(Complex z)
{
z = WithinTheSpecialFunctionsReach(z);
var context = MathS.Settings.DecimalPrecisionContext.Value;
var (re, im) = (z.RealPart.EDecimal, z.ImaginaryPart.EDecimal);
if (re.IsNaN() || im.IsNaN())
Expand All @@ -56,6 +57,17 @@ public static Complex Ei(Complex z)
return im.IsZero && re.IsInfinity() ? (re.IsNegative ? Integer.Zero : Real.PositiveInfinity) : Real.NaN;
if (re.IsZero && im.IsZero)
return Real.NegativeInfinity;
// Off the real line at an imaginary part from 10^(precision - 16), the cosine and the sine of which have
// 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
// value, Ei(10^130 i) among them, and otherwise there is none.
// https://github.com/asc-community/AngouriMath/issues/2668
if (!im.IsZero && im.CompareToTotalMagnitude(tenToTheEight) >= 0
&& im.Exponent.Add(im.Precision()).CompareTo(context.Precision.Subtract(16)) > 0)
{
var digitsOfTheRest = re.ToDouble() / Math.Log(10) - ((double)im.Exponent.Add(im.Precision()).ToInt64Checked() - 1);
if (digitsOfTheRest < -(context.Precision.ToInt32Checked() + 2))
return Complex.Create(EDecimal.Zero, im.IsNegative ? EDecimal.PI(context).Negate() : EDecimal.PI(context));
}
var ei = Ei(re, im, context, extraDigits: 0);
// A part is a residual only against what was added to make it, not against 1: Ei(-130) is about
// -2.7 * 10^(-59). https://github.com/asc-community/AngouriMath/issues/1944
Expand Down
Loading
Loading