Skip to content

Commit

Permalink
Do not panic if the interval bounds are not finite
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris00 committed Dec 23, 2023
1 parent bf5831c commit 667a738
Showing 1 changed file with 26 additions and 10 deletions.
36 changes: 26 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,14 +257,6 @@ macro_rules! new_root_finding_method {
/// Private constructor called by $fun with more constraints.
#[must_use]
fn new(f: F, a: $(&$l)? T, b: $(&$l)? T) -> Self {
if !a.is_finite() {
panic!("root1d::{}: a = {:?} must be finite",
stringify!($fun), a)
}
if !b.is_finite() {
panic!("root1d::{}: b = {:?} must be finite",
stringify!($fun), b)
}
$struct { f, a, b,
t: T::DefaultTerminate::default(),
maxiter: 100,
Expand All @@ -274,6 +266,27 @@ macro_rules! new_root_finding_method {
}
}

impl <$($l,)? T, F, Term> $struct<$($l,)? T, F, Term>
where T: Bisectable,
Term: Terminate<T>
{
/// Check that `a` and `b` are finite.
#[inline]
fn check_interval_bounds(&self) -> Result<(), Error<T>> {
if !self.a.is_finite() {
return Err(Error::NotFinite {
x: self.a.clone(), fx: self.a.clone(),
})
}
if !self.b.is_finite() {
return Err(Error::NotFinite {
x: self.b.clone(), fx: self.b.clone(),
})
}
Ok(())
}
}

impl<$($l,)? T, F, Term> $struct<$($l,)? T, F, Term>
where Term: Terminate<T> {
/// Set the maximum number of iterations.
Expand Down Expand Up @@ -472,6 +485,7 @@ where T: Bisectable + Copy,
/// interval still holds but it is not guaranteed that `f`
/// possesses a root in it.
pub fn root_mut(&mut self, root: &mut T) -> Result<(T,T), Error<T>> {
Self::check_interval_bounds(self)?;
let mut a;
let mut b;
if self.a <= self.b {
Expand Down Expand Up @@ -644,6 +658,7 @@ where T: Bisectable,
// If some workspace if given, use it even if internal storage
// is available because it may have different, say, precision
// characteristics.
Self::check_interval_bounds(self)?;
let (a, b, fa, fb, fx) = match &mut self.workspace {
None => {
if self.owned_workspace.is_none() {
Expand Down Expand Up @@ -894,6 +909,7 @@ where T: OrdField,
/// to achieve a desired precision. This is particularly
/// interesting if the function is costly to evaluate.
pub fn root_mut(&mut self, root: &mut T) -> Result<(T,T), Error<T>> {
Self::check_interval_bounds(self)?;
let mut a;
let mut b;
if self.a <= self.b {
Expand Down Expand Up @@ -1255,9 +1271,9 @@ where T: OrdFieldMut + 'a,
/// assert!(a <= &x && &x <= b);
/// # } Ok(()) }
/// ```
pub fn root_mut(&mut self, root: &mut T)
-> Result<(&T, &T), Error<T>>
pub fn root_mut(&mut self, root: &mut T) -> Result<(&T, &T), Error<T>>
{
Self::check_interval_bounds(self)?;
let Toms748MutWorkspace {
a, b, c, d, e,
fa, fb, fc, fd, fe,
Expand Down

0 comments on commit 667a738

Please sign in to comment.