Skip to content

Commit 87a0eff

Browse files
A negation of a signed term is printed in brackets (#2265)
A negation prints as a bare - in front of what it negates. A negative integer was bracketed by its priority, which is a sum's, but a negative rational has a quotient's, and a negation and +oo print with their own sign: -(-2/3) printed as --2/3, which reads back as -(-2) / 3, and sinh(+oo) as (e ^ +oo - e ^ (-+oo)) / 2. A negated term whose print starts with a sign is now bracketed. NegationOfASignedTermPrintsTest prints six negations and reads them back to their values; three of them, and sinh(-5/4)'s double sign, fail on master. Found by work/roundtrip: over 5000 random expressions no print failed to parse and none read back to another value. Fixes #2264. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent 5110f4c commit 87a0eff

4 files changed

Lines changed: 76 additions & 4 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,18 @@ and `Simplify` passed that on as `not x = 0`, though at `x = 0` the input is `-y
194194
| `"sgn(NaN)".ToEntity().Evaled` | `1` | `NaN` |
195195
| `"sgn(-1 provided false)".ToEntity().Evaled` | `1` | `NaN` |
196196

197+
### A negation of a signed term is printed in brackets
198+
199+
**Silent: `-(-2/3)` prints as `-(-2/3)`, where it printed as `--2/3`.** A negation prints as a bare `-` in front of what
200+
it negates, and a negative integer was bracketed by its priority, but a negative rational, a negation, and `+oo` were
201+
not. `--5/4` reads back as `-(-5) / 4`, which has the same value as a different expression, and is easy to misread
202+
([#2264](https://github.com/asc-community/AngouriMath/issues/2264)).
203+
204+
| Input | Was (2.6.0) | Now |
205+
|---|---|---|
206+
| `"sinh(-5/4)".ToEntity().ToString()` | `(e ^ (-5/4) - e ^ (--5/4)) / 2` | `(e ^ (-5/4) - e ^ (-(-5/4))) / 2` |
207+
| `new Entity.Mulf(-1, new Entity.Mulf(-1, x)).ToString()` | `--x` | `-(-x)` |
208+
197209
### A matrix is divided, raised to a power and compared as a matrix
198210

199211
**Silent: wrong answers about matrices become right, or stay written.** Where an operation had no answer of its own

‎Sources/.editorconfig‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,9 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed
252252
[Tests/UnitTests/Convenience/RealAndImaginaryPartTest.cs]
253253
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
254254

255+
[Tests/UnitTests/Convenience/NegationOfASignedTermPrintsTest.cs]
256+
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
257+
255258
[Tests/UnitTests/Convenience/DoubleFactorialTest.cs]
256259
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
257260

‎Sources/AngouriMath/Functions/Output/ToString/ToString.Arithmetics.Classes.cs‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,16 @@ public partial record Mulf
3434
// unbracketed -- but `mod` shares this precedence level and is not associative, so a
3535
// `mod` on the right must be bracketed or it is re-read as the outer operator:
3636
// `2 * (3 mod 2)` is 2 and `(2 * 3) mod 2` is 0.
37-
private protected override string StringizeNode() =>
38-
(Multiplier is Integer(-1) && !MathS.Diagnostic.OutputExplicit ? "-"
39-
: Multiplier.Stringize(Multiplier.Priority < Priority) + " * ")
40-
+ Multiplicand.Stringize(Multiplicand.Priority < Priority || Multiplicand is Modf);
37+
private protected override string StringizeNode()
38+
{
39+
var multiplicand = Multiplicand.Stringize(Multiplicand.Priority < Priority || Multiplicand is Modf);
40+
if (Multiplier is not Integer(-1) || MathS.Diagnostic.OutputExplicit)
41+
return Multiplier.Stringize(Multiplier.Priority < Priority) + " * " + multiplicand;
42+
// A negation of something printed with a sign of its own is bracketed: -(-2/3), not --2/3, which is
43+
// read back as -(-2) / 3; and -(+oo), not -+oo. A negative rational is not bracketed by its priority,
44+
// which is a quotient's, as a negative integer is. https://github.com/asc-community/AngouriMath/issues/2264
45+
return multiplicand.StartsWith("-") || multiplicand.StartsWith("+") ? "-(" + multiplicand + ")" : "-" + multiplicand;
46+
}
4147
/// <inheritdoc/>
4248
public override string ToString() => Stringize();
4349
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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;
9+
using AngouriMath.Extensions;
10+
using Xunit;
11+
using static AngouriMath.Entity;
12+
using static AngouriMath.Entity.Number;
13+
14+
namespace AngouriMath.Tests.Convenience
15+
{
16+
/// <summary>
17+
/// A negation of something printed with a sign of its own is bracketed, so it reads back as itself: <c>-(-2/3)</c>
18+
/// printed as <c>--2/3</c>, read back as <c>-(-2) / 3</c>, and <c>-(+oo)</c> as <c>-+oo</c>.
19+
/// <a href="https://github.com/asc-community/AngouriMath/issues/2264">#2264</a>
20+
/// </summary>
21+
[Trait("Area", "Convenience")]
22+
public sealed class NegationOfASignedTermPrintsTest
23+
{
24+
private static Entity Negated(Entity e) => new Mulf(Integer.Create(-1), e);
25+
26+
public static TheoryData<Entity, string> Negations => new()
27+
{
28+
{ Negated(Rational.Create(-2, 3)), "-(-2/3)" },
29+
{ Negated(Real.PositiveInfinity), "-(+oo)" },
30+
{ Negated(Negated(MathS.Var("x"))), "-(-x)" },
31+
{ Negated(Integer.Create(-3)), "-(-3)" },
32+
{ Negated(MathS.Var("x")), "-x" },
33+
{ Negated(Rational.Create(2, 3)), "-2/3" },
34+
};
35+
36+
[Theory]
37+
[MemberData(nameof(Negations))]
38+
public void ItPrintsWithOneSignAndReadsBackToItsValue(Entity negation, string printed)
39+
{
40+
Assert.Equal(printed, negation.Stringize());
41+
// The parser folds a negated number into the number, so -(-2/3) reads back as 2/3: the same value, and for
42+
// a symbol the same tree.
43+
var x = "7/10".ToEntity();
44+
Assert.Equal(negation.Substitute("x", x).Evaled, negation.Stringize().ToEntity().Substitute("x", x).Evaled);
45+
}
46+
47+
[Fact]
48+
public void AHyperbolicSineOfANegativeRationalHasNoDoubleSign()
49+
=> Assert.DoesNotContain("--", "sinh(-5/4)".ToEntity().Stringize());
50+
}
51+
}

0 commit comments

Comments
 (0)