Skip to content

Commit 65e7b65

Browse files
Rafael-SOWNetclaude
andcommitted
A zero imaginary part is on the real axis, whatever its sign
A decimal's negative zero -- 0E-100 * -0.43 is -0E-102 -- is an artefact of the exponent arithmetic and not a limit from below, but the phase read -pi off its sign the way Math.Atan2 does. With the downcasting off Complex.Create keeps -1.54 - 0i a Complex, and a power went through the polar form a hair below the negative axis: (-1.54)^(-1/2) was +0.80 i with the setting off and -0.80 i with it on. The setting decided a branch, and the numerical checks in the harnesses, which turn the downcasting off, called a right antiderivative wrong on the strength of it (Rubi 7.4.2, e^acoth(a x) sqrt(c - c/(a x))/x^2). Now a number on the negative axis has argument pi for either zero (EDecimal.Sign is 0 for both), the square root reads its sign from a strictly negative imaginary part, and a complex base or power with an exactly zero imaginary part takes the real paths of Pow, where the half-power arithmetic is exact rather than a cosine of pi/2 at a hundred digits. Nothing changes with the downcasting on. #1378 Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura
1 parent c11661b commit 65e7b65

5 files changed

Lines changed: 119 additions & 10 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,25 @@ was ([#718](https://github.com/asc-community/AngouriMath/issues/718)).
575575
| `"x * sqrt(c - a*c*x) / e^(3 * atanh(a*x))".Integrate("x")` | `integral(…)` — left unevaluated, with `e` evaluated to a hundred digits inside it | an antiderivative in `sqrt(c - a c x)`, `provided c - a * c * x >= 0` |
576576
| `"1/sqrt(x + x^(3/2))".Integrate("x")` | `integral(1 / sqrt(x + x ^ (3/2)), x)` — left unevaluated | `2 * sqrt(1 + sqrt(x)) / (1/2) + C provided x >= 0` |
577577

578+
### A zero imaginary part is on the real axis, whatever its sign
579+
580+
**Silent, with the downcasting off.** A decimal's negative zero — `0E-100 * -0.43` is `-0E-102`
581+
— is an artefact of the exponent arithmetic and not a limit from below, but the phase read
582+
`-pi` off its sign the way `Math.Atan2` does, and with `DowncastingEnabled` off, where
583+
`Complex.Create` keeps `-1.54 - 0i` a `Complex`, a power went through the polar form on the
584+
wrong side of the negative axis: `(-1.54)^(-1/2)` was `+0.80 i` with the setting off and
585+
`-0.80 i` with it on. The setting decided a branch, and a numerical check that turns it off
586+
called a right antiderivative wrong. Now a number on the negative axis has argument `pi` for
587+
either zero, a complex with an exactly zero imaginary part takes the real paths of `Pow`, and
588+
the square root's sign is read from a strictly negative imaginary part
589+
([#1378](https://github.com/asc-community/AngouriMath/issues/1378)). Nothing changes with
590+
the downcasting on.
591+
592+
| Input, under `MathS.Settings.DowncastingEnabled.As(false, …)` | Was (2.5.0) | Now |
593+
|---|---|---|
594+
| `"(2*1.44*(1/sqrt(-5.286))^2-1)^(-1/2)".ToEntity().Evaled` | `5.5500952471953097…` | `-0.804560841963042…i`, as with the downcasting on |
595+
| `"((1/sqrt(-5.286))*(2*1.44*(1/sqrt(-5.286))^2-1)^(-1/2))^(-3)".ToEntity().Evaled` | `23.335336258472899…` | `-23.335336258472899…`, as with the downcasting on |
596+
578597
### `binomial(n, k)` is a function
579598

580599
**Addition, not silent.** The binomial coefficient is a node, `Entity.Binomialf`, spelled

‎Sources/.editorconfig‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,9 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed
291291
[AngouriMath/Functions/NumberTheory/ResidueClasses.cs]
292292
file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n
293293

294+
[Tests/UnitTests/Core/SignedZeroBranchTest.cs]
295+
file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n
296+
294297
[Tests/UnitTests/Core/ResidueClassTest.cs]
295298
file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n
296299

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,9 @@ public static Complex Sqrt(Complex num)
418418
else
419419
{
420420
y = Hypot(re, im, context).Subtract(re, context).Divide(2, context).SqrtByIntegerRoot(context);
421-
if (im.IsNegative) y = -y;
421+
// Strictly negative: a negative zero is on the axis, where the root is
422+
// +i times the root of the modulus, as for any negative real.
423+
if (im.Sign < 0) y = -y;
422424
x = im.Divide(y.Multiply(2, context), context);
423425
}
424426
return Complex.Create(x, y);
@@ -461,6 +463,18 @@ static Complex BinaryIntPow(Complex num, EInteger val)
461463
var squared = half * half;
462464
return divRem[1].IsZero ? squared : squared * BinaryIntPow(num, divRem[1]);
463465
}
466+
// A complex whose imaginary part is exactly zero is the real number it is, and
467+
// takes the real paths below: with the downcasting off, Complex.Create keeps
468+
// `-1.54 - 0i` a Complex, and through the polar form the sign of that zero
469+
// put the number a hair below the negative axis -- `(-1.54)^(-1/2)` came back
470+
// `+0.80 i` there and `-0.80 i` with the downcasting on. The phase reads a
471+
// zero as a zero now, and a real base gets the exact half-power arithmetic
472+
// rather than a cosine of pi/2 at a hundred digits.
473+
// https://github.com/asc-community/AngouriMath/issues/1378
474+
if (@base is not Real && @base.ImaginaryPart.EDecimal.IsZero)
475+
@base = Real.Create(@base.RealPart.EDecimal);
476+
if (power is not Real && power.ImaginaryPart.EDecimal.IsZero)
477+
power = Real.Create(power.RealPart.EDecimal);
464478
// e to a real power is the exponential of the power: raising the constant's
465479
// hundred digits loses to the power what it multiplies their error by --
466480
// e^700 came back to ninety-seven digits -- and the exponential is a series

‎Sources/AngouriMath/Functions/InternalAMExtensions.cs‎

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -686,6 +686,17 @@ public static EDecimal Acos(this EDecimal x, EContext context)
686686
/// Analogy of <see cref="Math.Atan2(double, double)"/> for more see this
687687
/// <img src="http://i.imgur.com/TRLjs8R.png"/>
688688
/// </summary>
689+
/// <remarks>
690+
/// <b>A zero is a zero, whatever its sign.</b> <c>Math.Atan2</c> reads a negative zero
691+
/// as a limit from below and answers <c>-pi</c> on the negative axis for it, and this
692+
/// did the same. A decimal's negative zero is not a limit: it is what
693+
/// <c>0E-100 * -0.43</c> multiplies to, an artefact of the exponent arithmetic, and
694+
/// reading <c>-pi</c> off it put <c>-1.54 - 0i</c> a hair below the negative axis, so
695+
/// that <c>(-1.54)^(-1/2)</c> came back <c>+0.80 i</c> with the downcasting off and
696+
/// <c>-0.80 i</c> with it on -- the setting deciding a branch. A number on the negative
697+
/// axis has argument <c>pi</c>. <c>EDecimal.Sign</c> is 0 for either zero, and is what
698+
/// is read here. https://github.com/asc-community/AngouriMath/issues/1378
699+
/// </remarks>
689700
public static EDecimal Arctan2(this EDecimal y, EDecimal x, EContext context)
690701
{
691702
if (y.IsNaN() || x.IsNaN()) return EDecimal.NaN;
@@ -702,22 +713,18 @@ public static EDecimal Arctan2(this EDecimal y, EDecimal x, EContext context)
702713
(_, inf) => consts.HalfPi,
703714
(_, -inf) => -consts.HalfPi,
704715
(-inf, -1) => -consts.Pi,
705-
(-inf, 0) => y.IsNegative ? -consts.Pi : consts.Pi,
716+
(-inf, 0) => consts.Pi,
706717
(-inf, 1) => consts.Pi,
707718
(inf, _) => EDecimal.Zero,
708719
(1, _) => Arctan(y.Divide(x, context), context),
709720
(-1, -1) => Arctan(y.Divide(x, context), context).Subtract(consts.Pi, context),
710-
(-1, 0) => y.IsNegative ? -consts.Pi : consts.Pi,
721+
(-1, 0) => consts.Pi,
711722
(-1, 1) => Arctan(y.Divide(x, context), context).Add(consts.Pi, context),
712723
(0, 1) => consts.HalfPi,
713724
(0, -1) => -consts.HalfPi,
714-
(0, 0) => (x.IsNegative, y.IsNegative) switch
715-
{
716-
(true, true) => -consts.Pi,
717-
(false, true) => EDecimal.NegativeZero,
718-
(true, false) => consts.Pi,
719-
(false, false) => EDecimal.Zero,
720-
},
725+
// The origin: pi for a negative zero on the x axis, as Math.Atan2 has it, and
726+
// zero otherwise -- the sign of y decides nothing, see above.
727+
(0, 0) => x.IsNegative ? consts.Pi : EDecimal.Zero,
721728
_ => throw new AngouriBugException("Unexpected scenario"),
722729
};
723730
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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;
9+
using AngouriMath.Extensions;
10+
using PeterO.Numbers;
11+
using Xunit;
12+
using static AngouriMath.Entity.Number;
13+
14+
namespace AngouriMath.Tests.Core
15+
{
16+
/// <summary>
17+
/// A decimal's negative zero is an artefact of the exponent arithmetic -- <c>0E-100 * -0.43</c>
18+
/// is <c>-0E-102</c> -- and not a limit from below, so a number on the negative axis has
19+
/// argument <c>pi</c> whatever the sign of its zero imaginary part, and its powers, roots
20+
/// and logarithm are the principal ones. With the downcasting off <c>Complex.Create</c>
21+
/// keeps <c>-1.54 - 0i</c> a Complex, and the phase read <c>-pi</c> off the zero's sign:
22+
/// <c>(-1.54)^(-1/2)</c> was <c>+0.80 i</c> there and <c>-0.80 i</c> with the downcasting on,
23+
/// the setting deciding a branch, and a numerical check that turns the downcasting off called
24+
/// a right antiderivative wrong on the strength of it.
25+
/// <see href="https://github.com/asc-community/AngouriMath/issues/1378"/>
26+
/// </summary>
27+
[Trait("Area", "Core")]
28+
public sealed class SignedZeroBranchTest
29+
{
30+
private static Complex Evaluated(string expression, bool downcasting)
31+
=> MathS.Settings.DowncastingEnabled.As(downcasting, () => (Complex)expression.ToEntity().Evaled);
32+
33+
private static void AssertClose(Complex expected, Complex actual, string what)
34+
{
35+
var re = Math.Abs((double)(expected.RealPart - actual.RealPart));
36+
var im = Math.Abs((double)(expected.ImaginaryPart - actual.ImaginaryPart));
37+
Assert.True(re < 1e-40 && im < 1e-40, $"{what}: expected {expected}, got {actual}");
38+
}
39+
40+
/// <summary>The same value with the downcasting on and off, and it is the principal one.</summary>
41+
[Theory]
42+
[InlineData("(2*1.44*(1/sqrt(-5.286))^2-1)^(-1/2)")]
43+
[InlineData("((1/sqrt(-5.286))*(2*1.44*(1/sqrt(-5.286))^2-1)^(-1/2))^(-3)")]
44+
[InlineData("sqrt((1/sqrt(-2))^2 * 3)")]
45+
[InlineData("ln((1/sqrt(-2))^2)")]
46+
public void TheSettingDoesNotDecideTheBranch(string expression)
47+
{
48+
var on = Evaluated(expression, downcasting: true);
49+
var off = Evaluated(expression, downcasting: false);
50+
AssertClose(on, off, expression);
51+
}
52+
53+
[Fact]
54+
public void ANegativeZeroImaginaryPartIsOnTheAxis()
55+
{
56+
MathS.Settings.DowncastingEnabled.As(false, () =>
57+
{
58+
var minusFour = Complex.Create(EDecimal.FromInt32(-4), EDecimal.NegativeZero);
59+
AssertClose(Complex.Create(0, 2), Complex.Sqrt(minusFour), "sqrt(-4 - 0i)");
60+
AssertClose(Complex.Create(0, 2), Complex.Pow(minusFour, Rational.Create(1, 2)), "(-4 - 0i)^(1/2)");
61+
AssertClose(Complex.Create(EDecimal.FromInt32(4).Log(MathS.Settings.DecimalPrecisionContext), MathS.DecimalConst.pi),
62+
Complex.Ln(minusFour), "ln(-4 - 0i)");
63+
});
64+
}
65+
}
66+
}

0 commit comments

Comments
 (0)