mruby-math
A Rust implementation of the Math module for mruby/edge.
Features
This crate provides mathematical functions and constants for mruby/edge:
Constants
Math::PI- π (pi)Math::E- e (Euler's number)
Trigonometric Functions
Math.sin(x)- SineMath.cos(x)- CosineMath.tan(x)- TangentMath.asin(x)- ArcsineMath.acos(x)- ArccosineMath.atan(x)- ArctangentMath.atan2(y, x)- Arctangent of y/x
Hyperbolic Functions
Math.sinh(x)- Hyperbolic sineMath.cosh(x)- Hyperbolic cosineMath.tanh(x)- Hyperbolic tangentMath.asinh(x)- Inverse hyperbolic sineMath.acosh(x)- Inverse hyperbolic cosineMath.atanh(x)- Inverse hyperbolic tangent
Exponential and Logarithmic Functions
Math.exp(x)- e^xMath.log(x)- Natural logarithm (ln)Math.log(x, base)- Logarithm with custom baseMath.log10(x)- Base-10 logarithmMath.log2(x)- Base-2 logarithm
Root Functions
Math.sqrt(x)- Square rootMath.cbrt(x)- Cube root
Other Functions
Math.hypot(x, y)- Hypotenuse (√(x² + y²))Math.ldexp(fraction, exponent)- fraction × 2^exponentMath.erf(x)- Error functionMath.erfc(x)- Complementary error function
Usage
Add this to your Cargo.toml:
[]
= "0.1.0"
= "1.1.1"
Initialize the Math module in your VM:
use init_math;
use VM;
let mut vm = VMopen;
init_math;
Example
# Calculate sine wave
x = Math::PI / 4
y = Math.sin(x)
# Calculate distance
distance = Math.hypot(3, 4) # => 5.0
# Exponential growth
result = Math.exp(2) # => e^2
License
BSD-3-Clause