Evaluation of round, ceil, floor, abs
This commit is contained in:
parent
ec5b3371fc
commit
ce74ce83a0
@ -83,10 +83,32 @@ fn evaluate_node(ast_node: &AstNode) -> Result<Answer, String> {
|
||||
return Err(format!("exp() only accepts UnitType::NoUnit").to_string())
|
||||
}
|
||||
},
|
||||
// Ceil
|
||||
// Floor
|
||||
// Round
|
||||
// Fabs,
|
||||
Round => {
|
||||
// .quantize() rounds .5 to nearest even integer, so we correct that
|
||||
let mut result = child_answer.value.quantize(d128!(1));
|
||||
let rounding_change = result - child_answer.value;
|
||||
// If the result was rounded down by 0.5, correct by +1
|
||||
if rounding_change == d128!(-0.5) { result += d128!(1); }
|
||||
return Ok(Answer::new(result, child_answer.unit))
|
||||
},
|
||||
Ceil => {
|
||||
let mut result = child_answer.value.quantize(d128!(1));
|
||||
let rounding_change = result - child_answer.value;
|
||||
if rounding_change.is_negative() { result += d128!(1); }
|
||||
return Ok(Answer::new(result, child_answer.unit))
|
||||
},
|
||||
Floor => {
|
||||
let mut result = child_answer.value.quantize(d128!(1));
|
||||
let rounding_change = result - child_answer.value;
|
||||
if !rounding_change.is_negative() { result -= d128!(1); }
|
||||
return Ok(Answer::new(result, child_answer.unit))
|
||||
},
|
||||
Abs => {
|
||||
let mut result = child_answer.value.abs();
|
||||
let rounding_change = result - child_answer.value;
|
||||
if rounding_change == d128!(-0.5) { result += d128!(1); }
|
||||
return Ok(Answer::new(result, child_answer.unit))
|
||||
},
|
||||
|
||||
// Sin,
|
||||
// Cos,
|
||||
|
||||
@ -5,7 +5,7 @@ use crate::Operator::{Caret, Divide, LeftParen, Minus, Modulo, Multiply, Plus, R
|
||||
use crate::UnaryOperator::{Percent, Factorial};
|
||||
use crate::TextOperator::{Of, To};
|
||||
use crate::Constant::{E, Pi};
|
||||
use crate::FunctionIdentifier::{Acos, Acosh, Asin, Asinh, Atan, Atanh, Cbrt, Ceil, Cos, Cosh, Exp, Fabs, Floor, Ln, Log, Round, Sin, Sinh, Sqrt, Tan, Tanh};
|
||||
use crate::FunctionIdentifier::{Acos, Acosh, Asin, Asinh, Atan, Atanh, Cbrt, Ceil, Cos, Cosh, Exp, Abs, Floor, Ln, Log, Round, Sin, Sinh, Sqrt, Tan, Tanh};
|
||||
use crate::units::Unit::*;
|
||||
|
||||
pub fn lex(input: &str) -> Result<TokenVector, String> {
|
||||
@ -76,10 +76,10 @@ pub fn lex(input: &str) -> Result<TokenVector, String> {
|
||||
"ln" => tokens.push(Token::FunctionIdentifier(Ln)),
|
||||
"exp" => tokens.push(Token::FunctionIdentifier(Exp)),
|
||||
|
||||
"round" | "rint" => tokens.push(Token::FunctionIdentifier(Round)),
|
||||
"ceil" => tokens.push(Token::FunctionIdentifier(Ceil)),
|
||||
"floor" => tokens.push(Token::FunctionIdentifier(Floor)),
|
||||
"round" | "rint" => tokens.push(Token::FunctionIdentifier(Round)),
|
||||
"fabs" => tokens.push(Token::FunctionIdentifier(Fabs)),
|
||||
"abs" | "fabs" => tokens.push(Token::FunctionIdentifier(Abs)),
|
||||
|
||||
"sin" => tokens.push(Token::FunctionIdentifier(Sin)),
|
||||
"cos" => tokens.push(Token::FunctionIdentifier(Cos)),
|
||||
|
||||
@ -42,10 +42,10 @@ pub enum FunctionIdentifier {
|
||||
Ln,
|
||||
Exp,
|
||||
|
||||
Round,
|
||||
Ceil,
|
||||
Floor,
|
||||
Round,
|
||||
Fabs,
|
||||
Abs,
|
||||
|
||||
Sin,
|
||||
Cos,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user