31 lines
700 B
Text
31 lines
700 B
Text
use num::Float;
|
|
|
|
/// Note: We cannot use `range_step` here because Floats don't implement
|
|
/// the `CheckedAdd` trait.
|
|
fn find_roots<T, F>(f: F, start: T, stop: T, step: T, epsilon: T) -> Vec<T>
|
|
where
|
|
T: Copy + PartialOrd + Float,
|
|
F: Fn(T) -> T,
|
|
{
|
|
let mut ret = vec![];
|
|
let mut current = start;
|
|
while current < stop {
|
|
if f(current).abs() < epsilon {
|
|
ret.push(current);
|
|
}
|
|
current = current + step;
|
|
}
|
|
ret
|
|
}
|
|
|
|
fn main() {
|
|
let roots = find_roots(
|
|
|x: f64| x * x * x - 3.0 * x * x + 2.0 * x,
|
|
-1.0,
|
|
3.0,
|
|
0.0001,
|
|
0.00000001,
|
|
);
|
|
|
|
println!("roots of f(x) = x^3 - 3x^2 + 2x are: {:?}", roots);
|
|
}
|