Skip to content

Commit

Permalink
added impls for f64 methods on Num
Browse files Browse the repository at this point in the history
  • Loading branch information
g-s-k committed Jan 29, 2019
1 parent 0a47eca commit b991606
Show file tree
Hide file tree
Showing 3 changed files with 197 additions and 31 deletions.
61 changes: 31 additions & 30 deletions src/ctx/math.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::super::proc::utils::*;
use super::super::Num;
use super::Context;

macro_rules! define_with {
Expand Down Expand Up @@ -27,54 +28,54 @@ impl Context {
/// ```
pub fn math(mut self) -> Self {
// identification
define_with!(self, "is-nan", f64::is_nan, make_unary_numeric);
define_with!(self, "is-infinite", f64::is_infinite, make_unary_numeric);
define_with!(self, "is-finite", f64::is_finite, make_unary_numeric);
define_with!(self, "is-nan", Num::is_nan, make_unary_numeric);
define_with!(self, "is-infinite", Num::is_infinite, make_unary_numeric);
define_with!(self, "is-finite", Num::is_finite, make_unary_numeric);
define_with!(
self,
"is-positive",
f64::is_sign_positive,
Num::is_sign_positive,
make_unary_numeric
);
define_with!(
self,
"is-negative",
f64::is_sign_negative,
Num::is_sign_negative,
make_unary_numeric
);

// rounding etc.
define_with!(self, "floor", f64::floor, make_unary_numeric);
define_with!(self, "ceil", f64::ceil, make_unary_numeric);
define_with!(self, "round", f64::round, make_unary_numeric);
define_with!(self, "trunc", f64::trunc, make_unary_numeric);
define_with!(self, "fract", f64::fract, make_unary_numeric);
define_with!(self, "sign", f64::signum, make_unary_numeric);
define_with!(self, "floor", Num::floor, make_unary_numeric);
define_with!(self, "ceil", Num::ceil, make_unary_numeric);
define_with!(self, "round", Num::round, make_unary_numeric);
define_with!(self, "trunc", Num::trunc, make_unary_numeric);
define_with!(self, "fract", Num::fract, make_unary_numeric);
define_with!(self, "sign", Num::signum, make_unary_numeric);

// exponents, roots, and logs
define_with!(self, "recip", f64::recip, make_unary_numeric);
define_with!(self, "sqrt", f64::sqrt, make_unary_numeric);
define_with!(self, "cube-root", f64::cbrt, make_unary_numeric);
define_with!(self, "exp", f64::exp, make_unary_numeric);
define_with!(self, "log", f64::ln, make_unary_numeric);
define_with!(self, "exp-2", f64::exp2, make_unary_numeric);
define_with!(self, "log-2", f64::log2, make_unary_numeric);
define_with!(self, "log-10", f64::log10, make_unary_numeric);
define_with!(self, "log-n", f64::log, make_binary_numeric);
define_with!(self, "recip", Num::recip, make_unary_numeric);
define_with!(self, "sqrt", Num::sqrt, make_unary_numeric);
define_with!(self, "cube-root", Num::cbrt, make_unary_numeric);
define_with!(self, "exp", Num::exp, make_unary_numeric);
define_with!(self, "log", Num::ln, make_unary_numeric);
define_with!(self, "exp-2", Num::exp2, make_unary_numeric);
define_with!(self, "log-2", Num::log2, make_unary_numeric);
define_with!(self, "log-10", Num::log10, make_unary_numeric);
define_with!(self, "log-n", Num::log, make_binary_numeric);

// trigonometry
define_with!(self, "hypot", f64::hypot, make_binary_numeric);
define_with!(self, "sin", f64::sin, make_unary_numeric);
define_with!(self, "cos", f64::cos, make_unary_numeric);
define_with!(self, "tan", f64::tan, make_unary_numeric);
define_with!(self, "asin", f64::asin, make_unary_numeric);
define_with!(self, "acos", f64::acos, make_unary_numeric);
define_with!(self, "atan", f64::atan, make_unary_numeric);
define_with!(self, "atan2", f64::atan2, make_binary_numeric);
define_with!(self, "hypot", Num::hypot, make_binary_numeric);
define_with!(self, "sin", Num::sin, make_unary_numeric);
define_with!(self, "cos", Num::cos, make_unary_numeric);
define_with!(self, "tan", Num::tan, make_unary_numeric);
define_with!(self, "asin", Num::asin, make_unary_numeric);
define_with!(self, "acos", Num::acos, make_unary_numeric);
define_with!(self, "atan", Num::atan, make_unary_numeric);
define_with!(self, "atan2", Num::atan2, make_binary_numeric);

// unit conversions
define_with!(self, "to-degrees", f64::to_degrees, make_unary_numeric);
define_with!(self, "to-radians", f64::to_radians, make_unary_numeric);
define_with!(self, "to-degrees", Num::to_degrees, make_unary_numeric);
define_with!(self, "to-radians", Num::to_radians, make_unary_numeric);

self
}
Expand Down
2 changes: 1 addition & 1 deletion src/ctx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use super::{Cont, Env, Ns, Primitive, Proc, Result, SExp};

mod base;
mod core;
// mod math;
mod math;
mod write;

/// Evaluation context for LISP expressions.
Expand Down
165 changes: 165 additions & 0 deletions src/primitives/num.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,171 @@ impl Num {
(Float(f0), Float(f1)) => Float(f0.powf(f1)),
}
}

pub fn is_nan(self) -> bool {
match self {
Float(f) => f.is_nan(),
_ => false,
}
}

pub fn is_infinite(self) -> bool {
match self {
Float(f) => f.is_infinite(),
_ => false,
}
}

pub fn is_finite(self) -> bool {
match self {
Float(f) => f.is_finite(),
_ => false,
}
}

pub fn is_sign_positive(self) -> bool {
match self {
Float(f) => f.is_sign_positive(),
Int(i) => i.is_positive(),
}
}

pub fn is_sign_negative(self) -> bool {
match self {
Float(f) => f.is_sign_negative(),
Int(i) => i.is_negative(),
}
}

pub fn floor(self) -> Self {
match self {
Float(f) => Int(f.floor() as IntT),
_ => self,
}
}

pub fn ceil(self) -> Self {
match self {
Float(f) => Int(f.ceil() as IntT),
_ => self,
}
}

pub fn round(self) -> Self {
match self {
Float(f) => Int(f.round() as IntT),
_ => self,
}
}

pub fn trunc(self) -> Self {
match self {
Float(f) => Int(f.trunc() as IntT),
_ => self,
}
}

pub fn fract(self) -> Self {
match self {
Float(f) => Float(f.fract()),
_ => Int(0),
}
}

pub fn signum(self) -> Self {
match self {
Float(f) => Int(f.signum() as IntT),
Int(i) => Int(i.signum()),
}
}

pub fn recip(self) -> Self {
Float(f64::from(self).recip())
}

pub fn sqrt(self) -> Self {
Float(f64::from(self).sqrt())
}

pub fn cbrt(self) -> Self {
Float(f64::from(self).cbrt())
}

pub fn exp(self) -> Self {
Float(f64::from(self).exp())
}

pub fn ln(self) -> Self {
Float(f64::from(self).ln())
}

pub fn exp2(self) -> Self {
match self {
Float(f) => Float(f.exp2()),
Int(i) => Int((2 as IntT).pow(i as u32))
}
}

pub fn log2(self) -> Self {
Float(f64::from(self).log2())
}

pub fn log10(self) -> Self {
Float(f64::from(self).log10())
}

pub fn log<T>(self, other: T) -> Self
where
Self: From<T>,
{
Float(f64::from(self).log(f64::from(Self::from(other))))
}

pub fn hypot<T>(self, other: T) -> Self
where
Self: From<T>,
{
Float(f64::from(self).hypot(f64::from(Self::from(other))))
}

pub fn sin(self) -> Self {
Float(f64::from(self).sin())
}

pub fn cos(self) -> Self {
Float(f64::from(self).cos())
}

pub fn tan(self) -> Self {
Float(f64::from(self).tan())
}

pub fn asin(self) -> Self {
Float(f64::from(self).asin())
}

pub fn acos(self) -> Self {
Float(f64::from(self).acos())
}

pub fn atan(self) -> Self {
Float(f64::from(self).atan())
}

pub fn atan2<T>(self, other: T) -> Self
where
Self: From<T>,
{
Float(f64::from(self).atan2(f64::from(Self::from(other))))
}

pub fn to_degrees(self) -> Self {
Float(f64::from(self).to_degrees())
}

pub fn to_radians(self) -> Self {
Float(f64::from(self).to_radians())
}
}

impl FromStr for Num {
Expand Down

0 comments on commit b991606

Please sign in to comment.