From 0f4478a9d5e604a2ec55899effcab9baa173a803 Mon Sep 17 00:00:00 2001 From: bakk Date: Tue, 18 Jan 2022 01:03:32 +0100 Subject: [PATCH] Fixed estimator showing huge square roots --- kalk/src/kalk_value/rounding.rs | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/kalk/src/kalk_value/rounding.rs b/kalk/src/kalk_value/rounding.rs index 31f2e58..e784ca4 100644 --- a/kalk/src/kalk_value/rounding.rs +++ b/kalk/src/kalk_value/rounding.rs @@ -57,7 +57,7 @@ pub(super) fn estimate( ComplexNumberType::Imaginary => imaginary, }; - if value > &f64::MAX { + if value >= &f64::MAX { return None; } @@ -224,17 +224,20 @@ fn equivalent_constant(value_str: &str) -> Option { } fn equivalent_root(value: f64) -> Option { - if value.fract() != 0f64 { - let squared = KalkValue::Number(float!(value * value), float!(0), String::new()) - .round_if_needed() - .values() - .0; - if squared.clone().sqrt().fract() != 0f64 && squared.clone().fract() == 0f64 { - return Some(format!("√{}", primitive!(squared) as i32)); - } + if value.fract().abs() == 0f64 || value > 10e5f64 { + return None; } - None + let squared = KalkValue::Number(float!(value * value), float!(0), String::new()) + .round_if_needed() + .values() + .0; + + if squared.clone().sqrt().fract() != 0f64 && squared.clone().fract() == 0f64 { + Some(format!("√{}", primitive!(squared) as i32)) + } else { + None + } } pub(super) fn round(