fix π approximation

This commit is contained in:
jacekpoz 2023-11-05 17:17:06 +01:00
parent 80b72a2386
commit d4444e6580
No known key found for this signature in database
GPG key ID: 94E812A8B12AAE3C

View file

@ -39,7 +39,7 @@ fn points_under_graph(
fn get_sup( fn get_sup(
integral: &Integral<f64> integral: &Integral<f64>
) -> f64 { ) -> f64 {
let multiplier = 1_000f64; let multiplier = 1000.0;
let b = &integral.bounds; let b = &integral.bounds;
let f = &integral.function; let f = &integral.function;
@ -49,8 +49,14 @@ fn get_sup(
(((b.start * multiplier) as i64)..((b.end * multiplier) as i64)) (((b.start * multiplier) as i64)..((b.end * multiplier) as i64))
.map(|x| (x as f64) / multiplier) .map(|x| (x as f64) / multiplier)
{ {
if f(i) > sup { let y = f(i);
sup = f(i); // dla aproksymacji π, f(-1) i f(1)
// dawały ∞, ten if naprawia problem
if sup.is_infinite() {
sup = y;
}
if y > sup && y.is_normal() {
sup = y;
} }
} }
@ -129,33 +135,35 @@ fn graph_function(
} }
fn main() { fn main() {
// przykład z pdfu
let i0 = Integral::<f64> { let i0 = Integral::<f64> {
bounds: (1.0..3.0), bounds: (1.0..3.0),
function: Box::new(|x| x.powi(3)), function: Box::new(|x| x.powi(3)),
}; };
graph_function("0.png", 50, &i0, 20.0); graph_function("graphs/0.png", 50, &i0, 20.0);
let i1 = Integral::<f64> { let i1 = Integral::<f64> {
bounds: (0.0..8.0), bounds: (0.0..8.0),
function: Box::new(|x| x.powf(1.0 / 3.0)), function: Box::new(|x| x.powf(1.0 / 3.0)),
}; };
graph_function("1.png", 50, &i1, 12.0); graph_function("graphs/1.png", 50, &i1, 12.0);
let i2 = Integral::<f64> { let i2 = Integral::<f64> {
bounds: (0.0..std::f64::consts::PI), bounds: (0.0..std::f64::consts::PI),
function: Box::new(|x| x.sin()), function: Box::new(|x| x.sin()),
}; };
graph_function("2.png", 50, &i2, 2.0); graph_function("graphs/2.png", 50, &i2, 2.0);
let i3 = Integral::<f64> { let i3 = Integral::<f64> {
bounds: (0.0..1.0), bounds: (0.0..1.0),
function: Box::new(|x| 4.0 * x * (1.0 - x).powi(3)), function: Box::new(|x| 4.0 * x * (1.0 - x).powi(3)),
}; };
graph_function("3.png", 50, &i3, 0.2); graph_function("graphs/3.png", 50, &i3, 0.2);
// aproksymacja π
let i_pi = Integral::<f64> { let i_pi = Integral::<f64> {
bounds: (0.0..1.0), bounds: (-1.0..1.0),
function: Box::new(|x| 1.0 / (1.0 - (x * x)).sqrt()), function: Box::new(|x| 1.0 / (1.0 - (x * x)).sqrt()),
}; };
graph_function("pi.png", 50, &i_pi, std::f64::consts::PI / 2.0); graph_function("graphs/π.png", 50, &i_pi, std::f64::consts::PI);
} }