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())
|
return Err(format!("exp() only accepts UnitType::NoUnit").to_string())
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
// Ceil
|
Round => {
|
||||||
// Floor
|
// .quantize() rounds .5 to nearest even integer, so we correct that
|
||||||
// Round
|
let mut result = child_answer.value.quantize(d128!(1));
|
||||||
// Fabs,
|
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,
|
// Sin,
|
||||||
// Cos,
|
// Cos,
|
||||||
|
|||||||
@ -5,7 +5,7 @@ use crate::Operator::{Caret, Divide, LeftParen, Minus, Modulo, Multiply, Plus, R
|
|||||||
use crate::UnaryOperator::{Percent, Factorial};
|
use crate::UnaryOperator::{Percent, Factorial};
|
||||||
use crate::TextOperator::{Of, To};
|
use crate::TextOperator::{Of, To};
|
||||||
use crate::Constant::{E, Pi};
|
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::*;
|
use crate::units::Unit::*;
|
||||||
|
|
||||||
pub fn lex(input: &str) -> Result<TokenVector, String> {
|
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)),
|
"ln" => tokens.push(Token::FunctionIdentifier(Ln)),
|
||||||
"exp" => tokens.push(Token::FunctionIdentifier(Exp)),
|
"exp" => tokens.push(Token::FunctionIdentifier(Exp)),
|
||||||
|
|
||||||
|
"round" | "rint" => tokens.push(Token::FunctionIdentifier(Round)),
|
||||||
"ceil" => tokens.push(Token::FunctionIdentifier(Ceil)),
|
"ceil" => tokens.push(Token::FunctionIdentifier(Ceil)),
|
||||||
"floor" => tokens.push(Token::FunctionIdentifier(Floor)),
|
"floor" => tokens.push(Token::FunctionIdentifier(Floor)),
|
||||||
"round" | "rint" => tokens.push(Token::FunctionIdentifier(Round)),
|
"abs" | "fabs" => tokens.push(Token::FunctionIdentifier(Abs)),
|
||||||
"fabs" => tokens.push(Token::FunctionIdentifier(Fabs)),
|
|
||||||
|
|
||||||
"sin" => tokens.push(Token::FunctionIdentifier(Sin)),
|
"sin" => tokens.push(Token::FunctionIdentifier(Sin)),
|
||||||
"cos" => tokens.push(Token::FunctionIdentifier(Cos)),
|
"cos" => tokens.push(Token::FunctionIdentifier(Cos)),
|
||||||
|
|||||||
@ -42,10 +42,10 @@ pub enum FunctionIdentifier {
|
|||||||
Ln,
|
Ln,
|
||||||
Exp,
|
Exp,
|
||||||
|
|
||||||
|
Round,
|
||||||
Ceil,
|
Ceil,
|
||||||
Floor,
|
Floor,
|
||||||
Round,
|
Abs,
|
||||||
Fabs,
|
|
||||||
|
|
||||||
Sin,
|
Sin,
|
||||||
Cos,
|
Cos,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user