use std::collections::HashMap;
use std::collections::HashSet;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::LazyLock;
use crate::calc::context::EvalContext;
#[derive(Debug)]
pub enum EvaluationError {
ParseError(String),
DivisionByZero,
InvalidOperation(String),
UnknownFunction(String),
UnknownConstant(String),
StackOverflow,
ValueOverflow,
}
impl std::fmt::Display for EvaluationError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::ParseError(s) => write!(f, "Parse error: {}", s),
Self::DivisionByZero => write!(f, "Division by zero"),
Self::InvalidOperation(s) => write!(f, "Invalid operation: {}", s),
Self::UnknownFunction(s) => write!(f, "Unknown function: {}", s),
Self::UnknownConstant(s) => write!(f, "Unknown constant: {}", s),
Self::StackOverflow => write!(f, "Stack overflow (expression too nested)"),
Self::ValueOverflow => write!(f, "Value overflow"),
}
}
}
const MAX_NESTING_DEPTH: usize = 100;
const MAX_EXPONENT: f64 = 10_000.0;
const MAX_RESULT_VALUE: f64 = 1e308;
const MAX_SHIFT_COUNT: usize = 50_000;
const MAX_INPUT_LENGTH: usize = 100_000;
const MAX_FACTORIAL: i64 = 1000;
const MAX_PRIME: i64 = 1_000_000_000_000; const MAX_PERM_COMB: i64 = 10_000;
#[doc(hidden)]
pub static CONSTANTS: LazyLock<HashMap<&'static str, f64>> = LazyLock::new(|| {
let mut m = HashMap::new();
m.insert("pi", std::f64::consts::PI);
m.insert("e", std::f64::consts::E);
m.insert("tau", std::f64::consts::TAU);
m.insert("phi", 1.618_033_988_749_895);
m.insert("c", 299792458.0);
m.insert("c0", 299792458.0);
m.insert("speedoflight", 299792458.0);
m.insert("speedoflightvacuum", 299792458.0);
m.insert("epsilon0", 8.8541878128e-12);
m.insert("vacuumpermittivity", 8.8541878128e-12);
m.insert("mu0", 1.25663706212e-6);
m.insert("vacuumpermeability", 1.25663706212e-6);
m.insert("planck", 6.62607015e-34);
m.insert("planckconstant", 6.62607015e-34);
m.insert("hbar", 1.054571817e-34);
m.insert("planckbar", 1.054571817e-34);
m.insert("reducedplanck", 1.054571817e-34);
m.insert("k", 1.380649e-23);
m.insert("boltzmann", 1.380649e-23);
m.insert("boltzmannconstant", 1.380649e-23);
m.insert("R", 8.314462618);
m.insert("r", 8.314462618);
m.insert("gasconstant", 8.314462618);
m.insert("idealgasconstant", 8.314462618);
m.insert("G", 6.67430e-11);
m.insert("gravitationalconstant", 6.67430e-11);
m.insert("na", 6.02214076e23);
m.insert("avogadro", 6.02214076e23);
m.insert("avogadros", 6.02214076e23);
m.insert("qe", 1.602176634e-19);
m.insert("echarge", 1.602176634e-19);
m.insert("elementarycharge", 1.602176634e-19);
m.insert("me", 9.1093837015e-31);
m.insert("electronmass", 9.1093837015e-31);
m.insert("re", 2.8179403262e-15);
m.insert("electronradius", 2.8179403262e-15);
m.insert("mp", 1.67262192369e-27);
m.insert("protonmass", 1.67262192369e-27);
m.insert("mn", 1.67493e-27);
m.insert("neutronmass", 1.67493e-27);
m.insert("u", 1.6605390666e-27);
m.insert("amu", 1.6605390666e-27);
m.insert("atomicmassunit", 1.6605390666e-27);
m.insert("alpha", 0.0072973525693);
m.insert("finestructure", 0.0072973525693);
m.insert("rydberg", 10973731.56816);
m.insert("rydbergconstant", 10973731.56816);
m.insert("stefan", 5.670374419e-08);
m.insert("stefanboltzmann", 5.670374419e-08);
m.insert("wien", 0.002897771955);
m.insert("wienconstant", 0.002897771955);
m.insert("f", 96485.33212);
m.insert("faraday", 96485.33212);
m.insert("faradayconstant", 96485.33212);
m.insert("standardgravity", 9.80665);
m
});
pub type EvaluateResult = (String, String);
static RANDOM_FUNCTIONS: LazyLock<HashSet<&'static str>> = LazyLock::new(|| {
let mut s = HashSet::new();
for &name in &[
"random",
"randint",
"randrange",
"uniform",
"randn",
"gauss",
"seed",
] {
s.insert(name);
}
s
});
static SIDE_EFFECT_FUNCTIONS: LazyLock<HashSet<&'static str>> = LazyLock::new(|| {
let mut s = HashSet::new();
for &name in &[
"store",
"recall",
"m",
"mplus",
"mminus",
"mc",
"mr",
"setvar",
"getvar",
"delvar",
"listvars",
"clearvars",
] {
s.insert(name);
}
s
});
static MCP_MODE: AtomicBool = AtomicBool::new(false);
static ALLOW_RANDOM: AtomicBool = AtomicBool::new(true);
static ALLOW_SIDE_EFFECTS: AtomicBool = AtomicBool::new(true);
const MAX_USER_VARIABLES: usize = 1000;
static MEMORY_REGISTERS: LazyLock<std::sync::Mutex<HashMap<String, f64>>> =
LazyLock::new(|| std::sync::Mutex::new(HashMap::new()));
static USER_VARIABLES: LazyLock<std::sync::Mutex<HashMap<String, f64>>> =
LazyLock::new(|| std::sync::Mutex::new(HashMap::new()));
static PRNG_STATE: LazyLock<std::sync::Mutex<(u64, Option<f64>)>> =
LazyLock::new(|| std::sync::Mutex::new((123456789, None)));
#[deprecated(
since = "1.0.0",
note = "use EvalContext::mcp_mode() instead. The global AtomicBool flags remain for legacy evaluate()/run() callers."
)]
pub fn set_mcp_mode() {
if MCP_MODE.swap(true, Ordering::SeqCst) {
return; }
ALLOW_RANDOM.store(false, Ordering::SeqCst);
ALLOW_SIDE_EFFECTS.store(false, Ordering::SeqCst);
}
pub fn is_mcp_mode() -> bool {
MCP_MODE.load(Ordering::SeqCst)
}
fn check_function_allowed(name: &str) -> Result<(), EvaluationError> {
let lower = name.to_lowercase();
if !ALLOW_RANDOM.load(Ordering::SeqCst) && RANDOM_FUNCTIONS.contains(lower.as_str()) {
return Err(EvaluationError::InvalidOperation(format!(
"Function '{}' is non-deterministic and is disabled in MCP mode (allow_random=false)",
name
)));
}
if !ALLOW_SIDE_EFFECTS.load(Ordering::SeqCst) && SIDE_EFFECT_FUNCTIONS.contains(lower.as_str())
{
return Err(EvaluationError::InvalidOperation(format!(
"Function '{}' mutates evaluator state and is disabled in MCP mode (allow_side_effects=false)",
name
)));
}
Ok(())
}
fn check_function_allowed_with(name: &str, ctx: &EvalContext) -> Result<(), EvaluationError> {
let lower = name.to_lowercase();
if !ctx.allow_random && RANDOM_FUNCTIONS.contains(lower.as_str()) {
return Err(EvaluationError::InvalidOperation(format!(
"Function '{}' is non-deterministic and is disabled in MCP mode (allow_random=false)",
name
)));
}
if !ctx.allow_side_effects && SIDE_EFFECT_FUNCTIONS.contains(lower.as_str()) {
return Err(EvaluationError::InvalidOperation(format!(
"Function '{}' mutates evaluator state and is disabled in MCP mode (allow_side_effects=false)",
name
)));
}
Ok(())
}
fn prng_random_with(ctx: &mut EvalContext) -> f64 {
xorshift64(&mut ctx.prng_state) as f64 / u64::MAX as f64
}
fn prng_seed_with(ctx: &mut EvalContext, s: u64) {
ctx.prng_state = if s == 0 { 123456789 } else { s };
ctx.gauss_spare = None;
}
fn prng_randn_with(ctx: &mut EvalContext) -> f64 {
if let Some(val) = ctx.gauss_spare.take() {
return val;
}
let mut u1 = xorshift64(&mut ctx.prng_state) as f64 / u64::MAX as f64;
while u1 == 0.0 {
u1 = xorshift64(&mut ctx.prng_state) as f64 / u64::MAX as f64;
}
let u2 = xorshift64(&mut ctx.prng_state) as f64 / u64::MAX as f64;
let z0 = (-2.0 * u1.ln()).sqrt() * (2.0 * std::f64::consts::PI * u2).cos();
let z1 = (-2.0 * u1.ln()).sqrt() * (2.0 * std::f64::consts::PI * u2).sin();
ctx.gauss_spare = Some(z1);
z0
}
const MAX_USER_VARIABLES_CTX: usize = MAX_USER_VARIABLES;
pub fn evaluate_with_context(expr: &str, ctx: &mut EvalContext) -> Result<EvaluateResult, String> {
let expr = expr.trim();
if expr.len() > MAX_INPUT_LENGTH {
return Err(format!("Input exceeds {} bytes", MAX_INPUT_LENGTH));
}
match parse_expression_with(expr, &mut 0, ctx) {
Ok(result) => format_result(result),
Err(EvaluationError::InvalidOperation(ref s)) if s.starts_with("__string_result__") => {
let value = s.trim_start_matches("__string_result__").to_string();
Ok((value, "str".to_string()))
}
Err(EvaluationError::InvalidOperation(ref s)) if s.starts_with("__int_result__") => {
let value = s.trim_start_matches("__int_result__").to_string();
Ok((value, "int".to_string()))
}
Err(e) => Err(e.to_string()),
}
}
pub fn evaluate(expr: &str) -> Result<EvaluateResult, String> {
let expr = expr.trim();
if expr.len() > MAX_INPUT_LENGTH {
return Err(format!("Input exceeds {} bytes", MAX_INPUT_LENGTH));
}
match parse_expression(expr, &mut 0) {
Ok(result) => format_result(result),
Err(EvaluationError::InvalidOperation(ref s)) if s.starts_with("__string_result__") => {
let value = s.trim_start_matches("__string_result__").to_string();
Ok((value, "str".to_string()))
}
Err(EvaluationError::InvalidOperation(ref s)) if s.starts_with("__int_result__") => {
let value = s.trim_start_matches("__int_result__").to_string();
Ok((value, "int".to_string()))
}
Err(e) => Err(e.to_string()),
}
}
fn format_result(result: f64) -> Result<EvaluateResult, String> {
if result.is_nan() || result.is_infinite() {
Err(EvaluationError::ValueOverflow.to_string())
} else if result.fract() == 0.0
&& result >= i64::MIN as f64
&& result < 9_223_372_036_854_775_808.0
&& (result.abs() <= 9_007_199_254_740_992.0 || result == i64::MIN as f64)
{
Ok((format!("{}", result as i64), "int".to_string()))
} else {
Ok((format!("{}", result), "float".to_string()))
}
}
fn check_result_value(v: f64) -> Result<f64, EvaluationError> {
if v.is_nan() || v.is_infinite() || v.abs() > MAX_RESULT_VALUE {
Err(EvaluationError::ValueOverflow)
} else {
Ok(v)
}
}
fn checked_i64(value: f64, message: &str) -> Result<i64, EvaluationError> {
if !value.is_finite()
|| value.fract() != 0.0
|| value < i64::MIN as f64
|| value >= 9_223_372_036_854_775_808.0
{
return Err(EvaluationError::InvalidOperation(message.to_string()));
}
Ok(value as i64)
}
fn median(args: &[f64]) -> Result<f64, EvaluationError> {
if args.is_empty() {
return Err(EvaluationError::InvalidOperation(
"median() requires at least one argument".to_string(),
));
}
if args.iter().any(|a| a.is_nan()) {
return Err(EvaluationError::InvalidOperation(
"median does not support NaN values".to_string(),
));
}
let mut sorted = args.to_vec();
sorted.sort_by(|a, b| a.total_cmp(b));
let mid = sorted.len() / 2;
if sorted.len().is_multiple_of(2) {
Ok((sorted[mid - 1] + sorted[mid]) / 2.0)
} else {
Ok(sorted[mid])
}
}
#[derive(Debug, Clone)]
enum Token {
Number(f64),
Plus,
Minus,
Multiply,
Divide,
FloorDiv,
Power,
Modulo,
BitXor,
BitAnd,
BitOr,
BitNot,
LShift,
RShift,
LeftParen,
RightParen,
Comma,
Identifier(String),
}
fn tokenize(expr: &str) -> Result<Vec<Token>, EvaluationError> {
let mut tokens = Vec::new();
let mut chars = expr.chars().peekable();
while let Some(ch) = chars.next() {
match ch {
' ' | '\t' | '\n' | '\r' => continue,
'0'..='9' => {
tokens.push(tokenize_number(ch, &mut chars)?);
}
'.' => {
if chars.peek().is_some_and(|c| c.is_ascii_digit()) {
tokens.push(tokenize_number(ch, &mut chars)?);
} else {
return Err(EvaluationError::ParseError(
"Unexpected character: .".to_string(),
));
}
}
'+' => tokens.push(Token::Plus),
'-' => tokens.push(Token::Minus),
'*' => {
if chars.peek() == Some(&'*') {
chars.next();
tokens.push(Token::Power);
} else {
tokens.push(Token::Multiply);
}
}
'/' => {
if chars.peek() == Some(&'/') {
chars.next();
tokens.push(Token::FloorDiv);
} else {
tokens.push(Token::Divide);
}
}
'%' => tokens.push(Token::Modulo),
'^' => tokens.push(Token::BitXor),
'&' => tokens.push(Token::BitAnd),
'|' => tokens.push(Token::BitOr),
'~' => tokens.push(Token::BitNot),
'<' => {
if chars.peek() == Some(&'<') {
chars.next();
tokens.push(Token::LShift);
} else {
return Err(EvaluationError::ParseError(
"Unexpected character: <".to_string(),
));
}
}
'>' => {
if chars.peek() == Some(&'>') {
chars.next();
tokens.push(Token::RShift);
} else {
return Err(EvaluationError::ParseError(
"Unexpected character: >".to_string(),
));
}
}
'(' => tokens.push(Token::LeftParen),
')' => tokens.push(Token::RightParen),
',' => tokens.push(Token::Comma),
'a'..='z' | 'A'..='Z' | '_' => {
let mut ident = ch.to_string();
while let Some(&c) = chars.peek() {
if c.is_alphanumeric() || c == '_' {
ident.push(chars.next().unwrap());
} else {
break;
}
}
tokens.push(Token::Identifier(ident));
}
_ => {
return Err(EvaluationError::ParseError(format!(
"Unexpected character: {}",
ch
)));
}
}
}
Ok(tokens)
}
fn tokenize_number(
first: char,
chars: &mut std::iter::Peekable<std::str::Chars<'_>>,
) -> Result<Token, EvaluationError> {
let mut s = String::new();
s.push(first);
if first == '0' {
if let Some(&c) = chars.peek() {
if c == 'x' || c == 'X' {
s.push(chars.next().unwrap());
while let Some(&c) = chars.peek() {
if c.is_ascii_hexdigit() || c == '_' {
if c != '_' {
s.push(c);
}
chars.next();
} else {
break;
}
}
let cleaned: String = s.chars().skip(2).collect();
let val = u64::from_str_radix(&cleaned, 16).map_err(|_| {
EvaluationError::ParseError(format!("Invalid hex literal: {}", s))
})?;
return Ok(Token::Number(val as f64));
}
if c == 'o' || c == 'O' {
s.push(chars.next().unwrap());
while let Some(&c) = chars.peek() {
if ('0'..='7').contains(&c) || c == '_' {
if c != '_' {
s.push(c);
}
chars.next();
} else {
break;
}
}
let cleaned: String = s.chars().skip(2).collect();
let val = u64::from_str_radix(&cleaned, 8).map_err(|_| {
EvaluationError::ParseError(format!("Invalid octal literal: {}", s))
})?;
return Ok(Token::Number(val as f64));
}
if c == 'b' || c == 'B' {
s.push(chars.next().unwrap());
while let Some(&c) = chars.peek() {
if c == '0' || c == '1' || c == '_' {
if c != '_' {
s.push(c);
}
chars.next();
} else {
break;
}
}
let cleaned: String = s.chars().skip(2).collect();
let val = u64::from_str_radix(&cleaned, 2).map_err(|_| {
EvaluationError::ParseError(format!("Invalid binary literal: {}", s))
})?;
return Ok(Token::Number(val as f64));
}
}
}
while let Some(&c) = chars.peek() {
if c.is_ascii_digit() || c == '_' {
if c != '_' {
s.push(c);
}
chars.next();
} else {
break;
}
}
if chars.peek() == Some(&'.') {
s.push(chars.next().unwrap());
while let Some(&c) = chars.peek() {
if c.is_ascii_digit() || c == '_' {
if c != '_' {
s.push(c);
}
chars.next();
} else {
break;
}
}
}
if let Some(&c) = chars.peek() {
if c == 'e' || c == 'E' {
s.push(chars.next().unwrap());
if let Some(&c) = chars.peek() {
if c == '+' || c == '-' {
s.push(chars.next().unwrap());
}
}
while let Some(&c) = chars.peek() {
if c.is_ascii_digit() || c == '_' {
if c != '_' {
s.push(c);
}
chars.next();
} else {
break;
}
}
}
}
let val: f64 = s
.parse()
.map_err(|_| EvaluationError::ParseError(format!("Invalid number: {}", s)))?;
Ok(Token::Number(val))
}
fn parse_expression(expr: &str, depth: &mut usize) -> Result<f64, EvaluationError> {
if *depth > MAX_NESTING_DEPTH {
return Err(EvaluationError::StackOverflow);
}
let tokens = tokenize(expr)?;
let mut pos = 0;
let result = parse_bit_or(&tokens, &mut pos, depth)?;
if pos < tokens.len() {
return Err(EvaluationError::ParseError(format!(
"Unexpected token after expression: {:?}",
tokens[pos]
)));
}
Ok(result)
}
fn parse_expression_with(
expr: &str,
depth: &mut usize,
ctx: &mut EvalContext,
) -> Result<f64, EvaluationError> {
if *depth > MAX_NESTING_DEPTH {
return Err(EvaluationError::StackOverflow);
}
let tokens = tokenize(expr)?;
let mut pos = 0;
let result = parse_bit_or_with(&tokens, &mut pos, depth, ctx)?;
if pos < tokens.len() {
return Err(EvaluationError::ParseError(format!(
"Unexpected token after expression: {:?}",
tokens[pos]
)));
}
Ok(result)
}
fn parse_bit_or(
tokens: &[Token],
pos: &mut usize,
depth: &mut usize,
) -> Result<f64, EvaluationError> {
let mut left = parse_bit_xor(tokens, pos, depth)?;
while *pos < tokens.len() {
if let Token::BitOr = &tokens[*pos] {
*pos += 1;
let right = parse_bit_xor(tokens, pos, depth)?;
let l = checked_i64(left, "Bitwise operations require integer arguments")?;
let r = checked_i64(right, "Bitwise operations require integer arguments")?;
left = (l | r) as f64;
} else {
break;
}
}
Ok(left)
}
fn parse_bit_or_with(
tokens: &[Token],
pos: &mut usize,
depth: &mut usize,
ctx: &mut EvalContext,
) -> Result<f64, EvaluationError> {
let mut left = parse_bit_xor_with(tokens, pos, depth, ctx)?;
while *pos < tokens.len() {
if let Token::BitOr = &tokens[*pos] {
*pos += 1;
let right = parse_bit_xor_with(tokens, pos, depth, ctx)?;
let l = checked_i64(left, "Bitwise operations require integer arguments")?;
let r = checked_i64(right, "Bitwise operations require integer arguments")?;
left = (l | r) as f64;
} else {
break;
}
}
Ok(left)
}
fn parse_bit_xor(
tokens: &[Token],
pos: &mut usize,
depth: &mut usize,
) -> Result<f64, EvaluationError> {
let mut left = parse_bit_and(tokens, pos, depth)?;
while *pos < tokens.len() {
if let Token::BitXor = &tokens[*pos] {
*pos += 1;
let right = parse_bit_and(tokens, pos, depth)?;
let l = checked_i64(left, "Bitwise operations require integer arguments")?;
let r = checked_i64(right, "Bitwise operations require integer arguments")?;
left = (l ^ r) as f64;
} else {
break;
}
}
Ok(left)
}
fn parse_bit_xor_with(
tokens: &[Token],
pos: &mut usize,
depth: &mut usize,
ctx: &mut EvalContext,
) -> Result<f64, EvaluationError> {
let mut left = parse_bit_and_with(tokens, pos, depth, ctx)?;
while *pos < tokens.len() {
if let Token::BitXor = &tokens[*pos] {
*pos += 1;
let right = parse_bit_and_with(tokens, pos, depth, ctx)?;
let l = checked_i64(left, "Bitwise operations require integer arguments")?;
let r = checked_i64(right, "Bitwise operations require integer arguments")?;
left = (l ^ r) as f64;
} else {
break;
}
}
Ok(left)
}
fn parse_bit_and(
tokens: &[Token],
pos: &mut usize,
depth: &mut usize,
) -> Result<f64, EvaluationError> {
let mut left = parse_shift(tokens, pos, depth)?;
while *pos < tokens.len() {
if let Token::BitAnd = &tokens[*pos] {
*pos += 1;
let right = parse_shift(tokens, pos, depth)?;
let l = checked_i64(left, "Bitwise operations require integer arguments")?;
let r = checked_i64(right, "Bitwise operations require integer arguments")?;
left = (l & r) as f64;
} else {
break;
}
}
Ok(left)
}
fn parse_bit_and_with(
tokens: &[Token],
pos: &mut usize,
depth: &mut usize,
ctx: &mut EvalContext,
) -> Result<f64, EvaluationError> {
let mut left = parse_shift_with(tokens, pos, depth, ctx)?;
while *pos < tokens.len() {
if let Token::BitAnd = &tokens[*pos] {
*pos += 1;
let right = parse_shift_with(tokens, pos, depth, ctx)?;
let l = checked_i64(left, "Bitwise operations require integer arguments")?;
let r = checked_i64(right, "Bitwise operations require integer arguments")?;
left = (l & r) as f64;
} else {
break;
}
}
Ok(left)
}
fn parse_shift(
tokens: &[Token],
pos: &mut usize,
depth: &mut usize,
) -> Result<f64, EvaluationError> {
let mut left = parse_additive(tokens, pos, depth)?;
while *pos < tokens.len() {
match &tokens[*pos] {
Token::LShift => {
*pos += 1;
let right = parse_additive(tokens, pos, depth)?;
let shift = checked_i64(right, "Bitwise operations require integer arguments")?;
if shift < 0 || shift as usize > MAX_SHIFT_COUNT {
return Err(EvaluationError::InvalidOperation(format!(
"Shift count {} out of range",
shift
)));
}
let l = checked_i64(left, "Bitwise operations require integer arguments")?;
left = l.checked_shl(shift as u32).ok_or_else(|| {
EvaluationError::InvalidOperation(format!("Shift left by {} overflows", shift))
})? as f64;
}
Token::RShift => {
*pos += 1;
let right = parse_additive(tokens, pos, depth)?;
let shift = checked_i64(right, "Bitwise operations require integer arguments")?;
if shift < 0 || shift as usize > MAX_SHIFT_COUNT {
return Err(EvaluationError::InvalidOperation(format!(
"Shift count {} out of range",
shift
)));
}
let l = checked_i64(left, "Bitwise operations require integer arguments")?;
left = l.checked_shr(shift as u32).ok_or_else(|| {
EvaluationError::InvalidOperation(format!("Shift right by {} overflows", shift))
})? as f64;
}
_ => break,
}
}
Ok(left)
}
fn parse_shift_with(
tokens: &[Token],
pos: &mut usize,
depth: &mut usize,
ctx: &mut EvalContext,
) -> Result<f64, EvaluationError> {
let mut left = parse_additive_with(tokens, pos, depth, ctx)?;
while *pos < tokens.len() {
match &tokens[*pos] {
Token::LShift => {
*pos += 1;
let right = parse_additive_with(tokens, pos, depth, ctx)?;
let shift = checked_i64(right, "Bitwise operations require integer arguments")?;
if shift < 0 || shift as usize > MAX_SHIFT_COUNT {
return Err(EvaluationError::InvalidOperation(format!(
"Shift count {} out of range",
shift
)));
}
let l = checked_i64(left, "Bitwise operations require integer arguments")?;
left = l.checked_shl(shift as u32).ok_or_else(|| {
EvaluationError::InvalidOperation(format!("Shift left by {} overflows", shift))
})? as f64;
}
Token::RShift => {
*pos += 1;
let right = parse_additive_with(tokens, pos, depth, ctx)?;
let shift = checked_i64(right, "Bitwise operations require integer arguments")?;
if shift < 0 || shift as usize > MAX_SHIFT_COUNT {
return Err(EvaluationError::InvalidOperation(format!(
"Shift count {} out of range",
shift
)));
}
let l = checked_i64(left, "Bitwise operations require integer arguments")?;
left = l.checked_shr(shift as u32).ok_or_else(|| {
EvaluationError::InvalidOperation(format!("Shift right by {} overflows", shift))
})? as f64;
}
_ => break,
}
}
Ok(left)
}
fn parse_additive(
tokens: &[Token],
pos: &mut usize,
depth: &mut usize,
) -> Result<f64, EvaluationError> {
let mut left = parse_multiplicative(tokens, pos, depth)?;
while *pos < tokens.len() {
match &tokens[*pos] {
Token::Plus => {
*pos += 1;
let right = parse_multiplicative(tokens, pos, depth)?;
left = check_result_value(left + right)?;
}
Token::Minus => {
*pos += 1;
let right = parse_multiplicative(tokens, pos, depth)?;
left = check_result_value(left - right)?;
}
_ => break,
}
}
Ok(left)
}
fn parse_additive_with(
tokens: &[Token],
pos: &mut usize,
depth: &mut usize,
ctx: &mut EvalContext,
) -> Result<f64, EvaluationError> {
let mut left = parse_multiplicative_with(tokens, pos, depth, ctx)?;
while *pos < tokens.len() {
match &tokens[*pos] {
Token::Plus => {
*pos += 1;
let right = parse_multiplicative_with(tokens, pos, depth, ctx)?;
left = check_result_value(left + right)?;
}
Token::Minus => {
*pos += 1;
let right = parse_multiplicative_with(tokens, pos, depth, ctx)?;
left = check_result_value(left - right)?;
}
_ => break,
}
}
Ok(left)
}
fn parse_multiplicative(
tokens: &[Token],
pos: &mut usize,
depth: &mut usize,
) -> Result<f64, EvaluationError> {
let mut left = parse_unary(tokens, pos, depth)?;
while *pos < tokens.len() {
match &tokens[*pos] {
Token::Multiply => {
*pos += 1;
let right = parse_unary(tokens, pos, depth)?;
left = check_result_value(left * right)?;
}
Token::Divide => {
*pos += 1;
let right = parse_unary(tokens, pos, depth)?;
if right == 0.0 {
return Err(EvaluationError::DivisionByZero);
}
left = check_result_value(left / right)?;
}
Token::FloorDiv => {
*pos += 1;
let right = parse_unary(tokens, pos, depth)?;
if right == 0.0 {
return Err(EvaluationError::DivisionByZero);
}
left = check_result_value((left / right).floor())?;
}
Token::Modulo => {
*pos += 1;
let right = parse_unary(tokens, pos, depth)?;
if right == 0.0 {
return Err(EvaluationError::DivisionByZero);
}
let mut result = left % right;
if result != 0.0 && (result < 0.0) != (right < 0.0) {
result += right;
}
left = check_result_value(result)?;
}
_ => break,
}
}
Ok(left)
}
fn parse_multiplicative_with(
tokens: &[Token],
pos: &mut usize,
depth: &mut usize,
ctx: &mut EvalContext,
) -> Result<f64, EvaluationError> {
let mut left = parse_unary_with(tokens, pos, depth, ctx)?;
while *pos < tokens.len() {
match &tokens[*pos] {
Token::Multiply => {
*pos += 1;
let right = parse_unary_with(tokens, pos, depth, ctx)?;
left = check_result_value(left * right)?;
}
Token::Divide => {
*pos += 1;
let right = parse_unary_with(tokens, pos, depth, ctx)?;
if right == 0.0 {
return Err(EvaluationError::DivisionByZero);
}
left = check_result_value(left / right)?;
}
Token::FloorDiv => {
*pos += 1;
let right = parse_unary_with(tokens, pos, depth, ctx)?;
if right == 0.0 {
return Err(EvaluationError::DivisionByZero);
}
left = check_result_value((left / right).floor())?;
}
Token::Modulo => {
*pos += 1;
let right = parse_unary_with(tokens, pos, depth, ctx)?;
if right == 0.0 {
return Err(EvaluationError::DivisionByZero);
}
let mut result = left % right;
if result != 0.0 && (result < 0.0) != (right < 0.0) {
result += right;
}
left = check_result_value(result)?;
}
_ => break,
}
}
Ok(left)
}
fn parse_power(
tokens: &[Token],
pos: &mut usize,
depth: &mut usize,
) -> Result<f64, EvaluationError> {
let base = parse_primary(tokens, pos, depth)?;
if *pos < tokens.len() {
if let Token::Power = &tokens[*pos] {
*pos += 1;
let exp = parse_unary(tokens, pos, depth)?;
if exp.abs() > MAX_EXPONENT {
return Err(EvaluationError::InvalidOperation(format!(
"Exponent {} out of range (max {})",
exp, MAX_EXPONENT
)));
}
let exp = if (exp - exp.round()).abs() < 1e-9 {
exp.round()
} else {
exp
};
if base < 0.0 && exp.fract() != 0.0 {
return Err(EvaluationError::InvalidOperation(
"Cannot raise negative number to non-integer power".to_string(),
));
}
if base == 0.0 && exp < 0.0 {
return Err(EvaluationError::InvalidOperation(
"Cannot raise zero to a negative power".to_string(),
));
}
if exp.fract() == 0.0 && base.fract() == 0.0 && exp.abs() < 1e15 && base.abs() < 1e15 {
let base_i = checked_i64(base, "Power operands must be i64 integers")?;
let exp_i = checked_i64(exp, "Power operands must be i64 integers")?;
if exp_i >= 0 && exp_i <= u32::MAX as i64 {
if let Some(result) = base_i.checked_pow(exp_i as u32) {
return Ok(result as f64);
}
}
}
return check_result_value(base.powf(exp));
}
}
Ok(base)
}
fn parse_power_with(
tokens: &[Token],
pos: &mut usize,
depth: &mut usize,
ctx: &mut EvalContext,
) -> Result<f64, EvaluationError> {
let base = parse_primary_with(tokens, pos, depth, ctx)?;
if *pos < tokens.len() {
if let Token::Power = &tokens[*pos] {
*pos += 1;
let exp = parse_unary_with(tokens, pos, depth, ctx)?;
if exp.abs() > MAX_EXPONENT {
return Err(EvaluationError::InvalidOperation(format!(
"Exponent {} out of range (max {})",
exp, MAX_EXPONENT
)));
}
let exp = if (exp - exp.round()).abs() < 1e-9 {
exp.round()
} else {
exp
};
if base < 0.0 && exp.fract() != 0.0 {
return Err(EvaluationError::InvalidOperation(
"Cannot raise negative number to non-integer power".to_string(),
));
}
if base == 0.0 && exp < 0.0 {
return Err(EvaluationError::InvalidOperation(
"Cannot raise zero to a negative power".to_string(),
));
}
if exp.fract() == 0.0 && base.fract() == 0.0 && exp.abs() < 1e15 && base.abs() < 1e15 {
let base_i = checked_i64(base, "Power operands must be i64 integers")?;
let exp_i = checked_i64(exp, "Power operands must be i64 integers")?;
if exp_i >= 0 && exp_i <= u32::MAX as i64 {
if let Some(result) = base_i.checked_pow(exp_i as u32) {
return Ok(result as f64);
}
}
}
return check_result_value(base.powf(exp));
}
}
Ok(base)
}
fn parse_unary(
tokens: &[Token],
pos: &mut usize,
depth: &mut usize,
) -> Result<f64, EvaluationError> {
if *pos >= tokens.len() {
return parse_power(tokens, pos, depth);
}
match &tokens[*pos] {
Token::Minus => {
*pos += 1;
let value = parse_power(tokens, pos, depth)?;
Ok(-value)
}
Token::Plus => {
*pos += 1;
parse_power(tokens, pos, depth)
}
Token::BitNot => {
*pos += 1;
let value = parse_unary(tokens, pos, depth)?;
let i = checked_i64(value, "Bitwise NOT requires integer argument")?;
Ok((!i) as f64)
}
_ => parse_power(tokens, pos, depth),
}
}
fn parse_unary_with(
tokens: &[Token],
pos: &mut usize,
depth: &mut usize,
ctx: &mut EvalContext,
) -> Result<f64, EvaluationError> {
if *pos >= tokens.len() {
return parse_power_with(tokens, pos, depth, ctx);
}
match &tokens[*pos] {
Token::Minus => {
*pos += 1;
let value = parse_power_with(tokens, pos, depth, ctx)?;
Ok(-value)
}
Token::Plus => {
*pos += 1;
parse_power_with(tokens, pos, depth, ctx)
}
Token::BitNot => {
*pos += 1;
let value = parse_unary_with(tokens, pos, depth, ctx)?;
let i = checked_i64(value, "Bitwise NOT requires integer argument")?;
Ok((!i) as f64)
}
_ => parse_power_with(tokens, pos, depth, ctx),
}
}
fn parse_primary(
tokens: &[Token],
pos: &mut usize,
depth: &mut usize,
) -> Result<f64, EvaluationError> {
if *pos >= tokens.len() {
return Err(EvaluationError::ParseError(
"Unexpected end of expression".to_string(),
));
}
match &tokens[*pos] {
Token::Number(n) => {
*pos += 1;
Ok(*n)
}
Token::Identifier(name) => {
*pos += 1;
parse_identifier(name, tokens, pos, depth)
}
Token::LeftParen => {
*pos += 1;
*depth += 1;
if *depth > MAX_NESTING_DEPTH {
return Err(EvaluationError::StackOverflow);
}
let value = parse_bit_or(tokens, pos, depth)?;
*depth -= 1;
match tokens.get(*pos) {
Some(Token::RightParen) => {
*pos += 1;
}
_ => {
return Err(EvaluationError::ParseError(
"Missing closing parenthesis".into(),
))
}
}
Ok(value)
}
_ => Err(EvaluationError::ParseError("Unexpected token".to_string())),
}
}
fn parse_primary_with(
tokens: &[Token],
pos: &mut usize,
depth: &mut usize,
ctx: &mut EvalContext,
) -> Result<f64, EvaluationError> {
if *pos >= tokens.len() {
return Err(EvaluationError::ParseError(
"Unexpected end of expression".to_string(),
));
}
match &tokens[*pos] {
Token::Number(n) => {
*pos += 1;
Ok(*n)
}
Token::Identifier(name) => {
*pos += 1;
parse_identifier_with(name, tokens, pos, depth, ctx)
}
Token::LeftParen => {
*pos += 1;
*depth += 1;
if *depth > MAX_NESTING_DEPTH {
return Err(EvaluationError::StackOverflow);
}
let value = parse_bit_or_with(tokens, pos, depth, ctx)?;
*depth -= 1;
match tokens.get(*pos) {
Some(Token::RightParen) => {
*pos += 1;
}
_ => {
return Err(EvaluationError::ParseError(
"Missing closing parenthesis".into(),
))
}
}
Ok(value)
}
_ => Err(EvaluationError::ParseError("Unexpected token".to_string())),
}
}
fn parse_identifier(
name: &str,
tokens: &[Token],
pos: &mut usize,
depth: &mut usize,
) -> Result<f64, EvaluationError> {
if *pos < tokens.len() {
if let Token::LeftParen = &tokens[*pos] {
*pos += 1;
return evaluate_function(name, tokens, pos, depth);
}
}
if crate::calc::units::UNIT_ALIASES.get(name).is_some() {
return Ok(1.0);
}
if let Some(&value) = CONSTANTS.get(name) {
return Ok(value);
}
let lower = name.to_lowercase();
if let Some(&value) = CONSTANTS.get(lower.as_str()) {
return Ok(value);
}
Err(EvaluationError::UnknownConstant(name.to_string()))
}
fn parse_identifier_with(
name: &str,
tokens: &[Token],
pos: &mut usize,
depth: &mut usize,
ctx: &mut EvalContext,
) -> Result<f64, EvaluationError> {
if *pos < tokens.len() {
if let Token::LeftParen = &tokens[*pos] {
*pos += 1;
return evaluate_function_with(name, tokens, pos, depth, ctx);
}
}
if crate::calc::units::UNIT_ALIASES.get(name).is_some() {
return Ok(1.0);
}
if let Some(&value) = CONSTANTS.get(name) {
return Ok(value);
}
let lower = name.to_lowercase();
if let Some(&value) = CONSTANTS.get(lower.as_str()) {
return Ok(value);
}
Err(EvaluationError::UnknownConstant(name.to_string()))
}
fn parse_args(
tokens: &[Token],
pos: &mut usize,
depth: &mut usize,
) -> Result<Vec<f64>, EvaluationError> {
let mut args = Vec::new();
let mut closed = false;
while *pos < tokens.len() {
match &tokens[*pos] {
Token::RightParen => {
*pos += 1;
closed = true;
break;
}
Token::Comma => {
*pos += 1;
}
_ => {
args.push(parse_bit_or(tokens, pos, depth)?);
}
}
}
if !closed {
return Err(EvaluationError::InvalidOperation(
"Missing closing parenthesis".to_string(),
));
}
Ok(args)
}
fn parse_args_with(
tokens: &[Token],
pos: &mut usize,
depth: &mut usize,
ctx: &mut EvalContext,
) -> Result<Vec<f64>, EvaluationError> {
let mut args = Vec::new();
let mut closed = false;
while *pos < tokens.len() {
match &tokens[*pos] {
Token::RightParen => {
*pos += 1;
closed = true;
break;
}
Token::Comma => {
*pos += 1;
}
_ => {
args.push(parse_bit_or_with(tokens, pos, depth, ctx)?);
}
}
}
if !closed {
return Err(EvaluationError::InvalidOperation(
"Missing closing parenthesis".to_string(),
));
}
Ok(args)
}
fn banker_round(x: f64) -> f64 {
x.round_ties_even()
}
fn evaluate_function(
name: &str,
tokens: &[Token],
pos: &mut usize,
depth: &mut usize,
) -> Result<f64, EvaluationError> {
let args = parse_args(tokens, pos, depth)?;
let n = name.to_lowercase();
check_function_allowed(name)?;
match n.as_str() {
"sin" if args.len() == 1 => Ok(args[0].sin()),
"cos" if args.len() == 1 => Ok(args[0].cos()),
"tan" if args.len() == 1 => Ok(args[0].tan()),
"asin" if args.len() == 1 => Ok(args[0].asin()),
"acos" if args.len() == 1 => Ok(args[0].acos()),
"atan" if args.len() == 1 => Ok(args[0].atan()),
"atan2" if args.len() == 2 => Ok(args[0].atan2(args[1])),
"sinh" if args.len() == 1 => Ok(args[0].sinh()),
"cosh" if args.len() == 1 => Ok(args[0].cosh()),
"tanh" if args.len() == 1 => Ok(args[0].tanh()),
"asinh" if args.len() == 1 => Ok(args[0].asinh()),
"acosh" if args.len() == 1 => Ok(args[0].acosh()),
"atanh" if args.len() == 1 => Ok(args[0].atanh()),
"log" | "ln" if args.len() == 1 => {
if args[0] <= 0.0 {
return Err(EvaluationError::InvalidOperation(
"logarithm of non-positive number".to_string(),
));
}
Ok(args[0].ln())
}
"log" | "ln" if args.len() == 2 => {
if args[0] <= 0.0 {
return Err(EvaluationError::InvalidOperation(
"logarithm of non-positive number".to_string(),
));
}
if args[1] <= 0.0 || args[1] == 1.0 {
return Err(EvaluationError::InvalidOperation(
"log base must be positive and not 1".to_string(),
));
}
Ok(args[0].ln() / args[1].ln())
}
"log10" if args.len() == 1 => {
if args[0] <= 0.0 {
return Err(EvaluationError::InvalidOperation(
"logarithm of non-positive number".to_string(),
));
}
Ok(args[0].log10())
}
"log2" if args.len() == 1 => {
if args[0] <= 0.0 {
return Err(EvaluationError::InvalidOperation(
"logarithm of non-positive number".to_string(),
));
}
Ok(args[0].log2())
}
"log1p" if args.len() == 1 => Ok(args[0].ln_1p()),
"exp" if args.len() == 1 => {
let result = args[0].exp();
if result.is_infinite() || result.abs() > 1e308 {
return Err(EvaluationError::InvalidOperation(
"Result too large".to_string(),
));
}
Ok(result)
}
"expm1" if args.len() == 1 => Ok(args[0].exp_m1()),
"sqrt" if args.len() == 1 => {
if args[0] < 0.0 {
return Err(EvaluationError::InvalidOperation(
"square root of negative number".to_string(),
));
}
Ok(args[0].sqrt())
}
"cbrt" if args.len() == 1 => Ok(args[0].cbrt()),
"pow" | "power" if args.len() == 2 => {
if args[1].abs() > MAX_EXPONENT {
return Err(EvaluationError::InvalidOperation(format!(
"Exponent {} out of range",
args[1]
)));
}
if args[0] < 0.0 && args[1].fract() != 0.0 {
return Err(EvaluationError::InvalidOperation(
"Cannot raise negative number to non-integer power".to_string(),
));
}
if args[0] == 0.0 && args[1] < 0.0 {
return Err(EvaluationError::InvalidOperation(
"Cannot raise zero to a negative power".to_string(),
));
}
check_result_value(args[0].powf(args[1]))
}
"abs" if args.len() == 1 => Ok(args[0].abs()),
"floor" if args.len() == 1 => Ok(args[0].floor()),
"ceil" if args.len() == 1 => Ok(args[0].ceil()),
"round" if args.len() == 1 => Ok(banker_round(args[0])),
"round" if args.len() == 2 => {
if !args[1].is_finite() || args[1].fract() != 0.0 {
return Err(EvaluationError::InvalidOperation(
"ndigits must be a finite integer".to_string(),
));
}
let ndigits = args[1] as i32;
if !(-308..=308).contains(&ndigits) {
return Err(EvaluationError::InvalidOperation(format!(
"ndigits {} out of range (-308..=308)",
ndigits
)));
}
let factor = 10.0_f64.powi(ndigits);
if !factor.is_finite() || factor == 0.0 {
return Err(EvaluationError::InvalidOperation(format!(
"ndigits {} produces an unusable rounding factor",
ndigits
)));
}
let scaled = check_result_value(args[0] * factor)?;
check_result_value(banker_round(scaled) / factor)
}
"trunc" if args.len() == 1 => Ok(args[0].trunc()),
"sign" if args.len() == 1 => Ok(args[0].signum()),
"degrees" if args.len() == 1 => Ok(args[0].to_degrees()),
"radians" if args.len() == 1 => Ok(args[0].to_radians()),
"hypot" if !args.is_empty() => {
let max_val = args.iter().map(|x| x.abs()).fold(0.0, f64::max);
if max_val == 0.0 {
return Ok(0.0);
}
let sum_sq: f64 = args.iter().map(|x| (x / max_val).powi(2)).sum();
Ok(max_val * sum_sq.sqrt())
}
"factorial" | "fact" if args.len() == 1 => {
let n = checked_i64(args[0], "factorial requires integer argument")?;
if !(0..=MAX_FACTORIAL).contains(&n) {
return Err(EvaluationError::InvalidOperation(format!(
"factorial({}) out of range (0..={})",
args[0], MAX_FACTORIAL
)));
}
let s = factorial_bigint(n as u64);
Err(EvaluationError::InvalidOperation(format!(
"__int_result__{}",
s
)))
}
"perm" if args.len() == 1 => {
let n = checked_i64(args[0], "perm requires integer argument")?;
if !(0..=MAX_FACTORIAL).contains(&n) {
return Err(EvaluationError::InvalidOperation(format!(
"perm({}) out of range (0..={})",
args[0], MAX_FACTORIAL
)));
}
let s = factorial_bigint(n as u64);
Err(EvaluationError::InvalidOperation(format!(
"__int_result__{}",
s
)))
}
"perm" | "npr" if args.len() == 2 => {
let n = checked_i64(args[0], "perm requires integer arguments")?;
let r = checked_i64(args[1], "perm requires integer arguments")?;
if n < 0 || r < 0 || n > MAX_PERM_COMB || r > MAX_PERM_COMB {
return Err(EvaluationError::InvalidOperation(format!(
"perm({}, {}) out of range",
n, r
)));
}
if r > n {
return Ok(0.0);
}
let mut limbs: Vec<u64> = vec![1];
for i in 0..r as u64 {
multiply_in_place(&mut limbs, n as u64 - i);
}
bigint_or_float(limbs)
}
"comb" | "ncr" if args.len() == 2 => {
let n = checked_i64(args[0], "comb requires integer arguments")?;
let r = checked_i64(args[1], "comb requires integer arguments")?;
if n < 0 || r < 0 || n > MAX_PERM_COMB || r > MAX_PERM_COMB {
return Err(EvaluationError::InvalidOperation(format!(
"comb({}, {}) out of range",
n, r
)));
}
if r > n {
return Ok(0.0);
}
let r_small = r.min(n - r);
let mut limbs: Vec<u64> = vec![1];
for i in 0..r_small as u64 {
multiply_in_place(&mut limbs, n as u64 - i);
divide_in_place(&mut limbs, i + 1);
}
bigint_or_float(limbs)
}
"gcd" if !args.is_empty() => {
for a in &args {
if *a != a.floor() || !a.is_finite() {
return Err(EvaluationError::InvalidOperation(
"gcd() requires integer arguments".to_string(),
));
}
}
let ints: Vec<i64> = args
.iter()
.map(|a| checked_i64(*a, "gcd() requires integer arguments"))
.collect::<Result<_, _>>()?;
if ints.contains(&i64::MIN) {
return Err(EvaluationError::ValueOverflow);
}
let mut result = ints[0].abs();
for &v in &ints[1..] {
result = gcd(result, v.abs());
}
Ok(result as f64)
}
"lcm" if !args.is_empty() => {
for a in &args {
if *a != a.floor() || !a.is_finite() {
return Err(EvaluationError::InvalidOperation(
"lcm() requires integer arguments".to_string(),
));
}
}
let ints: Vec<i64> = args
.iter()
.map(|a| checked_i64(*a, "lcm() requires integer arguments"))
.collect::<Result<_, _>>()?;
if ints.contains(&i64::MIN) {
return Err(EvaluationError::ValueOverflow);
}
let mut result = ints[0].abs();
for &v in &ints[1..] {
let g = gcd(result, v.abs());
if g == 0 {
result = 0;
} else {
result = (result / g)
.abs()
.checked_mul(v.abs())
.ok_or(EvaluationError::ValueOverflow)?;
}
}
Ok(result as f64)
}
"max" if !args.is_empty() => Ok(args.iter().cloned().fold(f64::NEG_INFINITY, f64::max)),
"min" if !args.is_empty() => Ok(args.iter().cloned().fold(f64::INFINITY, f64::min)),
"sum" if args.is_empty() => Ok(0.0),
"sum" => {
let s: f64 = args.iter().sum();
check_result_value(s)
}
"mean" | "average" if !args.is_empty() => {
let s: f64 = args.iter().sum();
check_result_value(s / args.len() as f64)
}
"median" if !args.is_empty() => median(&args),
"mode" if !args.is_empty() => {
fn normalize_zero_bits(v: f64) -> u64 {
if v == 0.0 {
0.0_f64.to_bits()
} else {
v.to_bits()
}
}
let mut counts: HashMap<u64, usize> = HashMap::new();
for &a in &args {
let key = normalize_zero_bits(a);
*counts.entry(key).or_insert(0) += 1;
}
let max_count = counts.values().max().unwrap_or(&0);
for &a in &args {
if counts.get(&normalize_zero_bits(a)).unwrap_or(&0) == max_count {
return Ok(a);
}
}
Ok(args[0])
}
"product" if !args.is_empty() => {
let p: f64 = args.iter().fold(1.0, |acc, x| acc * x);
check_result_value(p)
}
"std" | "stddev" if args.len() >= 2 => {
let mean: f64 = args.iter().sum::<f64>() / args.len() as f64;
let variance: f64 =
args.iter().map(|x| (x - mean).powi(2)).sum::<f64>() / args.len() as f64;
Ok(variance.sqrt())
}
"std" | "stddev" if args.len() == 1 => Err(EvaluationError::InvalidOperation(
"std requires at least two arguments".to_string(),
)),
"std_sample" | "stds" if args.len() >= 2 => {
let mean: f64 = args.iter().sum::<f64>() / args.len() as f64;
let variance: f64 =
args.iter().map(|x| (x - mean).powi(2)).sum::<f64>() / (args.len() - 1) as f64;
Ok(variance.sqrt())
}
"variance" | "var" if args.len() >= 2 => {
let mean: f64 = args.iter().sum::<f64>() / args.len() as f64;
let variance: f64 =
args.iter().map(|x| (x - mean).powi(2)).sum::<f64>() / args.len() as f64;
Ok(variance)
}
"variance" | "var" if args.len() == 1 => Err(EvaluationError::InvalidOperation(
"variance requires at least two arguments".to_string(),
)),
"variance_sample" | "vars" | "var_sample" if args.len() >= 2 => {
let mean: f64 = args.iter().sum::<f64>() / args.len() as f64;
let variance: f64 =
args.iter().map(|x| (x - mean).powi(2)).sum::<f64>() / (args.len() - 1) as f64;
Ok(variance)
}
"percentof" | "percent_of" if args.len() == 2 => Ok(args[0] / 100.0 * args[1]),
"aspercent" | "as_percent" if args.len() == 2 => {
if args[1] == 0.0 {
return Err(EvaluationError::DivisionByZero);
}
if args[1].abs() < 1e-100 {
return Err(EvaluationError::InvalidOperation(
"aspercent: near-zero divisor could cause overflow".to_string(),
));
}
Ok(args[0] / args[1] * 100.0)
}
"clamp" if args.len() == 3 => {
if args[1] > args[2] {
return Err(EvaluationError::InvalidOperation(format!(
"clamp: lo ({}) > hi ({})",
args[1], args[2]
)));
}
Ok(args[0].max(args[1]).min(args[2]))
}
"bitand" if args.len() == 2 => {
let left = checked_i64(args[0], "bitand requires integer arguments")?;
let right = checked_i64(args[1], "bitand requires integer arguments")?;
Ok((left & right) as f64)
}
"bitor" if args.len() == 2 => {
let left = checked_i64(args[0], "bitor requires integer arguments")?;
let right = checked_i64(args[1], "bitor requires integer arguments")?;
Ok((left | right) as f64)
}
"bitxor" if args.len() == 2 => {
let left = checked_i64(args[0], "bitxor requires integer arguments")?;
let right = checked_i64(args[1], "bitxor requires integer arguments")?;
Ok((left ^ right) as f64)
}
"bitnot" if args.len() == 1 => {
let value = checked_i64(args[0], "bitnot requires integer argument")?;
Ok((!value) as f64)
}
"bitlshift" if args.len() == 2 => {
let value = checked_i64(args[0], "bitlshift requires integer arguments")?;
let shift = checked_i64(args[1], "bitlshift requires integer arguments")?;
if shift < 0 || shift as usize > MAX_SHIFT_COUNT {
return Err(EvaluationError::InvalidOperation(format!(
"Shift count {} out of range",
shift
)));
}
Ok((value.checked_shl(shift as u32).ok_or_else(|| {
EvaluationError::InvalidOperation(format!("Shift left by {} overflows", shift))
})?) as f64)
}
"bitrshift" if args.len() == 2 => {
let value = checked_i64(args[0], "bitrshift requires integer arguments")?;
let shift = checked_i64(args[1], "bitrshift requires integer arguments")?;
if shift < 0 || shift as usize > MAX_SHIFT_COUNT {
return Err(EvaluationError::InvalidOperation(format!(
"Shift count {} out of range",
shift
)));
}
Ok((value.checked_shr(shift as u32).ok_or_else(|| {
EvaluationError::InvalidOperation(format!("Shift right by {} overflows", shift))
})?) as f64)
}
"bin" if args.len() == 1 => {
let n = checked_i64(args[0], "bin requires integer argument")?;
let s = if n < 0 {
format!("-0b{:b}", n.unsigned_abs())
} else {
format!("0b{:b}", n)
};
Err(EvaluationError::InvalidOperation(format!(
"__string_result__{}",
s
)))
}
"hex" if args.len() == 1 => {
let n = checked_i64(args[0], "hex requires integer argument")?;
let s = if n < 0 {
format!("-0x{:x}", n.unsigned_abs())
} else {
format!("0x{:x}", n)
};
Err(EvaluationError::InvalidOperation(format!(
"__string_result__{}",
s
)))
}
"oct" if args.len() == 1 => {
let n = checked_i64(args[0], "oct requires integer argument")?;
let s = if n < 0 {
format!("-0o{:o}", n.unsigned_abs())
} else {
format!("0o{:o}", n)
};
Err(EvaluationError::InvalidOperation(format!(
"__string_result__{}",
s
)))
}
"isprime" | "is_prime" if args.len() == 1 => {
let n = checked_i64(args[0], "isprime requires integer argument")?;
if n > MAX_PRIME {
return Err(EvaluationError::InvalidOperation(format!(
"isprime({}) out of range",
args[0]
)));
}
Ok(if n < 2 {
0.0
} else if is_prime(n) {
1.0
} else {
0.0
})
}
"nextprime" | "next_prime" if args.len() == 1 => {
let n = checked_i64(args[0], "nextprime requires integer argument")?;
if !(0..=MAX_PRIME).contains(&n) {
return Err(EvaluationError::InvalidOperation(format!(
"nextprime({}) out of range",
args[0]
)));
}
Ok(next_prime(n)? as f64)
}
"prevprime" | "prev_prime" if args.len() == 1 => {
let n = checked_i64(args[0], "prevprime requires integer argument")?;
if n <= 2 || n > MAX_PRIME {
return Err(EvaluationError::InvalidOperation(format!(
"prevprime({}) out of range",
args[0]
)));
}
Ok(prev_prime(n)? as f64)
}
"primefactors" | "prime_factors" if args.len() == 1 => {
let n = checked_i64(args[0], "primefactors requires integer argument")?;
if n < 0 {
return Err(EvaluationError::InvalidOperation(format!(
"primefactors({}) out of range",
args[0]
)));
}
if n > 1_000_000_000_000 {
return Err(EvaluationError::InvalidOperation(
"factorization not available for numbers > 10^12".to_string(),
));
}
let s = prime_factors_string(n);
Err(EvaluationError::InvalidOperation(format!(
"__string_result__{}",
s
)))
}
"real" if args.len() == 1 => Ok(args[0]),
"imag" | "imaginary" if args.len() == 1 => Ok(0.0),
"conj" | "conjugate" if args.len() == 1 => Ok(args[0]),
"phase" | "arg" | "argument" if args.len() == 1 => Ok(if args[0] >= 0.0 {
0.0
} else {
std::f64::consts::PI
}),
"polar" if args.len() == 1 => {
let r = args[0].abs();
let phi = if args[0] >= 0.0 {
0.0
} else {
std::f64::consts::PI
};
let s = format!("({}, {})", r, phi);
Err(EvaluationError::InvalidOperation(format!(
"__string_result__{}",
s
)))
}
"polar" if args.len() == 2 => {
let s = format!("({}, {})", args[0], args[1]);
Err(EvaluationError::InvalidOperation(format!(
"__string_result__{}",
s
)))
}
"rect" if args.len() == 2 => {
let r = args[0];
let phi = args[1];
let real = r * phi.cos();
let imag = r * phi.sin();
let s = format!("({}, {})", real, imag);
Err(EvaluationError::InvalidOperation(format!(
"__string_result__{}",
s
)))
}
"random" | "rand" if args.is_empty() => Ok(prng_random()),
"randint" if args.len() == 2 => {
let a = checked_i64(args[0], "randint requires integer arguments")?;
let b = checked_i64(args[1], "randint requires integer arguments")?;
if a > b {
return Err(EvaluationError::InvalidOperation(format!(
"randint: lower bound ({}) > upper bound ({})",
a, b
)));
}
Ok(random_int_inclusive(a, b, prng_next_u64) as f64)
}
"randrange" if args.len() == 1 => {
let a = checked_i64(args[0], "randrange requires integer argument")?;
if a <= 0 {
return Err(EvaluationError::InvalidOperation(
"randrange: argument must be positive".to_string(),
));
}
Ok(random_int_inclusive(0, a - 1, prng_next_u64) as f64)
}
"randrange" if args.len() == 2 => {
let a = checked_i64(args[0], "randrange requires integer arguments")?;
let b = checked_i64(args[1], "randrange requires integer arguments")?;
if a >= b {
return Err(EvaluationError::InvalidOperation(
"randrange: start must be less than stop".to_string(),
));
}
Ok(random_int_inclusive(a, b - 1, prng_next_u64) as f64)
}
"uniform" if args.len() == 2 => {
let a = args[0];
let b = args[1];
if a > b {
return Err(EvaluationError::InvalidOperation(format!(
"uniform: lower bound ({}) > upper bound ({})",
a, b
)));
}
Ok(a + prng_random() * (b - a))
}
"randn" if args.is_empty() => Ok(prng_randn()),
"gauss" | "normal" if args.len() == 2 => {
let mu = args[0];
let sigma = args[1];
Ok(mu + sigma * prng_randn())
}
"seed" if args.is_empty() => {
let mut prng = PRNG_STATE.lock().unwrap_or_else(|e| e.into_inner());
prng.0 = 123456789;
prng.1 = None;
Ok(0.0)
}
"seed" if args.len() == 1 => {
if !args[0].is_finite() {
return Err(EvaluationError::InvalidOperation(
"seed requires a finite number".to_string(),
));
}
let s = args[0].trunc() as i64 as u64;
prng_seed(s);
Ok(0.0)
}
"store" if args.len() == 1 => {
let mut regs = MEMORY_REGISTERS.lock().unwrap_or_else(|e| e.into_inner());
regs.insert("M".to_string(), args[0]);
Ok(args[0])
}
"store" if args.len() == 2 => {
let id = checked_i64(args[1], "register index requires integer argument")?;
let name = format!("R{}", id);
let mut regs = MEMORY_REGISTERS.lock().unwrap_or_else(|e| e.into_inner());
regs.insert(name, args[0]);
Ok(args[0])
}
"recall" if args.is_empty() => {
let regs = MEMORY_REGISTERS.lock().unwrap_or_else(|e| e.into_inner());
Ok(*regs.get("M").unwrap_or(&0.0))
}
"recall" if args.len() == 1 => {
let id = checked_i64(args[0], "register index requires integer argument")?;
let name = format!("R{}", id);
let regs = MEMORY_REGISTERS.lock().unwrap_or_else(|e| e.into_inner());
Ok(*regs.get(&name).unwrap_or(&0.0))
}
"mplus" | "m+" | "madd" if args.len() == 1 => {
let mut regs = MEMORY_REGISTERS.lock().unwrap_or_else(|e| e.into_inner());
let current = *regs.get("M").unwrap_or(&0.0);
let new_val = current + args[0];
regs.insert("M".to_string(), new_val);
Ok(new_val)
}
"mminus" | "m-" | "msub" if args.len() == 1 => {
let mut regs = MEMORY_REGISTERS.lock().unwrap_or_else(|e| e.into_inner());
let current = *regs.get("M").unwrap_or(&0.0);
let new_val = current - args[0];
regs.insert("M".to_string(), new_val);
Ok(new_val)
}
"mc" | "mclear" if args.is_empty() => {
let mut regs = MEMORY_REGISTERS.lock().unwrap_or_else(|e| e.into_inner());
regs.clear();
Ok(0.0)
}
"mr" | "mrecall" if args.is_empty() => {
let regs = MEMORY_REGISTERS.lock().unwrap_or_else(|e| e.into_inner());
Ok(*regs.get("M").unwrap_or(&0.0))
}
"setvar" if args.len() == 2 => {
let var_id = checked_i64(args[1], "variable id requires integer argument")?;
let key = format!("v{}", var_id);
let mut vars = USER_VARIABLES.lock().unwrap_or_else(|e| e.into_inner());
if !vars.contains_key(&key) && vars.len() >= MAX_USER_VARIABLES {
if let Some(oldest) = vars.keys().min().cloned() {
vars.remove(&oldest);
}
}
vars.insert(key, args[0]);
Ok(args[0])
}
"getvar" if args.len() == 1 => {
let var_id = checked_i64(args[0], "variable id requires integer argument")?;
let key = format!("v{}", var_id);
let vars = USER_VARIABLES.lock().unwrap_or_else(|e| e.into_inner());
Ok(*vars.get(&key).unwrap_or(&0.0))
}
"getvar" if args.len() == 2 => {
let var_id = checked_i64(args[0], "variable id requires integer argument")?;
let key = format!("v{}", var_id);
let vars = USER_VARIABLES.lock().unwrap_or_else(|e| e.into_inner());
Ok(*vars.get(&key).unwrap_or(&args[1]))
}
"delvar" if args.len() == 1 => {
let var_id = checked_i64(args[0], "variable id requires integer argument")?;
let key = format!("v{}", var_id);
let mut vars = USER_VARIABLES.lock().unwrap_or_else(|e| e.into_inner());
vars.remove(&key);
Ok(0.0)
}
"listvars" if args.is_empty() => {
let vars = USER_VARIABLES.lock().unwrap_or_else(|e| e.into_inner());
let s = if vars.is_empty() {
"{}".to_string()
} else {
let entries: Vec<String> =
vars.iter().map(|(k, v)| format!("{}: {}", k, v)).collect();
format!("{{{}}}", entries.join(", "))
};
Err(EvaluationError::InvalidOperation(format!(
"__string_result__{}",
s
)))
}
"clearvars" if args.is_empty() => {
let mut vars = USER_VARIABLES.lock().unwrap_or_else(|e| e.into_inner());
vars.clear();
Ok(0.0)
}
"convert" => Err(EvaluationError::InvalidOperation(
"convert() must be called through the run() pipeline, not evaluate() directly"
.to_string(),
)),
"temp" => Err(EvaluationError::InvalidOperation(
"temp() must be called through the run() pipeline, not evaluate() directly".to_string(),
)),
_ => Err(EvaluationError::UnknownFunction(format!(
"{}({} args)",
name,
args.len()
))),
}
}
fn evaluate_function_with(
name: &str,
tokens: &[Token],
pos: &mut usize,
depth: &mut usize,
ctx: &mut EvalContext,
) -> Result<f64, EvaluationError> {
let args = parse_args_with(tokens, pos, depth, ctx)?;
let n = name.to_lowercase();
check_function_allowed_with(name, ctx)?;
match n.as_str() {
"sin" if args.len() == 1 => Ok(args[0].sin()),
"cos" if args.len() == 1 => Ok(args[0].cos()),
"tan" if args.len() == 1 => Ok(args[0].tan()),
"asin" if args.len() == 1 => Ok(args[0].asin()),
"acos" if args.len() == 1 => Ok(args[0].acos()),
"atan" if args.len() == 1 => Ok(args[0].atan()),
"atan2" if args.len() == 2 => Ok(args[0].atan2(args[1])),
"sinh" if args.len() == 1 => Ok(args[0].sinh()),
"cosh" if args.len() == 1 => Ok(args[0].cosh()),
"tanh" if args.len() == 1 => Ok(args[0].tanh()),
"asinh" if args.len() == 1 => Ok(args[0].asinh()),
"acosh" if args.len() == 1 => Ok(args[0].acosh()),
"atanh" if args.len() == 1 => Ok(args[0].atanh()),
"log" | "ln" if args.len() == 1 => {
if args[0] <= 0.0 {
return Err(EvaluationError::InvalidOperation(
"logarithm of non-positive number".to_string(),
));
}
Ok(args[0].ln())
}
"log" | "ln" if args.len() == 2 => {
if args[0] <= 0.0 {
return Err(EvaluationError::InvalidOperation(
"logarithm of non-positive number".to_string(),
));
}
if args[1] <= 0.0 || args[1] == 1.0 {
return Err(EvaluationError::InvalidOperation(
"log base must be positive and not 1".to_string(),
));
}
Ok(args[0].ln() / args[1].ln())
}
"log10" if args.len() == 1 => {
if args[0] <= 0.0 {
return Err(EvaluationError::InvalidOperation(
"logarithm of non-positive number".to_string(),
));
}
Ok(args[0].log10())
}
"log2" if args.len() == 1 => {
if args[0] <= 0.0 {
return Err(EvaluationError::InvalidOperation(
"logarithm of non-positive number".to_string(),
));
}
Ok(args[0].log2())
}
"log1p" if args.len() == 1 => Ok(args[0].ln_1p()),
"exp" if args.len() == 1 => {
let result = args[0].exp();
if result.is_infinite() || result.abs() > 1e308 {
return Err(EvaluationError::InvalidOperation(
"Result too large".to_string(),
));
}
Ok(result)
}
"expm1" if args.len() == 1 => Ok(args[0].exp_m1()),
"sqrt" if args.len() == 1 => {
if args[0] < 0.0 {
return Err(EvaluationError::InvalidOperation(
"square root of negative number".to_string(),
));
}
Ok(args[0].sqrt())
}
"cbrt" if args.len() == 1 => Ok(args[0].cbrt()),
"pow" | "power" if args.len() == 2 => {
if args[1].abs() > MAX_EXPONENT {
return Err(EvaluationError::InvalidOperation(format!(
"Exponent {} out of range",
args[1]
)));
}
if args[0] < 0.0 && args[1].fract() != 0.0 {
return Err(EvaluationError::InvalidOperation(
"Cannot raise negative number to non-integer power".to_string(),
));
}
if args[0] == 0.0 && args[1] < 0.0 {
return Err(EvaluationError::InvalidOperation(
"Cannot raise zero to a negative power".to_string(),
));
}
check_result_value(args[0].powf(args[1]))
}
"abs" if args.len() == 1 => Ok(args[0].abs()),
"floor" if args.len() == 1 => Ok(args[0].floor()),
"ceil" if args.len() == 1 => Ok(args[0].ceil()),
"round" if args.len() == 1 => Ok(banker_round(args[0])),
"round" if args.len() == 2 => {
if !args[1].is_finite() || args[1].fract() != 0.0 {
return Err(EvaluationError::InvalidOperation(
"ndigits must be a finite integer".to_string(),
));
}
let ndigits = args[1] as i32;
if !(-308..=308).contains(&ndigits) {
return Err(EvaluationError::InvalidOperation(format!(
"ndigits {} out of range (-308..=308)",
ndigits
)));
}
let factor = 10.0_f64.powi(ndigits);
if !factor.is_finite() || factor == 0.0 {
return Err(EvaluationError::InvalidOperation(format!(
"ndigits {} produces an unusable rounding factor",
ndigits
)));
}
let scaled = check_result_value(args[0] * factor)?;
check_result_value(banker_round(scaled) / factor)
}
"trunc" if args.len() == 1 => Ok(args[0].trunc()),
"sign" if args.len() == 1 => Ok(args[0].signum()),
"degrees" if args.len() == 1 => Ok(args[0].to_degrees()),
"radians" if args.len() == 1 => Ok(args[0].to_radians()),
"hypot" if !args.is_empty() => {
let max_val = args.iter().map(|x| x.abs()).fold(0.0, f64::max);
if max_val == 0.0 {
return Ok(0.0);
}
let sum_sq: f64 = args.iter().map(|x| (x / max_val).powi(2)).sum();
Ok(max_val * sum_sq.sqrt())
}
"factorial" | "fact" if args.len() == 1 => {
let n = checked_i64(args[0], "factorial requires integer argument")?;
if !(0..=MAX_FACTORIAL).contains(&n) {
return Err(EvaluationError::InvalidOperation(format!(
"factorial({}) out of range (0..={})",
args[0], MAX_FACTORIAL
)));
}
let s = factorial_bigint(n as u64);
Err(EvaluationError::InvalidOperation(format!(
"__int_result__{}",
s
)))
}
"perm" if args.len() == 1 => {
let n = checked_i64(args[0], "perm requires integer argument")?;
if !(0..=MAX_FACTORIAL).contains(&n) {
return Err(EvaluationError::InvalidOperation(format!(
"perm({}) out of range (0..={})",
args[0], MAX_FACTORIAL
)));
}
let s = factorial_bigint(n as u64);
Err(EvaluationError::InvalidOperation(format!(
"__int_result__{}",
s
)))
}
"perm" | "npr" if args.len() == 2 => {
let n = checked_i64(args[0], "perm requires integer arguments")?;
let r = checked_i64(args[1], "perm requires integer arguments")?;
if n < 0 || r < 0 || n > MAX_PERM_COMB || r > MAX_PERM_COMB {
return Err(EvaluationError::InvalidOperation(format!(
"perm({}, {}) out of range",
n, r
)));
}
if r > n {
return Ok(0.0);
}
let mut limbs: Vec<u64> = vec![1];
for i in 0..r as u64 {
multiply_in_place(&mut limbs, n as u64 - i);
}
bigint_or_float(limbs)
}
"comb" | "ncr" if args.len() == 2 => {
let n = checked_i64(args[0], "comb requires integer arguments")?;
let r = checked_i64(args[1], "comb requires integer arguments")?;
if n < 0 || r < 0 || n > MAX_PERM_COMB || r > MAX_PERM_COMB {
return Err(EvaluationError::InvalidOperation(format!(
"comb({}, {}) out of range",
n, r
)));
}
if r > n {
return Ok(0.0);
}
let r_small = r.min(n - r);
let mut limbs: Vec<u64> = vec![1];
for i in 0..r_small as u64 {
multiply_in_place(&mut limbs, n as u64 - i);
divide_in_place(&mut limbs, i + 1);
}
bigint_or_float(limbs)
}
"gcd" if !args.is_empty() => {
for a in &args {
if *a != a.floor() || !a.is_finite() {
return Err(EvaluationError::InvalidOperation(
"gcd() requires integer arguments".to_string(),
));
}
}
let ints: Vec<i64> = args
.iter()
.map(|a| checked_i64(*a, "gcd() requires integer arguments"))
.collect::<Result<_, _>>()?;
if ints.contains(&i64::MIN) {
return Err(EvaluationError::ValueOverflow);
}
let mut result = ints[0].abs();
for &v in &ints[1..] {
result = gcd(result, v.abs());
}
Ok(result as f64)
}
"lcm" if !args.is_empty() => {
for a in &args {
if *a != a.floor() || !a.is_finite() {
return Err(EvaluationError::InvalidOperation(
"lcm() requires integer arguments".to_string(),
));
}
}
let ints: Vec<i64> = args
.iter()
.map(|a| checked_i64(*a, "lcm() requires integer arguments"))
.collect::<Result<_, _>>()?;
if ints.contains(&i64::MIN) {
return Err(EvaluationError::ValueOverflow);
}
let mut result = ints[0].abs();
for &v in &ints[1..] {
let g = gcd(result, v.abs());
if g == 0 {
result = 0;
} else {
result = (result / g)
.abs()
.checked_mul(v.abs())
.ok_or(EvaluationError::ValueOverflow)?;
}
}
Ok(result as f64)
}
"max" if !args.is_empty() => Ok(args.iter().cloned().fold(f64::NEG_INFINITY, f64::max)),
"min" if !args.is_empty() => Ok(args.iter().cloned().fold(f64::INFINITY, f64::min)),
"sum" if args.is_empty() => Ok(0.0),
"sum" => {
let s: f64 = args.iter().sum();
check_result_value(s)
}
"mean" | "average" if !args.is_empty() => {
let s: f64 = args.iter().sum();
check_result_value(s / args.len() as f64)
}
"median" if !args.is_empty() => median(&args),
"mode" if !args.is_empty() => {
fn normalize_zero_bits(v: f64) -> u64 {
if v == 0.0 {
0.0_f64.to_bits()
} else {
v.to_bits()
}
}
let mut counts: HashMap<u64, usize> = HashMap::new();
for &a in &args {
let key = normalize_zero_bits(a);
*counts.entry(key).or_insert(0) += 1;
}
let max_count = counts.values().max().unwrap_or(&0);
for &a in &args {
if counts.get(&normalize_zero_bits(a)).unwrap_or(&0) == max_count {
return Ok(a);
}
}
Ok(args[0])
}
"product" if !args.is_empty() => {
let p: f64 = args.iter().fold(1.0, |acc, x| acc * x);
check_result_value(p)
}
"std" | "stddev" if args.len() >= 2 => {
let mean: f64 = args.iter().sum::<f64>() / args.len() as f64;
let variance: f64 =
args.iter().map(|x| (x - mean).powi(2)).sum::<f64>() / args.len() as f64;
Ok(variance.sqrt())
}
"std" | "stddev" if args.len() == 1 => Err(EvaluationError::InvalidOperation(
"std requires at least two arguments".to_string(),
)),
"std_sample" | "stds" if args.len() >= 2 => {
let mean: f64 = args.iter().sum::<f64>() / args.len() as f64;
let variance: f64 =
args.iter().map(|x| (x - mean).powi(2)).sum::<f64>() / (args.len() - 1) as f64;
Ok(variance.sqrt())
}
"variance" | "var" if args.len() >= 2 => {
let mean: f64 = args.iter().sum::<f64>() / args.len() as f64;
let variance: f64 =
args.iter().map(|x| (x - mean).powi(2)).sum::<f64>() / args.len() as f64;
Ok(variance)
}
"variance" | "var" if args.len() == 1 => Err(EvaluationError::InvalidOperation(
"variance requires at least two arguments".to_string(),
)),
"variance_sample" | "vars" | "var_sample" if args.len() >= 2 => {
let mean: f64 = args.iter().sum::<f64>() / args.len() as f64;
let variance: f64 =
args.iter().map(|x| (x - mean).powi(2)).sum::<f64>() / (args.len() - 1) as f64;
Ok(variance)
}
"percentof" | "percent_of" if args.len() == 2 => Ok(args[0] / 100.0 * args[1]),
"aspercent" | "as_percent" if args.len() == 2 => {
if args[1] == 0.0 {
return Err(EvaluationError::DivisionByZero);
}
if args[1].abs() < 1e-100 {
return Err(EvaluationError::InvalidOperation(
"aspercent: near-zero divisor could cause overflow".to_string(),
));
}
Ok(args[0] / args[1] * 100.0)
}
"clamp" if args.len() == 3 => {
if args[1] > args[2] {
return Err(EvaluationError::InvalidOperation(format!(
"clamp: lo ({}) > hi ({})",
args[1], args[2]
)));
}
Ok(args[0].max(args[1]).min(args[2]))
}
"bitand" if args.len() == 2 => {
let left = checked_i64(args[0], "bitand requires integer arguments")?;
let right = checked_i64(args[1], "bitand requires integer arguments")?;
Ok((left & right) as f64)
}
"bitor" if args.len() == 2 => {
let left = checked_i64(args[0], "bitor requires integer arguments")?;
let right = checked_i64(args[1], "bitor requires integer arguments")?;
Ok((left | right) as f64)
}
"bitxor" if args.len() == 2 => {
let left = checked_i64(args[0], "bitxor requires integer arguments")?;
let right = checked_i64(args[1], "bitxor requires integer arguments")?;
Ok((left ^ right) as f64)
}
"bitnot" if args.len() == 1 => {
let value = checked_i64(args[0], "bitnot requires integer argument")?;
Ok((!value) as f64)
}
"bitlshift" if args.len() == 2 => {
let value = checked_i64(args[0], "bitlshift requires integer arguments")?;
let shift = checked_i64(args[1], "bitlshift requires integer arguments")?;
if shift < 0 || shift as usize > MAX_SHIFT_COUNT {
return Err(EvaluationError::InvalidOperation(format!(
"Shift count {} out of range",
shift
)));
}
Ok((value.checked_shl(shift as u32).ok_or_else(|| {
EvaluationError::InvalidOperation(format!("Shift left by {} overflows", shift))
})?) as f64)
}
"bitrshift" if args.len() == 2 => {
let value = checked_i64(args[0], "bitrshift requires integer arguments")?;
let shift = checked_i64(args[1], "bitrshift requires integer arguments")?;
if shift < 0 || shift as usize > MAX_SHIFT_COUNT {
return Err(EvaluationError::InvalidOperation(format!(
"Shift count {} out of range",
shift
)));
}
Ok((value.checked_shr(shift as u32).ok_or_else(|| {
EvaluationError::InvalidOperation(format!("Shift right by {} overflows", shift))
})?) as f64)
}
"bin" if args.len() == 1 => {
let n = checked_i64(args[0], "bin requires integer argument")?;
let s = if n < 0 {
format!("-0b{:b}", n.unsigned_abs())
} else {
format!("0b{:b}", n)
};
Err(EvaluationError::InvalidOperation(format!(
"__string_result__{}",
s
)))
}
"hex" if args.len() == 1 => {
let n = checked_i64(args[0], "hex requires integer argument")?;
let s = if n < 0 {
format!("-0x{:x}", n.unsigned_abs())
} else {
format!("0x{:x}", n)
};
Err(EvaluationError::InvalidOperation(format!(
"__string_result__{}",
s
)))
}
"oct" if args.len() == 1 => {
let n = checked_i64(args[0], "oct requires integer argument")?;
let s = if n < 0 {
format!("-0o{:o}", n.unsigned_abs())
} else {
format!("0o{:o}", n)
};
Err(EvaluationError::InvalidOperation(format!(
"__string_result__{}",
s
)))
}
"isprime" | "is_prime" if args.len() == 1 => {
let n = checked_i64(args[0], "isprime requires integer argument")?;
if n > MAX_PRIME {
return Err(EvaluationError::InvalidOperation(format!(
"isprime({}) out of range",
args[0]
)));
}
Ok(if n < 2 {
0.0
} else if is_prime(n) {
1.0
} else {
0.0
})
}
"nextprime" | "next_prime" if args.len() == 1 => {
let n = checked_i64(args[0], "nextprime requires integer argument")?;
if !(0..=MAX_PRIME).contains(&n) {
return Err(EvaluationError::InvalidOperation(format!(
"nextprime({}) out of range",
args[0]
)));
}
Ok(next_prime(n)? as f64)
}
"prevprime" | "prev_prime" if args.len() == 1 => {
let n = checked_i64(args[0], "prevprime requires integer argument")?;
if n <= 2 || n > MAX_PRIME {
return Err(EvaluationError::InvalidOperation(format!(
"prevprime({}) out of range",
args[0]
)));
}
Ok(prev_prime(n)? as f64)
}
"primefactors" | "prime_factors" if args.len() == 1 => {
let n = checked_i64(args[0], "primefactors requires integer argument")?;
if n < 0 {
return Err(EvaluationError::InvalidOperation(format!(
"primefactors({}) out of range",
args[0]
)));
}
if n > 1_000_000_000_000 {
return Err(EvaluationError::InvalidOperation(
"factorization not available for numbers > 10^12".to_string(),
));
}
let s = prime_factors_string(n);
Err(EvaluationError::InvalidOperation(format!(
"__string_result__{}",
s
)))
}
"real" if args.len() == 1 => Ok(args[0]),
"imag" | "imaginary" if args.len() == 1 => Ok(0.0),
"conj" | "conjugate" if args.len() == 1 => Ok(args[0]),
"phase" | "arg" | "argument" if args.len() == 1 => Ok(if args[0] >= 0.0 {
0.0
} else {
std::f64::consts::PI
}),
"polar" if args.len() == 1 => {
let r = args[0].abs();
let phi = if args[0] >= 0.0 {
0.0
} else {
std::f64::consts::PI
};
let s = format!("({}, {})", r, phi);
Err(EvaluationError::InvalidOperation(format!(
"__string_result__{}",
s
)))
}
"polar" if args.len() == 2 => {
let s = format!("({}, {})", args[0], args[1]);
Err(EvaluationError::InvalidOperation(format!(
"__string_result__{}",
s
)))
}
"rect" if args.len() == 2 => {
let r = args[0];
let phi = args[1];
let real = r * phi.cos();
let imag = r * phi.sin();
let s = format!("({}, {})", real, imag);
Err(EvaluationError::InvalidOperation(format!(
"__string_result__{}",
s
)))
}
"random" | "rand" if args.is_empty() => Ok(prng_random_with(ctx)),
"randint" if args.len() == 2 => {
let a = checked_i64(args[0], "randint requires integer arguments")?;
let b = checked_i64(args[1], "randint requires integer arguments")?;
if a > b {
return Err(EvaluationError::InvalidOperation(format!(
"randint: lower bound ({}) > upper bound ({})",
a, b
)));
}
Ok(random_int_inclusive(a, b, || prng_next_u64_with(ctx)) as f64)
}
"randrange" if args.len() == 1 => {
let a = checked_i64(args[0], "randrange requires integer argument")?;
if a <= 0 {
return Err(EvaluationError::InvalidOperation(
"randrange: argument must be positive".to_string(),
));
}
Ok(random_int_inclusive(0, a - 1, || prng_next_u64_with(ctx)) as f64)
}
"randrange" if args.len() == 2 => {
let a = checked_i64(args[0], "randrange requires integer arguments")?;
let b = checked_i64(args[1], "randrange requires integer arguments")?;
if a >= b {
return Err(EvaluationError::InvalidOperation(
"randrange: start must be less than stop".to_string(),
));
}
Ok(random_int_inclusive(a, b - 1, || prng_next_u64_with(ctx)) as f64)
}
"uniform" if args.len() == 2 => {
let a = args[0];
let b = args[1];
if a > b {
return Err(EvaluationError::InvalidOperation(format!(
"uniform: lower bound ({}) > upper bound ({})",
a, b
)));
}
Ok(a + prng_random_with(ctx) * (b - a))
}
"randn" if args.is_empty() => Ok(prng_randn_with(ctx)),
"gauss" | "normal" if args.len() == 2 => {
let mu = args[0];
let sigma = args[1];
Ok(mu + sigma * prng_randn_with(ctx))
}
"seed" if args.is_empty() => {
ctx.prng_state = 123456789;
ctx.gauss_spare = None;
Ok(0.0)
}
"seed" if args.len() == 1 => {
if !args[0].is_finite() {
return Err(EvaluationError::InvalidOperation(
"seed requires a finite number".to_string(),
));
}
let s = args[0].trunc() as i64 as u64;
prng_seed_with(ctx, s);
Ok(0.0)
}
"store" if args.len() == 1 => {
ctx.memory_registers.insert("M".to_string(), args[0]);
Ok(args[0])
}
"store" if args.len() == 2 => {
let id = checked_i64(args[1], "register index requires integer argument")?;
let name = format!("R{}", id);
ctx.memory_registers.insert(name, args[0]);
Ok(args[0])
}
"recall" if args.is_empty() => Ok(*ctx.memory_registers.get("M").unwrap_or(&0.0)),
"recall" if args.len() == 1 => {
let id = checked_i64(args[0], "register index requires integer argument")?;
let name = format!("R{}", id);
Ok(*ctx.memory_registers.get(&name).unwrap_or(&0.0))
}
"mplus" | "m+" | "madd" if args.len() == 1 => {
let current = *ctx.memory_registers.get("M").unwrap_or(&0.0);
let new_val = current + args[0];
ctx.memory_registers.insert("M".to_string(), new_val);
Ok(new_val)
}
"mminus" | "m-" | "msub" if args.len() == 1 => {
let current = *ctx.memory_registers.get("M").unwrap_or(&0.0);
let new_val = current - args[0];
ctx.memory_registers.insert("M".to_string(), new_val);
Ok(new_val)
}
"mc" | "mclear" if args.is_empty() => {
ctx.memory_registers.clear();
Ok(0.0)
}
"mr" | "mrecall" if args.is_empty() => Ok(*ctx.memory_registers.get("M").unwrap_or(&0.0)),
"setvar" if args.len() == 2 => {
let var_id = checked_i64(args[1], "variable id requires integer argument")?;
let key = format!("v{}", var_id);
if !ctx.user_variables.contains_key(&key)
&& ctx.user_variables.len() >= MAX_USER_VARIABLES_CTX
{
if let Some(oldest) = ctx.user_variables.keys().min().cloned() {
ctx.user_variables.remove(&oldest);
}
}
ctx.user_variables.insert(key, args[0]);
Ok(args[0])
}
"getvar" if args.len() == 1 => {
let var_id = checked_i64(args[0], "variable id requires integer argument")?;
let key = format!("v{}", var_id);
Ok(*ctx.user_variables.get(&key).unwrap_or(&0.0))
}
"getvar" if args.len() == 2 => {
let var_id = checked_i64(args[0], "variable id requires integer argument")?;
let key = format!("v{}", var_id);
Ok(*ctx.user_variables.get(&key).unwrap_or(&args[1]))
}
"delvar" if args.len() == 1 => {
let var_id = checked_i64(args[0], "variable id requires integer argument")?;
let key = format!("v{}", var_id);
ctx.user_variables.remove(&key);
Ok(0.0)
}
"listvars" if args.is_empty() => {
let s = if ctx.user_variables.is_empty() {
"{}".to_string()
} else {
let entries: Vec<String> = ctx
.user_variables
.iter()
.map(|(k, v)| format!("{}: {}", k, v))
.collect();
format!("{{{}}}", entries.join(", "))
};
Err(EvaluationError::InvalidOperation(format!(
"__string_result__{}",
s
)))
}
"clearvars" if args.is_empty() => {
ctx.user_variables.clear();
Ok(0.0)
}
"convert" => Err(EvaluationError::InvalidOperation(
"convert() must be called through the run() pipeline, not evaluate() directly"
.to_string(),
)),
"temp" => Err(EvaluationError::InvalidOperation(
"temp() must be called through the run() pipeline, not evaluate() directly".to_string(),
)),
_ => Err(EvaluationError::UnknownFunction(format!(
"{}({} args)",
name,
args.len()
))),
}
}
fn factorial_bigint(n: u64) -> String {
let mut limbs: Vec<u64> = vec![1];
for i in 2..=n {
multiply_in_place(&mut limbs, i);
}
limbs_to_string(&limbs)
}
fn limbs_to_string(limbs: &[u64]) -> String {
if limbs.is_empty() {
return "0".to_string();
}
let mut out = limbs.last().copied().unwrap_or(0).to_string();
for &limb in limbs.iter().rev().skip(1) {
out.push_str(&format!("{:09}", limb));
}
if out.is_empty() {
out = "0".to_string();
}
out
}
fn bigint_or_float(limbs: Vec<u64>) -> Result<f64, EvaluationError> {
let s = limbs_to_string(&limbs);
if let Ok(u) = s.parse::<u64>() {
let f = u as f64;
if f as u64 == u && f.is_finite() {
return Ok(f);
}
}
Err(EvaluationError::InvalidOperation(format!(
"__int_result__{}",
s
)))
}
fn multiply_in_place(limbs: &mut Vec<u64>, multiplier: u64) {
let mut carry: u64 = 0;
for limb in limbs.iter_mut() {
let product = (*limb as u128) * (multiplier as u128) + (carry as u128);
*limb = (product % 1_000_000_000u128) as u64;
carry = (product / 1_000_000_000u128) as u64;
}
while carry > 0 {
limbs.push(carry % 1_000_000_000);
carry /= 1_000_000_000;
}
}
fn divide_in_place(limbs: &mut Vec<u64>, divisor: u64) {
let mut remainder: u64 = 0;
for limb in limbs.iter_mut().rev() {
let combined = (remainder as u128) * 1_000_000_000u128 + (*limb as u128);
*limb = (combined / divisor as u128) as u64;
remainder = (combined % divisor as u128) as u64;
}
while limbs.len() > 1 && *limbs.last().unwrap() == 0 {
limbs.pop();
}
let _ = remainder;
}
fn gcd(a: i64, b: i64) -> i64 {
if a == 0 {
return b.abs();
}
if b == 0 {
return a.abs();
}
gcd(b, a % b)
}
fn is_prime(n: i64) -> bool {
if n < 2 {
return false;
}
let n = n as u64;
for prime in [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37] {
if n == prime {
return true;
}
if n.is_multiple_of(prime) {
return false;
}
}
let mut odd_part = n - 1;
let mut powers_of_two = 0;
while odd_part.is_multiple_of(2) {
odd_part /= 2;
powers_of_two += 1;
}
[2, 3, 5, 7, 11, 13, 17].iter().all(|&base| {
let mut value = modular_pow(base, odd_part, n);
if value == 1 || value == n - 1 {
return true;
}
for _ in 1..powers_of_two {
value = modular_mul(value, value, n);
if value == n - 1 {
return true;
}
}
false
})
}
fn modular_mul(a: u64, b: u64, modulus: u64) -> u64 {
((a as u128 * b as u128) % modulus as u128) as u64
}
fn modular_pow(mut base: u64, mut exponent: u64, modulus: u64) -> u64 {
let mut result = 1;
base %= modulus;
while exponent > 0 {
if exponent & 1 == 1 {
result = modular_mul(result, base, modulus);
}
base = modular_mul(base, base, modulus);
exponent >>= 1;
}
result
}
fn next_prime(n: i64) -> Result<i64, EvaluationError> {
let mut candidate = if n < 2 {
2
} else {
n.checked_add(1).ok_or(EvaluationError::ValueOverflow)?
};
if candidate % 2 == 0 && candidate > 2 {
candidate = candidate
.checked_add(1)
.ok_or(EvaluationError::ValueOverflow)?;
}
let mut iterations = 0;
while iterations < 10_000 {
if is_prime(candidate) {
return Ok(candidate);
}
candidate = candidate
.checked_add(2)
.ok_or(EvaluationError::ValueOverflow)?;
iterations += 1;
}
Err(EvaluationError::InvalidOperation(
"Could not find next prime within search limit".to_string(),
))
}
fn prev_prime(n: i64) -> Result<i64, EvaluationError> {
if n <= 2 {
return Err(EvaluationError::InvalidOperation(
"prevprime: no prime less than 2 exists".to_string(),
));
}
let mut candidate = n - 1;
if candidate % 2 == 0 {
candidate -= 1;
}
let mut iterations = 0;
while iterations < 10_000 {
if is_prime(candidate) {
return Ok(candidate);
}
candidate -= 2;
if candidate < 2 {
return Ok(2);
}
iterations += 1;
}
Err(EvaluationError::InvalidOperation(
"Could not find previous prime within search limit".to_string(),
))
}
fn prime_factors_string(n: i64) -> String {
if n < 2 {
return n.to_string();
}
let mut factors: std::collections::HashMap<i64, i64> = std::collections::HashMap::new();
let mut temp = n;
let mut d = 2i64;
while d * d <= temp {
while temp % d == 0 {
*factors.entry(d).or_insert(0) += 1;
temp /= d;
}
d += 1;
}
if temp > 1 {
*factors.entry(temp).or_insert(0) += 1;
}
let mut parts: Vec<String> = Vec::new();
let mut primes: Vec<i64> = factors.keys().copied().collect();
primes.sort();
for prime in primes {
let exp = factors[&prime];
if exp == 1 {
parts.push(prime.to_string());
} else {
parts.push(format!("{}^{}", prime, exp));
}
}
parts.join(" × ")
}
fn xorshift64(state: &mut u64) -> u64 {
if *state == 0 {
*state = 123456789;
}
*state ^= *state << 13;
*state ^= *state >> 7;
*state ^= *state << 17;
*state
}
fn prng_next_u64() -> u64 {
let mut prng = PRNG_STATE.lock().unwrap_or_else(|e| e.into_inner());
xorshift64(&mut prng.0)
}
fn prng_random() -> f64 {
prng_next_u64() as f64 / u64::MAX as f64
}
fn prng_next_u64_with(ctx: &mut EvalContext) -> u64 {
xorshift64(&mut ctx.prng_state)
}
fn random_int_inclusive(a: i64, b: i64, mut next: impl FnMut() -> u64) -> i64 {
let range = (b as i128 - a as i128 + 1) as u128;
let offset = if range == (u64::MAX as u128) + 1 {
next() as u128
} else {
let range = range as u64;
let limit = u64::MAX - u64::MAX % range;
let mut fallback = 0u64;
let mut sampled = false;
let mut result = 0u128;
let mut accepted = false;
for _ in 0..1000 {
let value = next();
fallback = value;
sampled = true;
if value < limit {
result = (value % range) as u128;
accepted = true;
break;
}
}
if accepted {
result
} else {
debug_assert!(sampled, "rejection loop must sample at least once");
(fallback % range) as u128
}
};
(a as i128 + offset as i128) as i64
}
fn prng_seed(s: u64) {
let mut prng = PRNG_STATE.lock().unwrap_or_else(|e| e.into_inner());
prng.0 = if s == 0 { 123456789 } else { s };
prng.1 = None;
}
fn prng_randn() -> f64 {
let mut prng = PRNG_STATE.lock().unwrap_or_else(|e| e.into_inner());
if let Some(val) = prng.1.take() {
return val;
}
let mut u1 = xorshift64(&mut prng.0) as f64 / u64::MAX as f64;
while u1 == 0.0 {
u1 = xorshift64(&mut prng.0) as f64 / u64::MAX as f64;
}
let u2 = xorshift64(&mut prng.0) as f64 / u64::MAX as f64;
let z0 = (-2.0 * u1.ln()).sqrt() * (2.0 * std::f64::consts::PI * u2).cos();
let z1 = (-2.0 * u1.ln()).sqrt() * (2.0 * std::f64::consts::PI * u2).sin();
prng.1 = Some(z1);
z0
}
#[cfg(test)]
mod tests {
use super::*;
fn val(expr: &str) -> String {
evaluate(expr).unwrap().0
}
#[test]
fn test_basic_arithmetic() {
assert_eq!(val("5 + 3"), "8");
assert_eq!(val("10 - 4"), "6");
assert_eq!(val("6 * 7"), "42");
assert_eq!(val("15 / 3"), "5");
assert_eq!(val("17 % 5"), "2");
}
#[test]
fn test_scientific_notation() {
assert_eq!(val("1e5"), "100000");
assert_eq!(val("1.5E-3"), "0.0015");
assert_eq!(val(".5e+2"), "50");
assert_eq!(val("1E3"), "1000");
}
#[test]
fn test_hex_literal() {
assert_eq!(val("0xFF"), "255");
assert_eq!(val("0x0"), "0");
assert_eq!(val("0xDEAD"), "57005");
assert_eq!(val("0xff"), "255");
}
#[test]
fn test_octal_literal() {
assert_eq!(val("0o17"), "15");
assert_eq!(val("0o77"), "63");
assert_eq!(val("0o0"), "0");
}
#[test]
fn test_binary_literal() {
assert_eq!(val("0b1010"), "10");
assert_eq!(val("0b0"), "0");
assert_eq!(val("0b11111111"), "255");
}
#[test]
fn test_hex_with_underscores() {
assert_eq!(val("0xFF_FF"), "65535");
}
#[test]
fn test_decimal_with_underscores() {
assert_eq!(val("1_000"), "1000");
assert_eq!(val("1_000_000"), "1000000");
}
#[test]
fn test_power_right_associative() {
assert_eq!(val("2 ** 3 ** 2"), "512");
assert_eq!(val("2 ** 2 ** 3"), "256");
assert_eq!(val("2 ** 3 ** 2 ** 1"), "512");
}
#[test]
fn test_floor_division() {
assert_eq!(val("7 // 2"), "3");
assert_eq!(val("-7 // 2"), "-4");
assert_eq!(val("7 // -2"), "-4");
assert_eq!(val("10 // 3"), "3");
assert_eq!(val("0 // 5"), "0");
}
#[test]
fn test_bitwise_not() {
assert_eq!(val("~0"), "-1");
assert_eq!(val("~(-1)"), "0");
assert_eq!(val("~255"), "-256");
}
#[test]
fn test_bitwise_and() {
assert_eq!(val("5 & 3"), "1");
assert_eq!(val("0xFF & 0x0F"), "15");
assert_eq!(val("12 & 10"), "8");
}
#[test]
fn test_bitwise_or() {
assert_eq!(val("5 | 3"), "7");
assert_eq!(val("0 | 0xFF"), "255");
}
#[test]
fn test_bitwise_xor() {
assert_eq!(val("5 ^ 3"), "6");
assert_eq!(val("0xFF ^ 0x0F"), "240");
}
#[test]
fn test_shift_left() {
assert_eq!(val("1 << 4"), "16");
assert_eq!(val("5 << 3"), "40");
assert_eq!(val("1 << 0"), "1");
}
#[test]
fn test_shift_right() {
assert_eq!(val("16 >> 2"), "4");
assert_eq!(val("100 >> 3"), "12");
assert_eq!(val("1 >> 0"), "1");
}
#[test]
fn test_shifts_reject_fractional_operands() {
assert!(evaluate("3.5 << 1").is_err());
assert!(evaluate("3 >> 1.5").is_err());
assert!(evaluate("bitlshift(3.5, 1)").is_err());
assert!(evaluate("bitrshift(3, 1.5)").is_err());
}
#[test]
fn test_bitwise_precedence() {
assert_eq!(val("1 | 2 ^ 3"), "1");
assert_eq!(val("6 ^ 3 & 1"), "7");
assert_eq!(val("1 << 4 & 0xFF"), "16");
}
#[test]
fn test_unary_minus_with_power() {
assert_eq!(val("-2 ** 2"), "-4");
assert_eq!(val("-(2 ** 2)"), "-4");
}
#[test]
fn test_inverse_hyperbolic() {
let v: f64 = val("asinh(1)").parse().unwrap();
assert!((v - 0.881373587019543).abs() < 1e-10);
let v: f64 = val("acosh(1)").parse().unwrap();
assert!(v.abs() < 1e-10);
let v: f64 = val("atanh(0.5)").parse().unwrap();
assert!((v - 0.5493061443340549).abs() < 1e-10);
}
#[test]
fn test_log_with_base() {
assert_eq!(val("log(100, 10)"), "2");
assert_eq!(val("log(8, 2)"), "3");
assert_eq!(val("ln(e)"), "1");
}
#[test]
fn test_log1p_expm1() {
let v: f64 = val("log1p(0)").parse().unwrap();
assert!(v.abs() < 1e-10);
let v: f64 = val("log1p(1)").parse().unwrap();
assert!((v - std::f64::consts::LN_2).abs() < 1e-10);
let v: f64 = val("expm1(0)").parse().unwrap();
assert!(v.abs() < 1e-10);
let v: f64 = val("expm1(1)").parse().unwrap();
assert!((v - 1.718281828459045).abs() < 1e-10);
}
#[test]
fn test_degrees_radians() {
assert_eq!(val("degrees(pi)"), "180");
assert_eq!(val("radians(180)"), "3.141592653589793");
assert_eq!(val("degrees(0)"), "0");
assert_eq!(val("radians(0)"), "0");
}
#[test]
fn test_hypot() {
let v: f64 = val("hypot(3, 4)").parse().unwrap();
assert!((v - 5.0).abs() < 1e-10);
assert_eq!(val("hypot(0, 0)"), "0");
}
#[test]
fn test_randint_full_positive_i64_range_does_not_overflow() {
let result = random_int_inclusive(0, i64::MAX, || 0);
assert_eq!(result, 0);
}
#[test]
fn test_perm_comb() {
assert_eq!(val("perm(5, 2)"), "20");
assert_eq!(val("comb(5, 2)"), "10");
assert_eq!(val("nPr(5, 2)"), "20");
assert_eq!(val("nCr(5, 2)"), "10");
assert_eq!(val("perm(5)"), "120");
assert_eq!(val("perm(3, 5)"), "0");
}
#[test]
fn test_clamp() {
assert_eq!(val("clamp(5, 0, 10)"), "5");
assert_eq!(val("clamp(-5, 0, 10)"), "0");
assert_eq!(val("clamp(15, 0, 10)"), "10");
}
#[test]
fn test_mode() {
assert_eq!(val("mode(1, 2, 2, 3)"), "2");
assert_eq!(val("mode(1, 1, 2)"), "1");
assert_eq!(val("mode(5)"), "5");
}
#[test]
fn test_sample_stats() {
let v: f64 = val("std_sample(2, 4, 4, 4, 5, 5, 7, 9)").parse().unwrap();
assert!((v - 2.1381).abs() < 0.01);
let v: f64 = val("variance_sample(2, 4, 4, 4, 5, 5, 7, 9)")
.parse()
.unwrap();
assert!((v - 4.5714).abs() < 0.01);
assert_eq!(val("stds(1, 1)"), "0");
assert_eq!(val("vars(1, 1)"), "0");
}
#[test]
fn test_is_prime() {
assert_eq!(val("isprime(2)"), "1");
assert_eq!(val("isprime(3)"), "1");
assert_eq!(val("isprime(4)"), "0");
assert_eq!(val("isprime(17)"), "1");
assert_eq!(val("isprime(1)"), "0");
assert_eq!(val("isprime(0)"), "0");
assert_eq!(val("is_prime(97)"), "1");
}
#[test]
fn test_next_prev_prime() {
assert_eq!(val("nextprime(10)"), "11");
assert_eq!(val("nextprime(11)"), "13");
assert_eq!(val("prevprime(10)"), "7");
assert_eq!(val("prevprime(11)"), "7");
assert_eq!(val("next_prime(0)"), "2");
assert_eq!(val("prev_prime(5)"), "3");
assert!(evaluate("prevprime(2)").is_err());
assert!(evaluate("prevprime(1)").is_err());
}
#[test]
fn test_prime_factors() {
assert_eq!(val("primefactors(12)"), "2^2 × 3");
assert_eq!(val("prime_factors(12)"), "2^2 × 3");
assert_eq!(val("primefactors(7)"), "7");
assert_eq!(val("primefactors(100)"), "2^2 × 5^2");
assert_eq!(val("primefactors(1)"), "1");
assert_eq!(val("primefactors(0)"), "0");
assert_eq!(val("primefactors(2)"), "2");
assert_eq!(val("primefactors(36)"), "2^2 × 3^2");
assert!(evaluate("primefactors(-1)").is_err());
assert!(evaluate("primefactors(1000000000001)").is_err());
}
#[test]
fn test_percent() {
assert_eq!(val("percentof(50, 200)"), "100");
assert_eq!(val("percentof(25, 100)"), "25");
assert_eq!(val("aspercent(25, 100)"), "25");
assert_eq!(val("aspercent(1, 4)"), "25");
assert_eq!(val("percent_of(50, 200)"), "100");
assert_eq!(val("as_percent(1, 4)"), "25");
}
#[test]
fn test_factorial_alias() {
assert_eq!(val("fact(5)"), "120");
assert_eq!(val("fact(0)"), "1");
}
#[test]
fn test_average_alias() {
assert_eq!(val("average(2, 4, 6)"), "4");
}
#[test]
fn test_variadic_gcd() {
assert_eq!(val("gcd(12, 8, 4)"), "4");
assert_eq!(val("gcd(7)"), "7");
}
#[test]
fn test_variadic_lcm() {
assert_eq!(val("lcm(4, 6)"), "12");
assert_eq!(val("lcm(2, 3, 4)"), "12");
}
#[test]
fn test_gcd_lcm_min_i64_fail_closed() {
assert!(evaluate("gcd(-9223372036854775808)").is_err());
assert!(evaluate("lcm(-9223372036854775808, 2)").is_err());
}
#[test]
fn test_single_arg_std_var() {
let r = evaluate("std(5)");
assert!(r.is_err());
assert!(r.unwrap_err().contains("requires at least two"));
let r = evaluate("variance(5)");
assert!(r.is_err());
assert!(r.unwrap_err().contains("requires at least two"));
}
#[test]
fn test_max_exponent_enforced() {
let r = evaluate("2 ** 100001");
assert!(r.is_err());
assert!(r.unwrap_err().contains("out of range"));
}
#[test]
fn test_factorial_range() {
let r = evaluate("factorial(1001)");
assert!(r.is_err());
assert!(!val("factorial(170)").is_empty());
assert!(!val("factorial(1000)").is_empty());
}
#[test]
fn test_shift_count_limit() {
let r = evaluate("1 << 50001");
assert!(r.is_err());
}
#[test]
fn test_nesting_depth() {
let mut deep = String::new();
for _ in 0..101 {
deep.push('(');
}
deep.push('1');
for _ in 0..101 {
deep.push(')');
}
let r = evaluate(&deep);
assert!(r.is_err());
assert!(r.unwrap_err().contains("Stack overflow"));
}
#[test]
fn test_input_length_limit() {
let long: String = "1 + ".repeat(30_001);
let r = evaluate(&long);
assert!(r.is_err());
assert!(r.unwrap_err().contains("exceeds"));
}
#[test]
fn test_zero_power_zero() {
assert_eq!(val("0 ** 0"), "1");
}
#[test]
fn test_negative_base_integer_exponent() {
assert_eq!(val("(-2) ** 3"), "-8");
assert_eq!(val("(-2) ** 2"), "4");
}
#[test]
fn test_complex_precedence() {
assert_eq!(val("2 + 3 * 4"), "14");
assert_eq!(val("(2 + 3) * 4"), "20");
assert_eq!(val("2 * 3 + 4 * 5"), "26");
assert_eq!(val("2 ** 3 * 2"), "16");
assert_eq!(val("2 * 3 ** 2"), "18");
}
#[test]
fn test_unknown_function_error() {
let r = evaluate("foobar(1)");
assert!(r.is_err());
assert!(r.unwrap_err().contains("Unknown function"));
}
#[test]
fn test_unknown_constant_error() {
let r = evaluate("xyz");
assert!(r.is_err());
assert!(r.unwrap_err().contains("Unknown constant"));
}
#[test]
fn test_division_by_zero() {
let r = evaluate("1 / 0");
assert!(r.is_err());
assert!(r.unwrap_err().contains("Division by zero"));
}
#[test]
fn test_floor_div_by_zero() {
let r = evaluate("1 // 0");
assert!(r.is_err());
assert!(r.unwrap_err().contains("Division by zero"));
}
#[test]
#[allow(deprecated)]
fn test_set_mcp_mode_is_idempotent() {
set_mcp_mode();
set_mcp_mode();
assert!(is_mcp_mode());
}
#[test]
#[allow(deprecated)]
fn test_is_mcp_mode_after_set() {
set_mcp_mode();
assert!(is_mcp_mode());
}
#[test]
#[allow(deprecated)]
fn test_mcp_mode_rejects_random_functions() {
set_mcp_mode();
let r = evaluate("random(1)");
assert!(r.is_err());
let err = r.unwrap_err();
assert!(
err.contains("non-deterministic") || err.contains("disabled"),
"Expected random function rejection, got: {}",
err
);
}
#[test]
#[allow(deprecated)]
fn test_mcp_mode_rejects_randint() {
set_mcp_mode();
let r = evaluate("randint(1, 10)");
assert!(r.is_err());
let err = r.unwrap_err();
assert!(
err.contains("non-deterministic") || err.contains("disabled"),
"Expected randint rejection, got: {}",
err
);
}
#[test]
#[allow(deprecated)]
fn test_mcp_mode_rejects_gauss() {
set_mcp_mode();
let r = evaluate("gauss(0, 1)");
assert!(r.is_err());
let err = r.unwrap_err();
assert!(
err.contains("non-deterministic") || err.contains("disabled"),
"Expected gauss rejection, got: {}",
err
);
}
#[test]
#[allow(deprecated)]
fn test_mcp_mode_rejects_side_effect_functions() {
set_mcp_mode();
let r = evaluate("setvar(1, 2)");
assert!(r.is_err());
let err = r.unwrap_err();
assert!(
err.contains("mutates") || err.contains("disabled"),
"Expected side-effect function rejection, got: {}",
err
);
}
#[test]
#[allow(deprecated)]
fn test_mcp_mode_allows_deterministic_functions() {
set_mcp_mode();
assert_eq!(val("sin(0)"), "0");
assert_eq!(val("sqrt(144)"), "12");
assert_eq!(val("abs(-5)"), "5");
assert_eq!(val("5 + 3"), "8");
assert_eq!(val("factorial(5)"), "120");
}
#[test]
fn test_mode_uniform_frequency() {
assert_eq!(val("mode(1, 2, 3)"), "1");
}
#[test]
fn test_mode_negative_zero() {
assert_eq!(val("mode(0, -0, 1, 2)"), "0");
}
#[test]
fn test_exp_overflow() {
let r = evaluate("exp(1000)");
assert!(r.is_err());
assert!(r.unwrap_err().contains("too large"));
}
#[test]
fn test_isprime_negative() {
assert_eq!(val("isprime(-5)"), "0");
}
#[test]
fn test_isprime_zero() {
assert_eq!(val("isprime(0)"), "0");
}
#[test]
fn test_isprime_one() {
assert_eq!(val("isprime(1)"), "0");
}
#[test]
fn test_sum_no_args() {
assert_eq!(val("sum()"), "0");
}
#[test]
fn test_negative_base_near_integer_exponent() {
assert_eq!(val("(-2) ** 2.0000000001"), "4");
}
}