mirror of
https://github.com/PaddiM8/kalker.git
synced 2025-06-26 20:52:29 +02:00
Float error margin for comparison operations
This commit is contained in:
parent
535a834076
commit
814558d46c
@ -17,6 +17,8 @@ use lazy_static::lazy_static;
|
|||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use wasm_bindgen::prelude::*;
|
use wasm_bindgen::prelude::*;
|
||||||
|
|
||||||
|
const ACCEPTABLE_COMPARISON_MARGIN: f64 = 0.00000001;
|
||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
static ref CONSTANTS: HashMap<&'static str, &'static str> = {
|
static ref CONSTANTS: HashMap<&'static str, &'static str> = {
|
||||||
let mut m = HashMap::new();
|
let mut m = HashMap::new();
|
||||||
@ -319,15 +321,13 @@ impl KalkNum {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn eq(self, context: &mut crate::interpreter::Context, rhs: KalkNum) -> KalkNum {
|
pub(crate) fn eq(self, context: &mut crate::interpreter::Context, rhs: KalkNum) -> KalkNum {
|
||||||
let mut right = calculate_unit(context, &self, rhs.clone()).unwrap_or(rhs);
|
let right = calculate_unit(context, &self, rhs.clone()).unwrap_or(rhs);
|
||||||
right.boolean_value = Some(self.value == right.value);
|
self.eq_without_unit(&right)
|
||||||
right
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn not_eq(self, context: &mut crate::interpreter::Context, rhs: KalkNum) -> KalkNum {
|
pub(crate) fn not_eq(self, context: &mut crate::interpreter::Context, rhs: KalkNum) -> KalkNum {
|
||||||
let mut right = calculate_unit(context, &self, rhs.clone()).unwrap_or(rhs);
|
let right = calculate_unit(context, &self, rhs.clone()).unwrap_or(rhs);
|
||||||
right.boolean_value = Some(self.value != right.value);
|
self.not_eq_without_unit(&right)
|
||||||
right
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn greater_than(
|
pub(crate) fn greater_than(
|
||||||
@ -335,9 +335,8 @@ impl KalkNum {
|
|||||||
context: &mut crate::interpreter::Context,
|
context: &mut crate::interpreter::Context,
|
||||||
rhs: KalkNum,
|
rhs: KalkNum,
|
||||||
) -> KalkNum {
|
) -> KalkNum {
|
||||||
let mut right = calculate_unit(context, &self, rhs.clone()).unwrap_or(rhs);
|
let right = calculate_unit(context, &self, rhs.clone()).unwrap_or(rhs);
|
||||||
right.boolean_value = Some(self.value > right.value);
|
self.greater_than_without_unit(&right)
|
||||||
right
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn less_than(
|
pub(crate) fn less_than(
|
||||||
@ -345,9 +344,8 @@ impl KalkNum {
|
|||||||
context: &mut crate::interpreter::Context,
|
context: &mut crate::interpreter::Context,
|
||||||
rhs: KalkNum,
|
rhs: KalkNum,
|
||||||
) -> KalkNum {
|
) -> KalkNum {
|
||||||
let mut right = calculate_unit(context, &self, rhs.clone()).unwrap_or(rhs);
|
let right = calculate_unit(context, &self, rhs.clone()).unwrap_or(rhs);
|
||||||
right.boolean_value = Some(self.value < right.value);
|
self.less_than_without_unit(&right)
|
||||||
right
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn greater_or_equals(
|
pub(crate) fn greater_or_equals(
|
||||||
@ -355,9 +353,13 @@ impl KalkNum {
|
|||||||
context: &mut crate::interpreter::Context,
|
context: &mut crate::interpreter::Context,
|
||||||
rhs: KalkNum,
|
rhs: KalkNum,
|
||||||
) -> KalkNum {
|
) -> KalkNum {
|
||||||
let mut right = calculate_unit(context, &self, rhs.clone()).unwrap_or(rhs);
|
let right = calculate_unit(context, &self, rhs.clone()).unwrap_or(rhs.clone());
|
||||||
right.boolean_value = Some(self.value >= right.value);
|
let greater = self
|
||||||
right
|
.greater_than_without_unit(&right)
|
||||||
|
.boolean_value
|
||||||
|
.unwrap();
|
||||||
|
let equal = self.eq_without_unit(&right).boolean_value.unwrap();
|
||||||
|
KalkNum::from_bool(greater || equal)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn less_or_equals(
|
pub(crate) fn less_or_equals(
|
||||||
@ -365,9 +367,10 @@ impl KalkNum {
|
|||||||
context: &mut crate::interpreter::Context,
|
context: &mut crate::interpreter::Context,
|
||||||
rhs: KalkNum,
|
rhs: KalkNum,
|
||||||
) -> KalkNum {
|
) -> KalkNum {
|
||||||
let mut right = calculate_unit(context, &self, rhs.clone()).unwrap_or(rhs);
|
let right = calculate_unit(context, &self, rhs.clone()).unwrap_or(rhs.clone());
|
||||||
right.boolean_value = Some(self.value <= right.value);
|
let less = self.less_than_without_unit(&right).boolean_value.unwrap();
|
||||||
right
|
let equal = self.eq_without_unit(&right).boolean_value.unwrap();
|
||||||
|
KalkNum::from_bool(less || equal)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn add_without_unit(self, rhs: KalkNum) -> KalkNum {
|
pub(crate) fn add_without_unit(self, rhs: KalkNum) -> KalkNum {
|
||||||
@ -414,6 +417,25 @@ impl KalkNum {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) fn eq_without_unit(&self, rhs: &KalkNum) -> KalkNum {
|
||||||
|
KalkNum::from_bool(
|
||||||
|
(self.value.clone() - rhs.value.clone()).abs() < ACCEPTABLE_COMPARISON_MARGIN,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn not_eq_without_unit(&self, rhs: &KalkNum) -> KalkNum {
|
||||||
|
KalkNum::from_bool(
|
||||||
|
(self.value.clone() - rhs.value.clone()).abs() > ACCEPTABLE_COMPARISON_MARGIN,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn greater_than_without_unit(&self, rhs: &KalkNum) -> KalkNum {
|
||||||
|
KalkNum::from_bool(self.value.clone() - rhs.value.clone() > ACCEPTABLE_COMPARISON_MARGIN)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn less_than_without_unit(&self, rhs: &KalkNum) -> KalkNum {
|
||||||
|
KalkNum::from_bool(self.value.clone() - rhs.value.clone() < -ACCEPTABLE_COMPARISON_MARGIN)
|
||||||
|
}
|
||||||
pub fn get_conjugate(&self) -> KalkNum {
|
pub fn get_conjugate(&self) -> KalkNum {
|
||||||
KalkNum::new_with_imaginary(
|
KalkNum::new_with_imaginary(
|
||||||
self.value.clone(),
|
self.value.clone(),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user