Skip to content

Commit 006167a

Browse files
committed
Exceptions for infinte bounds.
1 parent f82490a commit 006167a

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

src/Numerics.Tests/RootFindingTests/RobustNewtonRaphsonTest.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,6 @@ public void InfinitelyManyRootsWithGivenAccuracy()
136136
[Test]
137137
public void BoundsNearMaxValueNoOverflow()
138138
{
139-
// degenerate case with infinitely many roots
140139
Func<double, double> f1 = x => 0.0;
141140
Func<double, double> df1 = x => 0.0;
142141
Assert.AreEqual(double.MaxValue, RobustNewtonRaphson.FindRoot(f1, df1, double.MaxValue - 2, double.MaxValue - 1), 1e-6);
@@ -145,12 +144,29 @@ public void BoundsNearMaxValueNoOverflow()
145144
[Test]
146145
public void BoundsNearMinValueNoUnderflow()
147146
{
148-
// degenerate case with infinitely many roots
149147
Func<double, double> f1 = x => 0.0;
150148
Func<double, double> df1 = x => 0.0;
151149
Assert.AreEqual(double.MinValue, RobustNewtonRaphson.FindRoot(f1, df1, double.MinValue + 1, double.MinValue + 2), 1e-6);
152150
}
153151

152+
[Test]
153+
public void InfiniteLowerBound()
154+
{
155+
Func<double, double> f1 = x => 0.0;
156+
Func<double, double> df1 = x => 0.0;
157+
Assert.That(() => RobustNewtonRaphson.FindRoot(f1, df1, double.NegativeInfinity, 5), Throws.TypeOf<ArgumentOutOfRangeException>());
158+
Assert.That(() => RobustNewtonRaphson.FindRoot(f1, df1, double.PositiveInfinity, 5), Throws.TypeOf<ArgumentOutOfRangeException>());
159+
}
160+
161+
[Test]
162+
public void InfiniteUpperBound()
163+
{
164+
Func<double, double> f1 = x => 0.0;
165+
Func<double, double> df1 = x => 0.0;
166+
Assert.That(() => RobustNewtonRaphson.FindRoot(f1, df1, -5, double.NegativeInfinity), Throws.TypeOf<ArgumentOutOfRangeException>());
167+
Assert.That(() => RobustNewtonRaphson.FindRoot(f1, df1, -5, double.PositiveInfinity), Throws.TypeOf<ArgumentOutOfRangeException>());
168+
}
169+
154170
[Test]
155171
public void NoRoot()
156172
{

src/Numerics/RootFinding/RobustNewtonRaphson.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ public static double FindRoot(Func<double, double> f, Func<double, double> df, d
6060
/// <summary>Find a solution of the equation f(x)=0.</summary>
6161
/// <param name="f">The function to find roots from.</param>
6262
/// <param name="df">The first derivative of the function to find roots from.</param>
63-
/// <param name="lowerBound">The low value of the range where the root is supposed to be.</param>
64-
/// <param name="upperBound">The high value of the range where the root is supposed to be.</param>
63+
/// <param name="lowerBound">The low value of the range where the root is supposed to be (finite number).</param>
64+
/// <param name="upperBound">The high value of the range where the root is supposed to be (finite number).</param>
6565
/// <param name="accuracy">Desired accuracy. The root will be refined until the accuracy or the maximum number of iterations is reached. Example: 1e-14. Must be greater than 0.</param>
6666
/// <param name="maxIterations">Maximum number of iterations. Example: 100.</param>
6767
/// <param name="subdivision">How many parts an interval should be split into for zero crossing scanning in case of lacking bracketing. Example: 20.</param>
@@ -74,6 +74,16 @@ public static bool TryFindRoot(Func<double, double> f, Func<double, double> df,
7474
throw new ArgumentOutOfRangeException(nameof(accuracy), "Must be greater than zero.");
7575
}
7676

77+
if (double.IsInfinity(lowerBound))
78+
{
79+
throw new ArgumentOutOfRangeException(nameof(lowerBound), "Must be a finite number.");
80+
}
81+
82+
if (double.IsInfinity(upperBound))
83+
{
84+
throw new ArgumentOutOfRangeException(nameof(upperBound), "Must be a finite number.");
85+
}
86+
7787
root = lowerBound + 0.5 * (upperBound - lowerBound);
7888
double fx = f(root);
7989
if (Math.Abs(fx) < accuracy)

0 commit comments

Comments
 (0)