1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
//! This module contains the definition of SlopeParams, useful to define a
//! polynomial function fastly.

/// This struct easies the definition of a simple polynomial.
pub struct SlopeParams {
    /// Value in which the slope starts.
    pub start: f64,
    /// Minimum (or maximum if the slope is positive) possible value of the slope.
    pub bound: f64,
    /// Coefficient of the slope (usually negative to decease rate along iterations).
    pub coefficient: f64,
}

impl SlopeParams {
    /// Returns the expected number: the bound or the result of the slope.
    pub fn check_bound(&self, y: f64) -> f64 {
        if self.coefficient >= 0.0f64 {
            if y < self.bound {
                y
            } else {
                self.bound
            }
        } else if y > self.bound {
            y
        } else {
            self.bound
        }
    }
}