From d4444e6580c3cbb6c0e56335ec40bde34210ca69 Mon Sep 17 00:00:00 2001 From: jacekpoz Date: Sun, 5 Nov 2023 17:17:06 +0100 Subject: [PATCH] =?UTF-8?q?fix=20=CF=80=20approximation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main.rs | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/src/main.rs b/src/main.rs index 0e8ea5d..beac60f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -39,7 +39,7 @@ fn points_under_graph( fn get_sup( integral: &Integral ) -> f64 { - let multiplier = 1_000f64; + let multiplier = 1000.0; let b = &integral.bounds; let f = &integral.function; @@ -49,8 +49,14 @@ fn get_sup( (((b.start * multiplier) as i64)..((b.end * multiplier) as i64)) .map(|x| (x as f64) / multiplier) { - if f(i) > sup { - sup = f(i); + let y = 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() { + // przykład z pdfu let i0 = Integral:: { bounds: (1.0..3.0), 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:: { bounds: (0.0..8.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:: { bounds: (0.0..std::f64::consts::PI), 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:: { bounds: (0.0..1.0), 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:: { - bounds: (0.0..1.0), + bounds: (-1.0..1.0), 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); }