Show approximation sign for calculations involving an an derivation, integration or an equation

This commit is contained in:
PaddiM8 2022-05-31 23:59:06 +02:00
parent 7154d38eb3
commit 7b4ccc8443
3 changed files with 26 additions and 5 deletions

View File

@ -6,14 +6,19 @@ use crate::kalk_value::{ComplexNumberType, KalkValue, ScientificNotation};
pub struct CalculationResult {
value: KalkValue,
radix: u8,
is_approximation: bool,
}
// Wraps around KalkValue since enums don't work
// with the javascript bindings.
#[wasm_bindgen]
impl CalculationResult {
pub(crate) fn new(value: KalkValue, radix: u8) -> Self {
CalculationResult { value, radix }
pub(crate) fn new(value: KalkValue, radix: u8, is_approximation: bool) -> Self {
CalculationResult {
value,
radix,
is_approximation,
}
}
#[allow(dead_code)]
@ -33,7 +38,7 @@ impl CalculationResult {
#[wasm_bindgen(js_name = toPrettyString)]
pub fn to_string_pretty(&self) -> String {
if self.radix == 10 {
let value = if self.radix == 10 {
self.value.to_string_pretty_radix(10)
} else {
format!(
@ -41,6 +46,12 @@ impl CalculationResult {
self.value.to_string_pretty_radix(10),
self.value.to_string_pretty_radix(self.radix),
)
};
if self.is_approximation {
format!("{}", value)
} else {
value
}
}

View File

@ -19,6 +19,7 @@ pub struct Context<'a> {
timeout: Option<u128>,
#[cfg(not(target_arch = "wasm32"))]
start_time: std::time::SystemTime,
is_approximation: bool,
}
impl<'a> Context<'a> {
@ -38,6 +39,7 @@ impl<'a> Context<'a> {
timeout,
#[cfg(not(target_arch = "wasm32"))]
start_time: std::time::SystemTime::now(),
is_approximation: false,
}
}
@ -66,7 +68,7 @@ impl<'a> Context<'a> {
if i == statements.len() - 1 {
if let Stmt::Expr(_) = stmt {
return Ok(Some(CalculationResult::new(num, 10)));
return Ok(Some(CalculationResult::new(num, 10, self.is_approximation)));
}
}
}
@ -326,6 +328,10 @@ pub(crate) fn eval_fn_call_expr(
expressions: &[Expr],
unit: Option<&String>,
) -> Result<KalkValue, KalkError> {
if identifier.prime_count > 0 {
context.is_approximation = true;
}
// Special functions
match identifier.full_name.as_ref() {
"sum" | "prod" => {
@ -355,6 +361,8 @@ pub(crate) fn eval_fn_call_expr(
}
}
"integrate" => {
context.is_approximation = true;
return match expressions.len() {
3 => numerical::integrate_with_unknown_variable(
context,
@ -778,6 +786,8 @@ fn eval_equation(
right: &Expr,
unknown_var: &Identifier,
) -> Result<KalkValue, KalkError> {
context.is_approximation = true;
let expr = Expr::Binary(
Box::new(left.clone()),
TokenKind::Minus,

View File

@ -353,7 +353,7 @@ fn invert_fn_call(
let mut parameters_iter = parameters.iter();
for argument in arguments {
symbol_table.insert(Stmt::VarDecl(
Identifier::from_full_name(&parameters_iter.next().unwrap().to_string()),
Identifier::from_full_name(parameters_iter.next().unwrap()),
Box::new(argument.clone()),
));
}