use crate::core::{CalcError, MAX_FACTORIAL_INPUT};
pub fn add(a: f64, b: f64) -> Result<f64, CalcError> {
let r = a + b;
if !r.is_finite() {
return Err(CalcError::nan_or_inf());
}
Ok(r)
}
pub fn sub(a: f64, b: f64) -> Result<f64, CalcError> {
let r = a - b;
if !r.is_finite() {
return Err(CalcError::nan_or_inf());
}
Ok(r)
}
pub fn mul(a: f64, b: f64) -> Result<f64, CalcError> {
let r = a * b;
if !r.is_finite() {
return Err(CalcError::nan_or_inf());
}
Ok(r)
}
pub fn div(a: f64, b: f64) -> Result<f64, CalcError> {
if a.is_nan() || b.is_nan() {
return Err(CalcError::nan_or_inf());
}
if b == 0.0 {
if a == 0.0 {
return Err(CalcError::nan_or_inf());
}
return Err(CalcError::division_by_zero());
}
let r = a / b;
if !r.is_finite() {
return Err(CalcError::nan_or_inf());
}
Ok(r)
}
pub fn pow(a: f64, b: f64) -> Result<f64, CalcError> {
if a == 0.0 && b == 0.0 {
return Ok(1.0);
}
if a < 0.0 && b.fract() != 0.0 {
return Err(CalcError::domain(format!(
"negative base with non-integer exponent: {}^{}",
a, b
))
.with_hint("use complex numbers for negative base with fractional exponent")
.with_hint_i18n("hint.complex_for_neg_base", vec![])
.with_i18n(
"msg.arithmetic.neg_base_frac_exp",
vec![
("base".to_string(), a.to_string()),
("exponent".to_string(), b.to_string()),
],
));
}
let r = a.powf(b);
if !r.is_finite() {
return Err(CalcError::nan_or_inf());
}
Ok(r)
}
pub fn rem(a: f64, b: f64) -> Result<f64, CalcError> {
if a.is_nan() || b.is_nan() {
return Err(CalcError::nan_or_inf());
}
if b == 0.0 {
return Err(CalcError::division_by_zero());
}
let r = a % b;
if !r.is_finite() {
return Err(CalcError::nan_or_inf());
}
Ok(r)
}
pub fn factorial(n: f64) -> Result<f64, CalcError> {
if n < 0.0 || n.fract() != 0.0 {
return Err(CalcError::domain(format!(
"factorial requires non-negative integer, got {}",
n
))
.with_i18n(
"msg.core.factorial_negative",
vec![("value".to_string(), n.to_string())],
));
}
let n = n as u64;
if n > MAX_FACTORIAL_INPUT {
return Err(CalcError::overflow());
}
let mut result: f64 = 1.0;
for i in 2..=n {
result *= i as f64;
if result.is_infinite() {
return Err(CalcError::overflow());
}
}
Ok(result)
}
pub fn abs(x: f64) -> f64 {
x.abs()
}
#[cfg(test)]
mod tests {
use super::*;
use crate::core::ErrorKind;
#[test]
fn test_add_normal() {
assert_eq!(add(2.0, 3.0).unwrap(), 5.0);
}
#[test]
fn test_add_negative() {
assert_eq!(add(-1.0, 1.0).unwrap(), 0.0);
}
#[test]
fn test_add_overflow() {
let r = add(f64::MAX, f64::MAX);
assert!(matches!(&r, Err(e) if e.kind == ErrorKind::NaNOrInf));
}
#[test]
fn test_sub_normal() {
assert_eq!(sub(10.0, 4.0).unwrap(), 6.0);
}
#[test]
fn test_sub_overflow() {
let r = sub(f64::MIN, f64::MAX);
assert!(matches!(&r, Err(e) if e.kind == ErrorKind::NaNOrInf));
}
#[test]
fn test_mul_normal() {
assert_eq!(mul(6.0, 7.0).unwrap(), 42.0);
}
#[test]
fn test_mul_zero() {
assert_eq!(mul(0.0, 1e308).unwrap(), 0.0);
}
#[test]
fn test_mul_overflow() {
let r = mul(f64::MAX, 2.0);
assert!(matches!(&r, Err(e) if e.kind == ErrorKind::NaNOrInf));
}
#[test]
fn test_div_normal() {
assert_eq!(div(20.0, 4.0).unwrap(), 5.0);
}
#[test]
fn test_div_zero_zero() {
let r = div(0.0, 0.0);
assert!(matches!(&r, Err(e) if e.kind == ErrorKind::NaNOrInf));
}
#[test]
fn test_div_nonzero_zero() {
let r = div(5.0, 0.0);
assert!(matches!(&r, Err(e) if e.kind == ErrorKind::DivisionByZero));
}
#[test]
fn test_pow_integer() {
assert_eq!(pow(2.0, 10.0).unwrap(), 1024.0);
}
#[test]
fn test_pow_zero_zero() {
assert_eq!(pow(0.0, 0.0).unwrap(), 1.0);
}
#[test]
fn test_pow_fractional() {
let r = pow(2.0, 0.5).unwrap();
assert!((r - 1.4142135623730951).abs() < 1e-10);
}
#[test]
fn test_pow_overflow() {
let r = pow(2.0, 10000.0);
assert!(matches!(&r, Err(e) if e.kind == ErrorKind::NaNOrInf));
}
#[test]
fn test_rem_normal() {
assert_eq!(rem(10.0, 3.0).unwrap(), 1.0);
}
#[test]
fn test_rem_negative() {
assert_eq!(rem(-7.0, 3.0).unwrap(), -1.0);
}
#[test]
fn test_rem_zero_divisor() {
let r = rem(10.0, 0.0);
assert!(matches!(&r, Err(e) if e.kind == ErrorKind::DivisionByZero));
}
#[test]
fn test_factorial_zero() {
assert_eq!(factorial(0.0).unwrap(), 1.0);
}
#[test]
fn test_factorial_five() {
assert_eq!(factorial(5.0).unwrap(), 120.0);
}
#[test]
fn test_factorial_ten() {
assert_eq!(factorial(10.0).unwrap(), 3628800.0);
}
#[test]
fn test_factorial_negative() {
let r = factorial(-1.0);
assert!(matches!(&r, Err(e) if e.kind == ErrorKind::Domain));
}
#[test]
fn test_factorial_fractional() {
let r = factorial(2.5);
assert!(matches!(&r, Err(e) if e.kind == ErrorKind::Domain));
}
#[test]
fn test_factorial_exceeds_bound() {
let r = factorial(10001.0);
assert!(matches!(&r, Err(e) if e.kind == ErrorKind::Overflow));
}
#[test]
fn test_factorial_overflow_f64() {
let r = factorial(171.0);
assert!(matches!(&r, Err(e) if e.kind == ErrorKind::Overflow));
}
#[test]
fn test_abs_negative() {
assert_eq!(abs(-5.0), 5.0);
}
#[test]
fn test_abs_positive() {
assert_eq!(abs(3.14), 3.14);
}
#[test]
fn test_abs_zero() {
assert_eq!(abs(0.0), 0.0);
}
}