Skip to content

Commit fef0b75

Browse files
round is exact, and an infinity is its own nearest integer (#2285)
round(10^120 + 2/3) was 10^120, where it is 10^120 + 1, and round(+oo) threw OverflowException. Roundf took a rational through its decimal, which at the precision had already rounded 10^120 + 2/3 onto 10^120, and handed an infinity to EInteger, which refuses one. floor and ceil had both defects and were fixed in #830 and #1807; round now does as they do: an infinity and NaN pass through, and a rational is rounded from its numerator and denominator, to the even integer at a tie. RoundIsExactTest takes thirteen roundings; six fail on master, the four long rationals that do not round onto the integer beside them and both infinities. The suite, the trio, F#, AOT and the eight harnesses pass. Fixes #2282. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent 34a4fe5 commit fef0b75

4 files changed

Lines changed: 79 additions & 1 deletion

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,19 @@ its rows in the order it finds them, which can differ from the eliminator's
386386
| `MathS.Equations("x*y", "x + y^2 - 1").Solve("x", "y")` | `[[1, 0]]` | `[[1, 0], [0, 1], [0, -1]]` |
387387
| `MathS.Equations("x*y + 3*x - 5*y - 15", "x^2 - 3*x - 4*y - 10").Solve("x", "y")` | `[[1, -3], [2, -3]]` | `[[5, 0], [1, -3], [2, -3]]` |
388388

389+
### `round` is exact, and an infinity is its own nearest integer
390+
391+
**Silent for a long rational, not silent for an infinity.** `round` of a rational with more digits than the precision was
392+
taken through its decimal, which had already rounded it onto the integer beside it, and `round` of an infinity threw
393+
`OverflowException`. `floor` and `ceil` were fixed for both before; `round` now does as they do
394+
([#2282](https://github.com/asc-community/AngouriMath/issues/2282)).
395+
396+
| Input | Was (2.6.0) | Now |
397+
|---|---|---|
398+
| `"round(10^120 + 2/3)".ToEntity().Evaled` | `10^120` | `10^120 + 1` |
399+
| `"round(10^120 + 3/2)".ToEntity().Evaled` | `10^120` | `10^120 + 2` |
400+
| `"round(+oo)".ToEntity().Evaled` | throws `OverflowException` | `+oo` |
401+
389402
### A squared sine and cosine with one coefficient are found anywhere in a sum
390403

391404
**Silent: an identity reaches its value however the sum is written.** `k sin(x)^2 + k cos(x)^2` was reduced to `k`

‎Sources/.editorconfig‎

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

60+
[Tests/UnitTests/Convenience/RoundIsExactTest.cs]
61+
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
62+
6063
[Tests/UnitTests/Core/FactorialCacheIsThreadSafeTest.cs]
6164
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
6265

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1297,6 +1297,19 @@ private static PeterO.Numbers.EInteger RoundedQuotient(PeterO.Numbers.ERational
12971297
: (value.Numerator.Sign < 0 ? quotient - PeterO.Numbers.EInteger.One : quotient);
12981298
}
12991299

1300+
/// <summary>
1301+
/// The integer nearest to a rational, exactly, and the even one of the two at a tie: its floor, or
1302+
/// the integer above where the rational is more than a half above it, or a half above it and the
1303+
/// floor is odd.
1304+
/// </summary>
1305+
private static PeterO.Numbers.EInteger RoundedHalfEven(PeterO.Numbers.ERational value)
1306+
{
1307+
var floor = RoundedQuotient(value, up: false);
1308+
var aboveTheFloor = value.Subtract(PeterO.Numbers.ERational.FromEInteger(floor))
1309+
.CompareTo(PeterO.Numbers.ERational.Create(1, 2));
1310+
return aboveTheFloor > 0 || aboveTheFloor == 0 && !floor.IsEven ? floor + PeterO.Numbers.EInteger.One : floor;
1311+
}
1312+
13001313
public partial record Ceilf
13011314
{
13021315
// Defined everywhere in the complex plane, taken componentwise.
@@ -1331,7 +1344,13 @@ protected override Entity InnerSimplify(bool isExact)
13311344
a => a switch
13321345
{
13331346
Integer n => n,
1334-
Rational n => Integer.Create(n.EDecimal.RoundHalfEven().ToEInteger()),
1347+
// As in Floorf: an infinity is its own nearest integer and NaN propagates,
1348+
// where EInteger refused both and round(+oo) threw, and a rational is
1349+
// rounded from its numerator and denominator, where through EDecimal
1350+
// round(10^120 + 2/3) came out 10^120.
1351+
// https://github.com/asc-community/AngouriMath/issues/2282
1352+
Real { IsFinite: false } n => n,
1353+
Rational n => Integer.Create(RoundedHalfEven(n.ERational)),
13351354
Real n when !isExact => Integer.Create(n.EDecimal.RoundHalfEven().ToEInteger()),
13361355
Complex n when !isExact => Complex.Create(
13371356
n.RealPart.EDecimal.RoundHalfEven(), n.ImaginaryPart.EDecimal.RoundHalfEven()),
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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 Xunit;
10+
11+
namespace AngouriMath.Tests.Convenience
12+
{
13+
/// <summary>
14+
/// round of a rational is the nearest integer, exactly, and the even one at a tie, and an infinity is its own: through
15+
/// EDecimal a rational of more digits than the precision was rounded onto the integer beside it first, and
16+
/// round(10^120 + 2/3) came out 10^120, while round(+oo) threw OverflowException. floor and ceil had both defects and
17+
/// were fixed in #830 and #1807.
18+
/// <a href="https://github.com/asc-community/AngouriMath/issues/2282">#2282</a>
19+
/// </summary>
20+
[Trait("Area", "Convenience")]
21+
public sealed class RoundIsExactTest
22+
{
23+
[Theory]
24+
[InlineData("round(10^120 + 2/3)", "10^120 + 1")]
25+
[InlineData("round(10^120 - 2/3)", "10^120 - 1")]
26+
[InlineData("round(10^120 + 3/2)", "10^120 + 2")]
27+
[InlineData("round(10^120 + 1/2)", "10^120")]
28+
[InlineData("round(-10^120 - 2/3)", "-10^120 - 1")]
29+
[InlineData("round(-10^400 + 1/3)", "-10^400")]
30+
[InlineData("round(2/3)", "1")]
31+
[InlineData("round(5/2)", "2")]
32+
[InlineData("round(7/2)", "4")]
33+
[InlineData("round(-5/2)", "-2")]
34+
[InlineData("round(-7/2)", "-4")]
35+
[InlineData("round(+oo)", "+oo")]
36+
[InlineData("round(-oo)", "-oo")]
37+
public void ItIsTheNearestInteger(string input, string expected)
38+
{
39+
Assert.Equal(expected.ToEntity().InnerSimplified, input.ToEntity().InnerSimplified);
40+
Assert.Equal(expected.ToEntity().InnerSimplified, input.ToEntity().Evaled);
41+
}
42+
}
43+
}

0 commit comments

Comments
 (0)