Skip to content

Commit 2045021

Browse files
Search for a root of a small or a large number at the size of one (#2067)
* Search for a root of a small or a large number at the size of one FindGoodRoot looks for an exact root. It rounded each candidate with the zero range set to 1e-6 and accepted a rational one whose power was within 1e-6 of the base, both absolutely. A root below 1e-6 became the integer 0, and 0 ^ q is within 1e-6 of a base below it, so sqrt(2/10^20), (3/10^30)^(1/3) and sqrt(i/10^20) were 0. And the rounded candidate was what it returned as the real root when no rational one held: the root of 10^12 + 1/2 was 10^6, and (2 + i/10^8)^(1/2) lost its imaginary part. The base is scaled to the size of one by an exact power of ten and the root scaled back, so sqrt(1/10^20) is 1/10^10 exactly. A candidate is rounded only to look for a rational root, which is accepted where its power is the base at the working precision; a real root is the candidate as computed. The real cube root of a negative base stays the one given: (-2/10^21)^(1/3) is -1.26e-7, as (-8)^(1/3) is -2. Fixes #2065. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura * Take the real root as the root search made it The real root is the candidate as GetAllRoots computed it at the working precision, where it was made a second time from its parts for every candidate: (-27)^(1/3) allocated 8.0% more than master, sqrt(2) 1.4%. Now both are within 0.1% of master, measured per call. Part of #2065. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura --------- Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent bd19bda commit 2045021

3 files changed

Lines changed: 121 additions & 6 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,25 @@ not seen, and a symmetric divergent one comes to its principal value
6262
| `"ln(x)".ToEntity().DefiniteIntegral("x", 0, 1)` | `NaN` | `-1.00000` |
6363
| `"1/x".ToEntity().DefiniteIntegral("x", 0, 1)` | `NaN` | `NaN` |
6464

65+
### A root of a small or a large number is searched for at the size of one
66+
67+
**Silent: wrong answers.** The search for an exact root of a number rounded each candidate to six digits, absolutely.
68+
A root below `1e-6` became the integer `0`, which was accepted because `0 ^ q` is within `1e-6` of a base below it; and
69+
a rounded candidate was returned as the real root even where its power was not the base. It now searches with the
70+
base scaled to the size of one by an exact power of ten. It accepts a rational root only where its power is the base
71+
at the working precision, and returns a real root as it was computed
72+
([#2065](https://github.com/asc-community/AngouriMath/issues/2065)).
73+
74+
| Input | Was (2.6.0) | Now |
75+
|---|---|---|
76+
| `"sqrt(2/10^20)".EvalNumerical()` | `0` | `1.4142135623730950…E-10` |
77+
| `"sqrt(1/10^20)".EvalNumerical()` | `0` | `1/10000000000` |
78+
| `"(-2/10^21)^(1/3)".EvalNumerical()` | `0` | `-1.2599210498948731…E-7` |
79+
| `"sqrt(i/10^20)".EvalNumerical()` | `0` | `7.0710678118654752…E-11 + 7.0710678118654752…E-11i` |
80+
| `"(10^12 + 1/2)^(1/2)".EvalNumerical()` | `1000000` | `1000000.00000024999999999996875…` |
81+
| `"(2 + i/10^8)^(1/2)".EvalNumerical()` | `1.4142135623730950…` | `1.4142135623730950… + 3.5355339059327376…E-9i` |
82+
| `"(-8)^(1/3)".EvalNumerical()`, `"sqrt(10^30)".EvalNumerical()` | `-2`, `1000000000000000` | `-2`, `1000000000000000` |
83+
6584
### An equality in an equation or a system is read as a difference equal to zero
6685

6786
**Silent: wrong answers.** `MathS.SolveEquation`, `Entity.SolveEquation` and `MathS.Equations` read each equation as an

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

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -330,17 +330,44 @@ private static bool CanHaveARealRoot(Complex @base, EInteger rootPower) =>
330330
@base is not Real { IsNegative: true } || !rootPower.IsEven;
331331

332332
internal static Real? FindGoodRoot(Complex @base, Integer power)
333+
{
334+
// Searched at the size of one, by an exact power of ten: the candidates are rounded with
335+
// the zero range at 1e-6, so a root below it was the integer 0, and 0 ^ q was within 1e-6
336+
// of the base, so sqrt(1/10^20) was 0. Scaled, it is sqrt(1) / 10^10.
337+
// https://github.com/asc-community/AngouriMath/issues/2065
338+
var size = EDecimal.Max(@base.RealPart.EDecimal.Abs(), @base.ImaginaryPart.EDecimal.Abs());
339+
if (!size.IsZero && size.IsFinite)
340+
{
341+
var shift = size.Exponent.Add(size.Precision()).Subtract(1).Divide(power.EInteger);
342+
if (!shift.IsZero)
343+
{
344+
Real factor = Integer.Create(EInteger.FromInt32(10).Pow(shift.Abs()));
345+
Complex power10 = Integer.Create(EInteger.FromInt32(10).Pow(shift.Abs().Multiply(power.EInteger)));
346+
var scaled = shift.Sign < 0 ? @base * power10 : @base / power10;
347+
return FindGoodRootNearOne(scaled, power) is { } root ? (shift.Sign < 0 ? root / factor : root * factor) : null;
348+
}
349+
}
350+
return FindGoodRootNearOne(@base, power);
351+
}
352+
353+
private static Real? FindGoodRootNearOne(Complex @base, Integer power)
333354
{
334355
Real? positive = null, real = null;
335356
foreach (var root in GetAllRoots(@base, power.EInteger))
336357
{
337-
using var _ = MathS.Settings.FloatToRationalIterCount.Set(15);
338-
using var __ = MathS.Settings.PrecisionErrorZeroRange.Set(1e-6m);
339-
var toCheck = Complex.Create(root.RealPart, root.ImaginaryPart);
340-
switch (toCheck)
358+
// Rounded to six digits only to look for a rational root, which is that root only if its
359+
// power is the base at the working precision: within 1e-6, sqrt(10^12 + 1/2) was 10^6.
360+
// A real root is the candidate as computed, never the rounded one.
361+
// https://github.com/asc-community/AngouriMath/issues/2065
362+
Complex rounded;
363+
using (MathS.Settings.FloatToRationalIterCount.Set(15))
364+
using (MathS.Settings.PrecisionErrorZeroRange.Set(1e-6m))
365+
rounded = Complex.Create(root.RealPart, root.ImaginaryPart);
366+
if (rounded is Rational rational && IsZero(Pow(rational, power) - @base)) // To keep user's desired precision
367+
return rational;
368+
// The root as GetAllRoots made it, at the working precision.
369+
switch (root)
341370
{
342-
case Rational rational when IsZero(Pow(rational, power) - @base): // To keep user's desired precision
343-
return rational;
344371
case Real r when r > 0:
345372
positive ??= r;
346373
break;
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
//
2+
// Copyright (c) 2019-2026 Angouri.
3+
// AngouriMath is licensed under MIT.
4+
// Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.
5+
// Website: https://am.angouri.org.
6+
//
7+
8+
using AngouriMath.Extensions;
9+
using PeterO.Numbers;
10+
using Xunit;
11+
using static AngouriMath.Entity.Number;
12+
13+
namespace AngouriMath.Tests.Core
14+
{
15+
/// <summary>
16+
/// The search for an exact root rounds its candidates to six digits. A root below 1e-6 was the integer 0, which was
17+
/// accepted because 0 ^ q is within 1e-6 of a base below it; and a rounded candidate was returned as the real root
18+
/// even where its power was not the base. It searches at the size of one, by an exact power of ten, accepts a
19+
/// rational root only where its power is the base at the working precision, and returns a real root as computed.
20+
/// References from mpmath.
21+
/// <a href="https://github.com/asc-community/AngouriMath/issues/2065">#2065</a>
22+
/// </summary>
23+
[Trait("Area", "Core")]
24+
public sealed class ARootOfASmallNumberTest
25+
{
26+
private static void AssertClose(string reference, EDecimal actual, string relativeTolerance = "1E-40")
27+
{
28+
var expected = EDecimal.FromString(reference);
29+
var error = actual.Subtract(expected, EContext.ForPrecision(200)).Abs().Divide(expected.Abs(), EContext.ForPrecision(10));
30+
Assert.True(error.LessThan(EDecimal.FromString(relativeTolerance)), $"{actual} where it is {expected}");
31+
}
32+
33+
[Theory]
34+
[InlineData("sqrt(2/10^20)", "1.41421356237309504880168872420969807856967187537694807317668E-10")]
35+
[InlineData("(3/10^30)^(1/3)", "1.44224957030740838232163831078010958839186925349935057754642E-10")]
36+
// The real root, as (-8)^(1/3) is -2.
37+
[InlineData("(-2/10^21)^(1/3)", "-1.25992104989487316476721060727822835057025146470150798008198E-7")]
38+
[InlineData("(10^12 + 1/2)^(1/2)", "1000000.00000024999999999996875000000000781249999999755859375")]
39+
public void IsItsValue(string expression, string reference)
40+
=> AssertClose(reference, Assert.IsType<Real>(expression.EvalNumerical()).EDecimal);
41+
42+
[Theory]
43+
[InlineData("sqrt(1/10^20)", "1/10000000000")]
44+
[InlineData("(-1/10^30)^(1/3)", "-1/10000000000")]
45+
[InlineData("(16/10^40)^(1/4)", "1/5000000000")]
46+
[InlineData("sqrt(10^30)", "1000000000000000")]
47+
[InlineData("(-8)^(1/3)", "-2")]
48+
[InlineData("(27/8)^(2/3)", "9/4")]
49+
public void AnExactRootIsExact(string expression, string root)
50+
=> Assert.Equal(root.ToEntity(), expression.EvalNumerical());
51+
52+
[Fact]
53+
public void TheRootOfASmallImaginaryIsComplex()
54+
{
55+
var root = Assert.IsType<Complex>("sqrt(i/10^20)".EvalNumerical());
56+
AssertClose("7.07106781186547524400844362104849039284835937688474036588340E-11", root.RealPart.EDecimal);
57+
AssertClose("7.07106781186547524400844362104849039284835937688474036588340E-11", root.ImaginaryPart.EDecimal);
58+
}
59+
60+
[Fact]
61+
public void ARootOffTheRealLineKeepsItsImaginaryPart()
62+
{
63+
// It was 1: the candidate rounded to six digits lost 5e-9 i. To twelve digits, because the modulus,
64+
// 1 + 5e-17, is read as 1 within 1e-16 (https://github.com/asc-community/AngouriMath/issues/1944).
65+
var root = Assert.IsType<Complex>("(1 + i/10^8)^(1/2)".EvalNumerical());
66+
AssertClose("4.9999999999999999375000000000000027343749999999998388671875E-9", root.ImaginaryPart.EDecimal, "1E-12");
67+
}
68+
}
69+
}

0 commit comments

Comments
 (0)