From 0bbf1c4974fa58d44e46f6caa38ef1693c41f1dc Mon Sep 17 00:00:00 2001 From: bakk Date: Sun, 23 May 2021 01:10:22 +0200 Subject: [PATCH] Finding square roots for estimate() --- kalk/src/kalk_num/mod.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/kalk/src/kalk_num/mod.rs b/kalk/src/kalk_num/mod.rs index 5fbe237..e553dba 100644 --- a/kalk/src/kalk_num/mod.rs +++ b/kalk/src/kalk_num/mod.rs @@ -377,7 +377,7 @@ impl KalkNum { output.push_str(&format!("{}i", value)); } - } else if self.has_real() { + } else if self.has_imaginary() { // If there is a real value as well if output.len() > 0 { output.push_str(" + "); @@ -473,6 +473,14 @@ impl KalkNum { } } + // If the value squared (and rounded) is an integer, + // eg. x² is an integer, + // then it can be expressed as sqrt(x²) + let squared = KalkNum::new(value.clone() * value, "").round_if_needed(); + if squared.value.clone().fract() == 0f64 { + return Some(format!("√{}", squared.to_string())); + } + // If nothing above was relevant, simply round it off a bit, eg. from 0.99999 to 1 let rounded = self.round_one_value(complex_number_type)?.to_string(); Some(trim_zeroes(&rounded))