Finding square roots for estimate()

This commit is contained in:
bakk 2021-05-23 01:10:22 +02:00
parent 34e374b346
commit 0bbf1c4974

View File

@ -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))