mirror of
https://github.com/PaddiM8/kalker.git
synced 2025-03-01 06:41:15 +01:00
Division with complex numbers
This commit is contained in:
parent
4042f2d801
commit
aeef49e9ef
@ -292,8 +292,23 @@ impl KalkNum {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn div(self, context: &mut crate::interpreter::Context, rhs: KalkNum) -> KalkNum {
|
pub(crate) fn div(self, context: &mut crate::interpreter::Context, rhs: KalkNum) -> KalkNum {
|
||||||
let right = calculate_unit(context, &self, rhs.clone()).unwrap_or(rhs);
|
let right = calculate_unit(context, &self, rhs.clone()).unwrap_or(rhs.clone());
|
||||||
KalkNum::new(self.value / right.value, &right.unit)
|
|
||||||
|
// Avoid unecessary calculations
|
||||||
|
if self.imaginary_value == 0f64 && right.imaginary_value == 0f64 {
|
||||||
|
KalkNum::new(self.value / right.value, &right.unit)
|
||||||
|
} else {
|
||||||
|
// Multiply both the numerator and denominator
|
||||||
|
// with the conjugate of the denominator, and divide.
|
||||||
|
let conjugate = rhs.get_conjugate();
|
||||||
|
let numerator = self.clone().mul(context, conjugate.clone());
|
||||||
|
let denominator = rhs.clone().mul(context, conjugate);
|
||||||
|
KalkNum::new_with_imaginary(
|
||||||
|
numerator.value / denominator.value.clone(),
|
||||||
|
&right.unit,
|
||||||
|
numerator.imaginary_value / denominator.value,
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn rem(self, context: &mut crate::interpreter::Context, rhs: KalkNum) -> KalkNum {
|
pub(crate) fn rem(self, context: &mut crate::interpreter::Context, rhs: KalkNum) -> KalkNum {
|
||||||
@ -301,6 +316,14 @@ impl KalkNum {
|
|||||||
KalkNum::new(self.value % right.value, &right.unit)
|
KalkNum::new(self.value % right.value, &right.unit)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn get_conjugate(&self) -> KalkNum {
|
||||||
|
KalkNum::new_with_imaginary(
|
||||||
|
self.value.clone(),
|
||||||
|
&self.unit,
|
||||||
|
self.imaginary_value.clone() * (-1f64),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
/// Get an estimate of what the number is, eg. 3.141592 => π
|
/// Get an estimate of what the number is, eg. 3.141592 => π
|
||||||
pub fn estimate(&self) -> Option<String> {
|
pub fn estimate(&self) -> Option<String> {
|
||||||
let fract = self.value.clone().fract().abs();
|
let fract = self.value.clone().fract().abs();
|
||||||
|
Loading…
Reference in New Issue
Block a user