Num

Trait Num 

Source
pub trait Num:
    Debug
    + Display
    + Clone
    + PartialEq
where Self: Sized,
{
Show 23 methods // Required methods fn from_f64(t: f64, ctx: &Context<Self>) -> Calculation<Self>; fn from_f64_complex(t: (f64, f64), ctx: &Context<Self>) -> Calculation<Self>; fn typename() -> String; // Provided methods fn tryord( &self, _other: &Self, _ctx: &Context<Self>, ) -> Result<Ordering, MathError> { ... } fn add(&self, _other: &Self, _ctx: &Context<Self>) -> Calculation<Self> { ... } fn sub(&self, _other: &Self, _ctx: &Context<Self>) -> Calculation<Self> { ... } fn mul(&self, _other: &Self, _ctx: &Context<Self>) -> Calculation<Self> { ... } fn div(&self, _other: &Self, _ctx: &Context<Self>) -> Calculation<Self> { ... } fn pow(&self, _other: &Self, _ctx: &Context<Self>) -> Calculation<Self> { ... } fn sqrt(&self, _ctx: &Context<Self>) -> Calculation<Self> { ... } fn nrt(&self, _other: &Self, _ctx: &Context<Self>) -> Calculation<Self> { ... } fn abs(&self, _ctx: &Context<Self>) -> Calculation<Self> { ... } fn sin(&self, _ctx: &Context<Self>) -> Calculation<Self> { ... } fn cos(&self, _ctx: &Context<Self>) -> Calculation<Self> { ... } fn tan(&self, _ctx: &Context<Self>) -> Calculation<Self> { ... } fn asin(&self, _ctx: &Context<Self>) -> Calculation<Self> { ... } fn acos(&self, _ctx: &Context<Self>) -> Calculation<Self> { ... } fn atan(&self, _ctx: &Context<Self>) -> Calculation<Self> { ... } fn atan2(&self, _other: &Self, _ctx: &Context<Self>) -> Calculation<Self> { ... } fn floor(&self, _ctx: &Context<Self>) -> Calculation<Self> { ... } fn ceil(&self, _ctx: &Context<Self>) -> Calculation<Self> { ... } fn round(&self, _ctx: &Context<Self>) -> Calculation<Self> { ... } fn log(&self, _other: &Self, _ctx: &Context<Self>) -> Calculation<Self> { ... }
}
Expand description

A Num represents any type that can be used in an expression. It requires lots of operations to be implemented for it, any of which can fail, as well as the traits: Debug, Clone, Display, PartialOrd, and PartialEq.

Required Methods§

Source

fn from_f64(t: f64, ctx: &Context<Self>) -> Calculation<Self>

Attempts to create an instance of the number from an f64

Source

fn from_f64_complex(t: (f64, f64), ctx: &Context<Self>) -> Calculation<Self>

Attempts to create an instance of the number from complex parts. It’s possible the imaginary part will be ignored for Numbers that don’t support it.

Source

fn typename() -> String

Returns the name of this Num type (used for errors)

Provided Methods§

Source

fn tryord( &self, _other: &Self, _ctx: &Context<Self>, ) -> Result<Ordering, MathError>

Source

fn add(&self, _other: &Self, _ctx: &Context<Self>) -> Calculation<Self>

Examples found in repository?
examples/context.rs (line 29)
8fn main() {
9	// A context holds data that can be used in an expression
10	let mut context: Context<f64> = Context::new();
11	// Add a variable "x" to the context with the value 5.4
12	context.set_var("x", 5.4);
13	// Add a function "sum" to the context that returns the sum of it's arguments. A closure is passed
14	// in that takes twp arguments: args: &[Term], which is a slice if the arguments passed into the
15	// function, and ctx: &Context, which is a reference to the context which the expression is being
16	// evaluated with. The item passed in can be anything that implements the `Func` trait. There exists
17	// a blanket impl for Fn(&[Term], &Context) -> Calculation which allows you to pass in closures in
18	// that format.
19	context.set_func(
20		"sum",
21		|args: &[Term<f64>], ctx: &Context<f64>| -> Calculation<f64> {
22			if args.len() < 1 {
23				return Err(MathError::IncorrectArguments);
24			};
25
26			let mut sum = Answer::Single(0.0);
27			for arg in args {
28				let b = arg.eval_ctx(ctx)?;
29				sum = sum.op(&b, |a, b| Num::add(a, b, ctx))?;
30			}
31
32			Ok(sum)
33		},
34	);
35
36	let raw = "2 * sum(x, 7, 400)";
37	// The expression needs to be parsed with the context in order do decide if some names are functions
38	// or variables.
39	let expr = Expression::parse_ctx(raw, context).unwrap();
40	// The expression also needs to be evaluated with a context. This context can be different than the
41	// one it was parsed with, but if it is missing something that is necessary for evaluation the
42	// evaluation will fail.
43	println!("{} = {}", raw, expr.eval().unwrap())
44}
Source

fn sub(&self, _other: &Self, _ctx: &Context<Self>) -> Calculation<Self>

Source

fn mul(&self, _other: &Self, _ctx: &Context<Self>) -> Calculation<Self>

Source

fn div(&self, _other: &Self, _ctx: &Context<Self>) -> Calculation<Self>

Source

fn pow(&self, _other: &Self, _ctx: &Context<Self>) -> Calculation<Self>

Source

fn sqrt(&self, _ctx: &Context<Self>) -> Calculation<Self>

Source

fn nrt(&self, _other: &Self, _ctx: &Context<Self>) -> Calculation<Self>

Source

fn abs(&self, _ctx: &Context<Self>) -> Calculation<Self>

Source

fn sin(&self, _ctx: &Context<Self>) -> Calculation<Self>

Source

fn cos(&self, _ctx: &Context<Self>) -> Calculation<Self>

Source

fn tan(&self, _ctx: &Context<Self>) -> Calculation<Self>

Source

fn asin(&self, _ctx: &Context<Self>) -> Calculation<Self>

Source

fn acos(&self, _ctx: &Context<Self>) -> Calculation<Self>

Source

fn atan(&self, _ctx: &Context<Self>) -> Calculation<Self>

Source

fn atan2(&self, _other: &Self, _ctx: &Context<Self>) -> Calculation<Self>

Source

fn floor(&self, _ctx: &Context<Self>) -> Calculation<Self>

Source

fn ceil(&self, _ctx: &Context<Self>) -> Calculation<Self>

Source

fn round(&self, _ctx: &Context<Self>) -> Calculation<Self>

Source

fn log(&self, _other: &Self, _ctx: &Context<Self>) -> Calculation<Self>

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl Num for f64

Source§

fn tryord( &self, other: &Self, ctx: &Context<Self>, ) -> Result<Ordering, MathError>

Compares two floats. Errors if either is NaN. Infinity is greater than anything except equal to infinity. Negative infinity is less than anything except equal to negative infinity.

Source§

fn from_f64(t: f64, _ctx: &Context<Self>) -> Calculation<Self>

Source§

fn from_f64_complex( (r, _i): (f64, f64), _ctx: &Context<Self>, ) -> Calculation<Self>

Source§

fn typename() -> String

Source§

fn add(&self, other: &Self, _ctx: &Context<Self>) -> Calculation<Self>

Source§

fn sub(&self, other: &Self, _ctx: &Context<Self>) -> Calculation<Self>

Source§

fn mul(&self, other: &Self, _ctx: &Context<Self>) -> Calculation<Self>

Source§

fn div(&self, other: &Self, _ctx: &Context<Self>) -> Calculation<Self>

Source§

fn pow(&self, other: &Self, _ctx: &Context<Self>) -> Calculation<Self>

Source§

fn sqrt(&self, ctx: &Context<Self>) -> Calculation<Self>

Source§

fn abs(&self, _ctx: &Context<Self>) -> Calculation<Self>

Source§

fn sin(&self, _ctx: &Context<Self>) -> Calculation<Self>

Source§

fn cos(&self, _ctx: &Context<Self>) -> Calculation<Self>

Source§

fn tan(&self, _ctx: &Context<Self>) -> Calculation<Self>

Source§

fn asin(&self, _ctx: &Context<Self>) -> Calculation<Self>

Source§

fn acos(&self, _ctx: &Context<Self>) -> Calculation<Self>

Source§

fn atan(&self, _ctx: &Context<Self>) -> Calculation<Self>

Source§

fn atan2(&self, other: &Self, _ctx: &Context<Self>) -> Calculation<Self>

Source§

fn floor(&self, _ctx: &Context<Self>) -> Calculation<Self>

Source§

fn ceil(&self, _ctx: &Context<Self>) -> Calculation<Self>

Source§

fn round(&self, _ctx: &Context<Self>) -> Calculation<Self>

Source§

fn log(&self, other: &Self, _ctx: &Context<Self>) -> Calculation<Self>

Source§

impl Num for Complex

Source§

fn from_f64(t: f64, ctx: &Context<Self>) -> Calculation<Self>

Source§

fn from_f64_complex(val: (f64, f64), ctx: &Context<Self>) -> Calculation<Self>

Source§

fn typename() -> String

Source§

fn tryord( &self, other: &Self, _ctx: &Context<Self>, ) -> Result<Ordering, MathError>

Source§

fn add(&self, other: &Self, ctx: &Context<Self>) -> Calculation<Self>

Source§

fn sub(&self, other: &Self, ctx: &Context<Self>) -> Calculation<Self>

Source§

fn mul(&self, other: &Self, ctx: &Context<Self>) -> Calculation<Self>

Source§

fn div(&self, other: &Self, ctx: &Context<Self>) -> Calculation<Self>

Source§

fn pow(&self, other: &Self, ctx: &Context<Self>) -> Calculation<Self>

Source§

fn sqrt(&self, ctx: &Context<Self>) -> Calculation<Self>

Source§

fn abs(&self, ctx: &Context<Self>) -> Calculation<Self>

Source§

fn sin(&self, ctx: &Context<Self>) -> Calculation<Self>

Source§

fn cos(&self, ctx: &Context<Self>) -> Calculation<Self>

Source§

fn tan(&self, ctx: &Context<Self>) -> Calculation<Self>

Source§

fn asin(&self, ctx: &Context<Self>) -> Calculation<Self>

Source§

fn acos(&self, ctx: &Context<Self>) -> Calculation<Self>

Source§

fn atan(&self, ctx: &Context<Self>) -> Calculation<Self>

Source§

fn floor(&self, ctx: &Context<Self>) -> Calculation<Self>

Source§

fn ceil(&self, ctx: &Context<Self>) -> Calculation<Self>

Source§

fn round(&self, ctx: &Context<Self>) -> Calculation<Self>

Source§

fn log(&self, other: &Self, ctx: &Context<Self>) -> Calculation<Self>

Source§

impl Num for Rational

Source§

fn from_f64(t: f64, _ctx: &Context<Self>) -> Calculation<Self>

Source§

fn from_f64_complex( (r, _i): (f64, f64), _ctx: &Context<Self>, ) -> Calculation<Self>

Source§

fn typename() -> String

Source§

fn tryord( &self, other: &Self, _ctx: &Context<Self>, ) -> Result<Ordering, MathError>

Source§

fn add(&self, other: &Self, _ctx: &Context<Self>) -> Calculation<Self>

Source§

fn sub(&self, other: &Self, _ctx: &Context<Self>) -> Calculation<Self>

Source§

fn mul(&self, other: &Self, _ctx: &Context<Self>) -> Calculation<Self>

Source§

fn div(&self, other: &Self, _ctx: &Context<Self>) -> Calculation<Self>

Source§

fn abs(&self, _ctx: &Context<Self>) -> Calculation<Self>

Source§

fn floor(&self, _ctx: &Context<Self>) -> Calculation<Self>

Source§

fn ceil(&self, _ctx: &Context<Self>) -> Calculation<Self>

Source§

fn round(&self, _ctx: &Context<Self>) -> Calculation<Self>

Implementors§