Skip to content

Commit aab5e7f

Browse files
A logarithm to base 0 has no value (#2258)
log(0, 2) evaluated to 0, and log(x, 2) compiled to (-0, -0) at 0. Both took it as ln(x) / ln(0), which is ln(x) / (-oo). But 0 is not a base, since no power of it is anything but 0, and Logf's condition already excluded it: not x = 0 and not x = 1. Number.Log, ComplexBranches.Log and the LINQ compiler's double and float Log now answer an exact zero base with NaN. Over double, Math.Log already gave NaN, except for an antilogarithm of 1, where it gave -0. A base that is small but not 0 keeps its logarithm, so the test is for an exact zero, not IsZero's tolerance. log(b, 1) simplified to 0 provided not b = 1, which is 0 at b = 0. With log(0, 1) now NaN, BoundCheck found that disagreement, and the fold now provides not b = 0 as well, as log's condition does. LogarithmToBaseZeroTest takes six logarithms to base 0, evaluated and simplified, and four compiled by FastExpression, the LINQ compiler over Complex and over double. Seven fail on master. Three logarithms to other bases pin what has not changed, 10^(-100) among them. TheLogarithmOfOneExcludesTheBase0 takes the simplification at 0, 1 and 2. Found by work/domaincheck. Fixes #2255. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent ca785d3 commit aab5e7f

8 files changed

Lines changed: 114 additions & 9 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,20 @@ reals, to `1` and `0` ([#2195](https://github.com/asc-community/AngouriMath/issu
644644
| `"arg(x)".ToEntity().Compile("x").Call(-2)` | throws `UncompilableNodeException`, `arg` being a variable | `pi` |
645645
| `"conj(x)".ToEntity().Compile<Complex, Complex>("x")(3 + 4i)` | throws `UncompilableNodeException`, `conj` being a variable | `3 - 4i` |
646646

647+
### A logarithm to base 0 has no value
648+
649+
**Silent: `log(0, x)` is `NaN`, where it was `0` for a positive `x`.** Evaluation and both compilers took it as
650+
`ln(x) / ln(0)`, which is `ln(x) / (-oo)`, and came out with 0. But 0 is not a base, since no power of it is anything
651+
but 0, and `log`'s own condition already excluded it. A base that is small but not 0 keeps its logarithm.
652+
`log(b, 1)`, simplified to 0 for every base but 1, now excludes the base 0 as well
653+
([#2255](https://github.com/asc-community/AngouriMath/issues/2255)).
654+
655+
| Input | Was (2.6.0) | Now |
656+
|---|---|---|
657+
| `"log(0, 2)".ToEntity().Evaled` | `0` | `NaN` |
658+
| `"log(x, 2)".ToEntity().Compile("x").Call(0)` | `(-0, -0)` | `(NaN, 0)` |
659+
| `"log(x, 1)".ToEntity().Simplify()` | `0 provided not x = 1` | `0 provided not x = 0 and not x = 1` |
660+
647661
### `re` and `im` are the real and the imaginary part
648662

649663
**Not silent: `re(…)` and `im(…)`, and `real(…)` and `imag(…)`, are now the real and the imaginary part, where they were

‎Sources/.editorconfig‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -932,6 +932,9 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed
932932
[Tests/UnitTests/Common/FiniteSetStatementCompilationTest.cs]
933933
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
934934

935+
[Tests/UnitTests/Convenience/LogarithmToBaseZeroTest.cs]
936+
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
937+
935938
[Tests/UnitTests/Convenience/EulerGammaTest.cs]
936939
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
937940

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,11 @@ static Complex BinaryIntPow(Complex num, EInteger val)
695695
/// <param name="x">The number of which we want to get its base power</param>
696696
public static Complex Log(Complex @base, Complex x)
697697
{
698+
// 0 is not a base: no power of it is anything but 0, and log_0(x) has no value, which is what
699+
// Logf's condition says. ln(x)/ln(0) is ln(x)/(-oo), and was 0. An exact zero only: a base that
700+
// is small and not 0 has its logarithm. https://github.com/asc-community/AngouriMath/issues/2255
701+
if (@base.RealPart.EDecimal.IsZero && @base.ImaginaryPart.EDecimal.IsZero)
702+
return Real.NaN;
698703
if (LostToExponentRange(x) || LostToExponentRange(@base))
699704
return Ln(x) / Ln(@base);
700705
// A base of 1 is excluded from the real shortcut so that it falls through to the

‎Sources/AngouriMath/Functions/Compilation/ComplexBranches.cs‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,11 @@ public static NumericsComplex Arcsin(NumericsComplex a)
5959
/// https://github.com/asc-community/AngouriMath/issues/2239
6060
/// </summary>
6161
public static NumericsComplex Log(NumericsComplex @base, NumericsComplex value)
62-
=> RealProducts.Quotient(NumericsComplex.Log(Unsigned(value)), NumericsComplex.Log(Unsigned(@base)));
62+
=> @base == NumericsComplex.Zero
63+
// A logarithm to base 0 has no value, as in evaluation, where ln(value) / ln(0) made it 0.
64+
// https://github.com/asc-community/AngouriMath/issues/2255
65+
? new NumericsComplex(double.NaN, 0)
66+
: RealProducts.Quotient(NumericsComplex.Log(Unsigned(value)), NumericsComplex.Log(Unsigned(@base)));
6367

6468
public static NumericsComplex Arccos(NumericsComplex a) => NumericsComplex.Acos(Unsigned(a));
6569

‎Sources/AngouriMath/Functions/Compilation/IntoLinq/MathAllMethods.cs‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@ internal static class MathAllMethods
2424

2525
public static System.Numerics.Complex Log(System.Numerics.Complex a, System.Numerics.Complex b)
2626
=> AngouriMath.Core.Compilation.ComplexBranches.Log(a, b);
27+
// A logarithm to base 0 has no value. https://github.com/asc-community/AngouriMath/issues/2255
2728
public static double Log(double a, double b)
28-
=> Math.Log(b, a);
29+
=> a == 0 ? double.NaN : Math.Log(b, a);
2930
public static float Log(float a, float b)
30-
=> (float)Math.Log(b, a);
31+
=> a == 0 ? float.NaN : (float)Math.Log(b, a);
3132
public static long Log(long a, long b)
3233
=> (long)Math.Log(b, a);
3334
public static int Log(int a, int b)

‎Sources/AngouriMath/Functions/Compilation/IntoLinq/MathAllMethods.tt‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,11 @@ namespace AngouriMath.Core.Compilation.IntoLinq
4949

5050
public static System.Numerics.Complex Log(System.Numerics.Complex a, System.Numerics.Complex b)
5151
=> AngouriMath.Core.Compilation.ComplexBranches.Log(a, b);
52+
// A logarithm to base 0 has no value. https://github.com/asc-community/AngouriMath/issues/2255
5253
public static double Log(double a, double b)
53-
=> Math.Log(b, a);
54+
=> a == 0 ? double.NaN : Math.Log(b, a);
5455
public static float Log(float a, float b)
55-
=> (float)Math.Log(b, a);
56+
=> a == 0 ? float.NaN : (float)Math.Log(b, a);
5657
public static long Log(long a, long b)
5758
=> (long)Math.Log(b, a);
5859
public static int Log(int a, int b)

‎Sources/AngouriMath/Functions/Evaluation/Evaluation.Continuous/Evaluation.Continuous.Arithmetics.Classes.cs‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -463,11 +463,13 @@ when a.Evaled is Real below && below > 0 && below < 1
463463
=> Real.PositiveInfinity.Provided(condition),
464464

465465
// log_b(1) is 0/ln(b), which is 0 for every base but 1, where it is
466-
// 0/0 -- and every division by zero here is NaN. The condition belongs
467-
// on the answer rather than gating the rule, because at b = 1 the
468-
// expression is genuinely undefined and not merely something else.
466+
// 0/0 -- and every division by zero here is NaN -- and 0, which is not a
467+
// base (https://github.com/asc-community/AngouriMath/issues/2255). The
468+
// condition belongs on the answer rather than gating the rule, because at
469+
// those bases the expression is genuinely undefined and not merely
470+
// something else.
469471
({ DomainCondition: var condition }, Integer(1))
470-
=> Integer.Zero.Provided(condition).Provided(!a.EqualTo(1)),
472+
=> Integer.Zero.Provided(condition).Provided(!a.EqualTo(0) & !a.EqualTo(1)),
471473

472474
_ => null
473475
},
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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.Numerics;
9+
using AngouriMath;
10+
using AngouriMath.Extensions;
11+
using Xunit;
12+
13+
namespace AngouriMath.Tests.Convenience
14+
{
15+
/// <summary>
16+
/// A logarithm to base 0 has no value, as <c>log</c>'s condition says, where <c>ln(x) / ln(0)</c>, which is
17+
/// <c>ln(x) / (-oo)</c>, made it 0 in evaluation and in the compilers.
18+
/// <a href="https://github.com/asc-community/AngouriMath/issues/2255">#2255</a>
19+
/// </summary>
20+
[Trait("Area", "Convenience")]
21+
public sealed class LogarithmToBaseZeroTest
22+
{
23+
[Theory]
24+
[InlineData("log(0, 2)")]
25+
[InlineData("log(0, 1/2)")]
26+
[InlineData("log(0, -3)")]
27+
[InlineData("log(0, i)")]
28+
[InlineData("log(0, 0)")]
29+
[InlineData("log(0, 1)")]
30+
public void ItHasNoValue(string expression)
31+
{
32+
Assert.True(expression.ToEntity().Evaled.IsNaN, $"{expression} evaluates to {expression.ToEntity().Evaled}");
33+
Assert.True(expression.ToEntity().Simplify().IsNaN, $"{expression} simplifies to {expression.ToEntity().Simplify()}");
34+
}
35+
36+
[Theory]
37+
[InlineData(2)]
38+
[InlineData(0.5)]
39+
[InlineData(-3)]
40+
[InlineData(1)]
41+
public void CompiledItHasNoValue(double antilogarithm)
42+
{
43+
var expression = $"log(x, {antilogarithm})".ToEntity();
44+
var fast = expression.Compile("x").Call(0);
45+
var linq = expression.Compile<Complex, Complex>("x")(0);
46+
Assert.True(double.IsNaN(fast.Real) || double.IsNaN(fast.Imaginary), $"{expression} at 0: {fast} by FastExpression");
47+
Assert.True(double.IsNaN(linq.Real) || double.IsNaN(linq.Imaginary), $"{expression} at 0: {linq} by LINQ");
48+
Assert.True(double.IsNaN(expression.Compile<double, double>("x")(0)), $"{expression} at 0 over double");
49+
}
50+
51+
/// <summary>
52+
/// <c>log(x, 1)</c> is 0 for every base but 0 and 1, and its simplification says so: it said only <c>not x = 1</c>,
53+
/// and was 0 at <c>x = 0</c>.
54+
/// </summary>
55+
[Fact]
56+
public void TheLogarithmOfOneExcludesTheBase0()
57+
{
58+
var simplified = "log(x, 1)".ToEntity().Simplify();
59+
Assert.True(simplified.Substitute("x", 0).Evaled.IsNaN, $"{simplified} at 0");
60+
Assert.True(simplified.Substitute("x", 1).Evaled.IsNaN, $"{simplified} at 1");
61+
Assert.Equal(Entity.Number.Integer.Zero, simplified.Substitute("x", 2).Evaled);
62+
}
63+
64+
/// <summary>A base that is small and not 0 has its logarithm.</summary>
65+
[Theory]
66+
[InlineData("log(2, 8)", 3)]
67+
[InlineData("log(1/2, 4)", -2)]
68+
[InlineData("log(10^(-100), 10^(-200))", 2)]
69+
public void AnotherBaseHasItsLogarithm(string expression, double value)
70+
{
71+
var evaluated = ((Entity.Number.Complex)expression.ToEntity().Evaled).ToNumerics();
72+
Assert.True(Complex.Abs(evaluated - value) < 1e-12, $"{expression} evaluates to {evaluated}");
73+
}
74+
}
75+
}

0 commit comments

Comments
 (0)