Skip to main content

numra_integrate/
error.rs

1//!
2//! Author: Moussa Leblouba
3//! Date: 9 February 2026
4//! Modified: 2 May 2026
5
6use core::fmt;
7use numra_core::NumraError;
8
9/// Errors that can occur during numerical integration.
10#[derive(Clone, Debug, PartialEq)]
11pub enum IntegrationError {
12    /// Maximum subdivisions reached without achieving tolerance.
13    MaxSubdivisions {
14        subdivisions: usize,
15        error_estimate: f64,
16    },
17    /// Roundoff error detected.
18    RoundoffDetected,
19    /// Integrand returned NaN or infinity.
20    InvalidValue { x: f64 },
21    /// Bad input (e.g., a >= b for finite interval).
22    InvalidInput(String),
23}
24
25impl fmt::Display for IntegrationError {
26    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
27        match self {
28            IntegrationError::MaxSubdivisions {
29                subdivisions,
30                error_estimate,
31            } => {
32                write!(
33                    f,
34                    "Maximum subdivisions ({}) reached, error estimate: {:.2e}",
35                    subdivisions, error_estimate
36                )
37            }
38            IntegrationError::RoundoffDetected => {
39                write!(f, "Roundoff error detected during integration")
40            }
41            IntegrationError::InvalidValue { x } => {
42                write!(f, "Integrand returned invalid value at x = {}", x)
43            }
44            IntegrationError::InvalidInput(msg) => {
45                write!(f, "Invalid input: {}", msg)
46            }
47        }
48    }
49}
50
51impl From<IntegrationError> for NumraError {
52    fn from(e: IntegrationError) -> Self {
53        NumraError::Integrate(e.to_string())
54    }
55}