string_calculator 0.2.1

A string calculator to compute formulas inside strings.
Documentation

String Calculator

A small package containing eval methods to compute formulas inside strings.

How to use

Simply import the eval you need and use it.

use string_templater::eval_f64;

fn main() {
  println!("{}", eval_f64("(2+3) / 2".to_string(), 0.0)); // 2.5
}

Operators

Since there's a lot of things that could be simplified visually, here's the full list of operators implemented.

  1. Add (x+y) This operator handle the addition between two number. Example: 1 + 2 = 3
  2. Subtract (x-y) This operator handle the subtraction between two number. Example: 2 - 1 = 1
  3. Multiply (x*y) This operator handle the multiplication between two number. Example: 3 * 2 = 6
  4. Divide (x/y) This operator handle the division between two number. Example: 4 / 2 = 2
  5. Modulo (x%y) This operator handle the rest of the euclidian division between two number. Example: 4 % 2 = 0
  6. Left Shift (x<<y) (only in eval_i64) This operator shift the bits of x to y positions to the left (it's the same as multiplying by 2^y). Example: 1 << 2 = 4
  7. Right Shift (x>>y) (only in eval_i64) This operator shift the bits of x to y positions to the right (it's the same as dividing by 2^y). Example: 4 >> 2 = 1
  8. PowerOf (x^y) This operator handle the power of x by y, x and y being both numbers. Example: 3^3 = 27
  9. Subscript support (x²) This operator handle the power of x by using the superscript notation (such as ²) for integers, x being a number. Example: = 25
  10. Factorial (x!) This operator handle the factorial of a real x. Example: 5! = 120
  11. DegToRad (x°) (only in eval_f64) This operator handle the conversion from degree to radian. You should note that it's priority is the same as multiplication. Example: = 0.05235987755982989
  12. RadToDeg (x rad) (only in eval_f64) This operator handle the conversion from radian to degree. You should note that it's priority is the same as multiplication. Example: 3 rad = 171.8873385393

Function notation

Some function can be written purely using their original mathematical notation if wanted.

  1. Floor (⌊x⌋) (only in eval_f64) This function gives the greatest integer less than or equal to x. Example: ⌊2.4⌋ = 2
  2. Ceiling (⌈x⌉) (only in eval_f64) This function gives the smallest integer greater or equal to x. Example: ⌈2.4⌉ = 3

Functions

  1. Absolute value (abs(x))
  2. Signum (sgn(x), sign(x), signum(x))
  3. Power (pow(x,y))
  4. Square root (sqrt(x))
  5. Root (root(x))
  6. Modulo (mod(x,y))
  7. Exponential (exp(x), exp2(x))
  8. Logarithm (ln(x), log(x, b))
  9. Extremum (min(...X), max(...X))
  10. Avg (avg(...X))
  11. Median (median(...X), med(...X))
  12. Truncate (trunc(x), truncate(x)) (only in eval_f64)
  13. Floor (floor(x)) (only in eval_f64)
  14. Ceil (ceil(x)) (only in eval_f64)
  15. Round (round(x)) (only in eval_f64)
  16. Sin (sin(θ)) (only in eval_f64)
  17. Asin (asin(x)) (only in eval_f64)
  18. cos (cos(θ)) (only in eval_f64)
  19. Acos (acos(x)) (only in eval_f64)
  20. Tan (tan(θ)) (only in eval_f64)
  21. Atan (atan(x)) (only in eval_f64)
  22. Sinh (sinh(θ)) (only in eval_f64)
  23. Asinh (asinh(x), arsinh(x)) (only in eval_f64)
  24. Cosh (cosh(θ)) (only in eval_f64)
  25. Acosh (acosh(x), arcosh(x)) (only in eval_f64)
  26. Tanh (tanh(θ)) (only in eval_f64)
  27. Atanh (atanh(x), artanh(x)) (only in eval_f64)
  28. Atan 2 (atan2(y, x)) (only in eval_f64)

Placeholder Getter

In the case you're writting a calculator, it might be useful to use your previous answer for example. The @ symbol is used here as a placeholder for the value you want to put into the eval_f64.