Skip to content

Commit 53ce725

Browse files
Build a whole power by a loop over the exponent's bits (#2363)
The whole power was a recursion on the half of the exponent, one frame for each of its bits, and a power of -1, i or -i is never too large to build. So (-1)^(2^20000) took 20000 frames and overflowed the stack of a thread-pool thread, 1.5 MB on Linux -- which ends the process rather than throwing. limitcheck died that way at SEED=28, evaluating a sample point. A loop from the exponent's top bit down makes the same multiplications in the same order: the recursion's half truncates toward zero, so a negative exponent is the bits of its magnitude, each multiplying by 1 / num. Exact and decimal powers, real and complex, come out digit for digit as before. limitcheck at DEPTH=3, SEED=28 now finishes its 600 expressions. No issue was filed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 43d9b81 commit 53ce725

2 files changed

Lines changed: 67 additions & 8 deletions

File tree

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

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -557,18 +557,27 @@ public static Complex Pow(Complex @base, Complex power)
557557
// cube was three multiplications and a tenth power was nine, each with the
558558
// rational search on its result, and the recursion was linear in the exponent
559559
// where it should be logarithmic.
560+
//
561+
// A loop over the exponent's bits, from the top, rather than a recursion on its
562+
// half: the recursion was one frame a bit, and a power of -1, i or -i is never
563+
// too large to build, so (-1)^(2^20000) took 20000 frames and overflowed the
564+
// stack of a thread-pool thread, which ends the process. The multiplications are
565+
// the recursion's, in its order: the half truncates toward zero, so a negative
566+
// exponent is the bits of its magnitude, each one multiplying by 1 / num.
560567
static Complex BinaryIntPow(Complex num, EInteger val)
561568
{
562569
if (val.IsZero)
563570
return 1;
564-
if (val.Equals(1))
565-
return num;
566-
if (val.Equals(-1))
567-
return 1 / num;
568-
var divRem = val.DivRem(2); // divRem[0] == val / 2, divRem[1] == val % 2
569-
var half = BinaryIntPow(num, divRem[0]);
570-
var squared = half * half;
571-
return divRem[1].IsZero ? squared : squared * BinaryIntPow(num, divRem[1]);
571+
var magnitude = val.Abs();
572+
var factor = val.Sign < 0 ? 1 / num : num;
573+
var result = factor;
574+
for (var bit = magnitude.GetUnsignedBitLengthAsInt64() - 2; bit >= 0; bit--)
575+
{
576+
result *= result;
577+
if (magnitude.GetSignedBit(bit))
578+
result *= factor;
579+
}
580+
return result;
572581
}
573582
// A complex whose imaginary part is exactly zero is the real number it is, and
574583
// takes the real paths below: with the downcasting off, Complex.Create keeps
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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 System.Threading;
9+
using Xunit;
10+
11+
namespace AngouriMath.Tests.Core
12+
{
13+
/// <summary>
14+
/// A whole power was built by a recursion on the half of the exponent, one frame for each of its bits. A power of
15+
/// -1, i or -i is never too large to build, so <c>(-1)^(2^20000)</c> took 20000 frames and overflowed the stack of a
16+
/// thread-pool thread, 1.5 MB on Linux, which ends the process rather than throwing. Found by limitcheck at
17+
/// <c>DEPTH=3</c>, <c>SEED=28</c>, evaluating a sample point; no issue was filed.
18+
/// </summary>
19+
[Trait("Area", "Core")]
20+
public sealed class AWholePowerOfAUnitTest
21+
{
22+
// A quarter of a megabyte: smaller than any thread the library is run on, and enough that the recursion
23+
// overflowed on these exponents, of 20000 bits and more, and ended the test host.
24+
private const int SmallStack = 256 * 1024;
25+
26+
[Theory]
27+
[InlineData("(-1)^(2^20000)", "1")]
28+
[InlineData("(-1)^(2^20000 + 1)", "-1")]
29+
[InlineData("i^(3^20000)", "i")]
30+
[InlineData("(-i)^(-(2^20001) - 3)", "-i")]
31+
[InlineData("(-1)^(-(2^20000) - 1)", "-1")]
32+
public void IsItsCycleOnASmallStack(string input, string expected)
33+
{
34+
Entity? value = null;
35+
var thread = new Thread(() => value = MathS.FromString(input).EvalNumerical(), SmallStack);
36+
thread.Start();
37+
thread.Join();
38+
Assert.Equal(MathS.FromString(expected).EvalNumerical(), value);
39+
}
40+
41+
// The multiplications are the recursion's, in its order, so a decimal power keeps every digit it had.
42+
[Theory]
43+
[InlineData("(1.1 + 0.2i)^(-13)", "-0.16276672053742730437722112 - 0.16876781705731410143215616i")]
44+
[InlineData("(0.3 + 0.4i)^(-9)", "-241.716953088 - 451.350102016i")]
45+
[InlineData("2.5^(-37)", "137438953472/72759576141834259033203125")]
46+
[InlineData("i^(-7)", "i")]
47+
public void IsWhatItWas(string input, string expected)
48+
=> Assert.Equal(MathS.FromString(expected).EvalNumerical(), MathS.FromString(input).EvalNumerical());
49+
}
50+
}

0 commit comments

Comments
 (0)