Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ num = "0.4.1"
num-bigint = "0.4.4"
num-traits = "0.2.18"
bigdecimal = "0.4.2"
statrs = "0.18.0"

[profile.release]
opt-level = 3
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ There are many examples of processed expressions in the [integration test file](
Ceil
Round
Exp
Cdf
```

## Built-in Defined Constants
Expand Down
7 changes: 7 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,13 @@
//! Log
//! Abs
//! Sqrt
//! Max
//! Min
//! Floor
//! Ceil
//! Round
//! Exp
//! Cdf
//! ```
/// Parser
pub mod parser;
Expand Down
5 changes: 5 additions & 0 deletions src/rpn_resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::{
parser::Parser,
token::{self, MathFunction, Number, Operator, Token},
};
use statrs::distribution::{ContinuousCDF, Normal};
use anyhow::anyhow;
use log::debug;
use std::{
Expand Down Expand Up @@ -200,6 +201,10 @@ impl RpnResolver<'_> {
MathFunction::Floor => f64::floor(value.into()),
MathFunction::Ceil => f64::ceil(value.into()),
MathFunction::Round => f64::round(value.into()),
MathFunction::Cdf => {
let normal = Normal::new(0.0, 1.0).expect("valid normal dist");
normal.cdf(value.into())
}
MathFunction::Exp => f64::exp(value.into()),
MathFunction::None => return Err(anyhow!("This should never happen!")),
};
Expand Down
3 changes: 3 additions & 0 deletions src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ pub enum MathFunction {
Round,
/// e^x exponentiation
Exp,
/// Standard Normal cumulative distribution function
Cdf,
/// No function expected
None,
}
Expand Down Expand Up @@ -182,6 +184,7 @@ impl Token<'_> {
"ceil" => Some(MathFunction::Ceil),
"round" => Some(MathFunction::Round),
"exp" => Some(MathFunction::Exp),
"cdf" => Some(MathFunction::Cdf),
&_ => None,
}
}
Expand Down
1 change: 1 addition & 0 deletions tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ fn test_expressions() {
resolve_decimal!("round(3.6)", 4.0);
resolve_decimal!("round(3.4)", 3.0);
resolve_decimal!("exp(1)", std::f64::consts::E);
resolve_decimal!("cdf(0)", 0.5);

resolve_err!("min()");
resolve_err!("max()");
Expand Down