Keep previous variable value after integration using the same identifier

This commit is contained in:
bakk 2021-05-24 21:51:25 +02:00
parent c4c768374f
commit f944e3cebc
2 changed files with 15 additions and 0 deletions

View File

@ -79,6 +79,9 @@ fn simpsons_rule(
integration_variable: &str,
) -> Result<KalkNum, CalcError> {
let mut result = KalkNum::default();
let original_variable_value = context
.symbol_table
.get_and_remove_var(integration_variable);
const N: i32 = 900;
let a = interpreter::eval_expr(context, a_expr, "")?;
@ -105,6 +108,14 @@ fn simpsons_rule(
result.imaginary_value += mul.imaginary_value;
}
if let Some(value) = original_variable_value {
context.symbol_table.insert(value);
} else {
context
.symbol_table
.get_and_remove_var(integration_variable);
}
Ok(result.mul_without_unit(KalkNum::new_with_imaginary(
3f64 / 8f64 * h.value,
&h.unit,

View File

@ -81,6 +81,10 @@ impl SymbolTable {
}
}
pub fn get_and_remove_var(&mut self, identifier: &str) -> Option<Stmt> {
self.hashmap.remove(&format!("var.{}", identifier))
}
pub fn contains_var(&self, identifier: &str) -> bool {
prelude::CONSTANTS.contains_key(identifier)
|| identifier == "i"