|
|
|
@ -6,7 +6,9 @@ use std::f64;
|
|
|
|
|
/// Get the absolute value of a number
|
|
|
|
|
pub fn abs(args: Vec<Value>, _: Value, _: Value, _: Value) -> ResultValue { |
|
|
|
|
Ok(to_value(if args.len() >= 1 { |
|
|
|
|
from_value::<f64>(*args.get(0)).unwrap().abs() |
|
|
|
|
from_value::<f64>(args.get(0).unwrap().clone()) |
|
|
|
|
.unwrap() |
|
|
|
|
.abs() |
|
|
|
|
} else { |
|
|
|
|
f64::NAN |
|
|
|
|
})) |
|
|
|
@ -14,7 +16,9 @@ pub fn abs(args: Vec<Value>, _: Value, _: Value, _: Value) -> ResultValue {
|
|
|
|
|
/// Get the arccos of a number
|
|
|
|
|
pub fn acos(args: Vec<Value>, _: Value, _: Value, _: Value) -> ResultValue { |
|
|
|
|
Ok(to_value(if args.len() >= 1 { |
|
|
|
|
from_value::<f64>(*args.get(0)).unwrap().acos() |
|
|
|
|
from_value::<f64>(args.get(0).unwrap().clone()) |
|
|
|
|
.unwrap() |
|
|
|
|
.acos() |
|
|
|
|
} else { |
|
|
|
|
f64::NAN |
|
|
|
|
})) |
|
|
|
@ -22,7 +26,9 @@ pub fn acos(args: Vec<Value>, _: Value, _: Value, _: Value) -> ResultValue {
|
|
|
|
|
/// Get the arcsine of a number
|
|
|
|
|
pub fn asin(args: Vec<Value>, _: Value, _: Value, _: Value) -> ResultValue { |
|
|
|
|
Ok(to_value(if args.len() >= 1 { |
|
|
|
|
from_value::<f64>(*args.get(0)).unwrap().asin() |
|
|
|
|
from_value::<f64>(args.get(0).unwrap().clone()) |
|
|
|
|
.unwrap() |
|
|
|
|
.asin() |
|
|
|
|
} else { |
|
|
|
|
f64::NAN |
|
|
|
|
})) |
|
|
|
@ -30,7 +36,9 @@ pub fn asin(args: Vec<Value>, _: Value, _: Value, _: Value) -> ResultValue {
|
|
|
|
|
/// Get the arctangent of a number
|
|
|
|
|
pub fn atan(args: Vec<Value>, _: Value, _: Value, _: Value) -> ResultValue { |
|
|
|
|
Ok(to_value(if args.len() >= 1 { |
|
|
|
|
from_value::<f64>(*args.get(0)).unwrap().atan() |
|
|
|
|
from_value::<f64>(args.get(0).unwrap().clone()) |
|
|
|
|
.unwrap() |
|
|
|
|
.atan() |
|
|
|
|
} else { |
|
|
|
|
f64::NAN |
|
|
|
|
})) |
|
|
|
@ -38,9 +46,9 @@ pub fn atan(args: Vec<Value>, _: Value, _: Value, _: Value) -> ResultValue {
|
|
|
|
|
/// Get the arctangent of a numbers
|
|
|
|
|
pub fn atan2(args: Vec<Value>, _: Value, _: Value, _: Value) -> ResultValue { |
|
|
|
|
Ok(to_value(if args.len() >= 1 { |
|
|
|
|
from_value::<f64>(*args.get(0)) |
|
|
|
|
from_value::<f64>(args.get(0).unwrap().clone()) |
|
|
|
|
.unwrap() |
|
|
|
|
.atan2(args.get(1).to_num()) |
|
|
|
|
.atan2(args.get(1).unwrap().to_num()) |
|
|
|
|
} else { |
|
|
|
|
f64::NAN |
|
|
|
|
})) |
|
|
|
@ -48,7 +56,9 @@ pub fn atan2(args: Vec<Value>, _: Value, _: Value, _: Value) -> ResultValue {
|
|
|
|
|
/// Get the cubic root of a number
|
|
|
|
|
pub fn cbrt(args: Vec<Value>, _: Value, _: Value, _: Value) -> ResultValue { |
|
|
|
|
Ok(to_value(if args.len() >= 1 { |
|
|
|
|
from_value::<f64>(*args.get(0)).unwrap().cbrt() |
|
|
|
|
from_value::<f64>(args.get(0).unwrap().clone()) |
|
|
|
|
.unwrap() |
|
|
|
|
.cbrt() |
|
|
|
|
} else { |
|
|
|
|
f64::NAN |
|
|
|
|
})) |
|
|
|
@ -56,7 +66,9 @@ pub fn cbrt(args: Vec<Value>, _: Value, _: Value, _: Value) -> ResultValue {
|
|
|
|
|
/// Get lowest integer above a number
|
|
|
|
|
pub fn ceil(args: Vec<Value>, _: Value, _: Value, _: Value) -> ResultValue { |
|
|
|
|
Ok(to_value(if args.len() >= 1 { |
|
|
|
|
from_value::<f64>(*args.get(0)).unwrap().ceil() |
|
|
|
|
from_value::<f64>(args.get(0).unwrap().clone()) |
|
|
|
|
.unwrap() |
|
|
|
|
.ceil() |
|
|
|
|
} else { |
|
|
|
|
f64::NAN |
|
|
|
|
})) |
|
|
|
@ -64,7 +76,9 @@ pub fn ceil(args: Vec<Value>, _: Value, _: Value, _: Value) -> ResultValue {
|
|
|
|
|
/// Get the cosine of a number
|
|
|
|
|
pub fn cos(args: Vec<Value>, _: Value, _: Value, _: Value) -> ResultValue { |
|
|
|
|
Ok(to_value(if args.len() >= 1 { |
|
|
|
|
from_value::<f64>(*args.get(0)).unwrap().cos() |
|
|
|
|
from_value::<f64>(args.get(0).unwrap().clone()) |
|
|
|
|
.unwrap() |
|
|
|
|
.cos() |
|
|
|
|
} else { |
|
|
|
|
f64::NAN |
|
|
|
|
})) |
|
|
|
@ -72,7 +86,9 @@ pub fn cos(args: Vec<Value>, _: Value, _: Value, _: Value) -> ResultValue {
|
|
|
|
|
/// Get the power to raise the natural logarithm to get the number
|
|
|
|
|
pub fn exp(args: Vec<Value>, _: Value, _: Value, _: Value) -> ResultValue { |
|
|
|
|
Ok(to_value(if args.len() >= 1 { |
|
|
|
|
from_value::<f64>(*args.get(0)).unwrap().exp() |
|
|
|
|
from_value::<f64>(args.get(0).unwrap().clone()) |
|
|
|
|
.unwrap() |
|
|
|
|
.exp() |
|
|
|
|
} else { |
|
|
|
|
f64::NAN |
|
|
|
|
})) |
|
|
|
@ -80,7 +96,9 @@ pub fn exp(args: Vec<Value>, _: Value, _: Value, _: Value) -> ResultValue {
|
|
|
|
|
/// Get the highest integer below a number
|
|
|
|
|
pub fn floor(args: Vec<Value>, _: Value, _: Value, _: Value) -> ResultValue { |
|
|
|
|
Ok(to_value(if args.len() >= 1 { |
|
|
|
|
from_value::<f64>(*args.get(0)).unwrap().floor() |
|
|
|
|
from_value::<f64>(args.get(0).unwrap().clone()) |
|
|
|
|
.unwrap() |
|
|
|
|
.floor() |
|
|
|
|
} else { |
|
|
|
|
f64::NAN |
|
|
|
|
})) |
|
|
|
@ -88,7 +106,9 @@ pub fn floor(args: Vec<Value>, _: Value, _: Value, _: Value) -> ResultValue {
|
|
|
|
|
/// Get the natural logarithm of a number
|
|
|
|
|
pub fn log(args: Vec<Value>, _: Value, _: Value, _: Value) -> ResultValue { |
|
|
|
|
Ok(to_value(if args.len() >= 1 { |
|
|
|
|
from_value::<f64>(*args.get(0)).unwrap().log(f64::consts::E) |
|
|
|
|
from_value::<f64>(args.get(0).unwrap().clone()) |
|
|
|
|
.unwrap() |
|
|
|
|
.log(f64::consts::E) |
|
|
|
|
} else { |
|
|
|
|
f64::NAN |
|
|
|
|
})) |
|
|
|
@ -114,8 +134,8 @@ pub fn min(args: Vec<Value>, _: Value, _: Value, _: Value) -> ResultValue {
|
|
|
|
|
/// Raise a number to a power
|
|
|
|
|
pub fn pow(args: Vec<Value>, _: Value, _: Value, _: Value) -> ResultValue { |
|
|
|
|
Ok(to_value(if args.len() >= 2 { |
|
|
|
|
let num: f64 = from_value(*args.get(0)).unwrap(); |
|
|
|
|
let power: f64 = from_value(*args.get(1)).unwrap(); |
|
|
|
|
let num: f64 = from_value(args.get(0).unwrap().clone()).unwrap(); |
|
|
|
|
let power: f64 = from_value(args.get(1).unwrap().clone()).unwrap(); |
|
|
|
|
num.powf(power) |
|
|
|
|
} else { |
|
|
|
|
f64::NAN |
|
|
|
@ -128,7 +148,9 @@ pub fn _random(_: Vec<Value>, _: Value, _: Value, _: Value) -> ResultValue {
|
|
|
|
|
/// Round a number to the nearest integer
|
|
|
|
|
pub fn round(args: Vec<Value>, _: Value, _: Value, _: Value) -> ResultValue { |
|
|
|
|
Ok(to_value(if args.len() >= 1 { |
|
|
|
|
from_value::<f64>(*args.get(0)).unwrap().round() |
|
|
|
|
from_value::<f64>(args.get(0).unwrap().clone()) |
|
|
|
|
.unwrap() |
|
|
|
|
.round() |
|
|
|
|
} else { |
|
|
|
|
f64::NAN |
|
|
|
|
})) |
|
|
|
@ -136,7 +158,9 @@ pub fn round(args: Vec<Value>, _: Value, _: Value, _: Value) -> ResultValue {
|
|
|
|
|
/// Get the sine of a number
|
|
|
|
|
pub fn sin(args: Vec<Value>, _: Value, _: Value, _: Value) -> ResultValue { |
|
|
|
|
Ok(to_value(if args.len() >= 1 { |
|
|
|
|
from_value::<f64>(*args.get(0)).unwrap().sin() |
|
|
|
|
from_value::<f64>(args.get(0).unwrap().clone()) |
|
|
|
|
.unwrap() |
|
|
|
|
.sin() |
|
|
|
|
} else { |
|
|
|
|
f64::NAN |
|
|
|
|
})) |
|
|
|
@ -144,7 +168,9 @@ pub fn sin(args: Vec<Value>, _: Value, _: Value, _: Value) -> ResultValue {
|
|
|
|
|
/// Get the square root of a number
|
|
|
|
|
pub fn sqrt(args: Vec<Value>, _: Value, _: Value, _: Value) -> ResultValue { |
|
|
|
|
Ok(to_value(if args.len() >= 1 { |
|
|
|
|
from_value::<f64>(*args.get(0)).unwrap().sqrt() |
|
|
|
|
from_value::<f64>(args.get(0).unwrap().clone()) |
|
|
|
|
.unwrap() |
|
|
|
|
.sqrt() |
|
|
|
|
} else { |
|
|
|
|
f64::NAN |
|
|
|
|
})) |
|
|
|
@ -152,7 +178,9 @@ pub fn sqrt(args: Vec<Value>, _: Value, _: Value, _: Value) -> ResultValue {
|
|
|
|
|
/// Get the tangent of a number
|
|
|
|
|
pub fn tan(args: Vec<Value>, _: Value, _: Value, _: Value) -> ResultValue { |
|
|
|
|
Ok(to_value(if args.len() >= 1 { |
|
|
|
|
from_value::<f64>(*args.get(0)).unwrap().tan() |
|
|
|
|
from_value::<f64>(args.get(0).unwrap().clone()) |
|
|
|
|
.unwrap() |
|
|
|
|
.tan() |
|
|
|
|
} else { |
|
|
|
|
f64::NAN |
|
|
|
|
})) |
|
|
|
@ -166,30 +194,30 @@ pub fn _create(global: Value) -> Value {
|
|
|
|
|
math.set_field_slice("LOG2E", to_value(f64::consts::LOG2_E)); |
|
|
|
|
math.set_field_slice("LOG10E", to_value(f64::consts::LOG10_E)); |
|
|
|
|
math.set_field_slice("SQRT1_2", to_value(0.5f64.sqrt())); |
|
|
|
|
math.set_field_slice("SQRT2", to_value(f64::consts::SQRT2)); |
|
|
|
|
math.set_field_slice("SQRT2", to_value(f64::consts::SQRT_2)); |
|
|
|
|
math.set_field_slice("PI", to_value(f64::consts::PI)); |
|
|
|
|
math.set_field_slice("abs", Function::make(abs, ["num1", "num2"])); |
|
|
|
|
math.set_field_slice("acos", Function::make(acos, ["num1", "num2"])); |
|
|
|
|
math.set_field_slice("asin", Function::make(asin, ["num1", "num2"])); |
|
|
|
|
math.set_field_slice("atan", Function::make(atan, ["num1", "num2"])); |
|
|
|
|
math.set_field_slice("atan2", Function::make(atan2, ["num1", "num2"])); |
|
|
|
|
math.set_field_slice("cbrt", Function::make(cbrt, ["num1", "num2"])); |
|
|
|
|
math.set_field_slice("ceil", Function::make(ceil, ["num1", "num2"])); |
|
|
|
|
math.set_field_slice("cos", Function::make(cos, ["num1", "num2"])); |
|
|
|
|
math.set_field_slice("exp", Function::make(exp, ["num1", "num2"])); |
|
|
|
|
math.set_field_slice("floor", Function::make(floor, ["num"])); |
|
|
|
|
math.set_field_slice("log", Function::make(log, ["num1", "num2"])); |
|
|
|
|
math.set_field_slice("max", Function::make(max, ["num1", "num2"])); |
|
|
|
|
math.set_field_slice("min", Function::make(min, ["num1", "num2"])); |
|
|
|
|
math.set_field_slice("pow", Function::make(pow, ["num1", "num2"])); |
|
|
|
|
math.set_field_slice("random", Function::make(_random, [])); |
|
|
|
|
math.set_field_slice("round", Function::make(round, ["num"])); |
|
|
|
|
math.set_field_slice("sin", Function::make(sin, ["num"])); |
|
|
|
|
math.set_field_slice("sqrt", Function::make(sqrt, ["num"])); |
|
|
|
|
math.set_field_slice("tan", Function::make(tan, ["num"])); |
|
|
|
|
math.set_field_slice("abs", Function::make(abs, &["num1", "num2"])); |
|
|
|
|
math.set_field_slice("acos", Function::make(acos, &["num1", "num2"])); |
|
|
|
|
math.set_field_slice("asin", Function::make(asin, &["num1", "num2"])); |
|
|
|
|
math.set_field_slice("atan", Function::make(atan, &["num1", "num2"])); |
|
|
|
|
math.set_field_slice("atan2", Function::make(atan2, &["num1", "num2"])); |
|
|
|
|
math.set_field_slice("cbrt", Function::make(cbrt, &["num1", "num2"])); |
|
|
|
|
math.set_field_slice("ceil", Function::make(ceil, &["num1", "num2"])); |
|
|
|
|
math.set_field_slice("cos", Function::make(cos, &["num1", "num2"])); |
|
|
|
|
math.set_field_slice("exp", Function::make(exp, &["num1", "num2"])); |
|
|
|
|
math.set_field_slice("floor", Function::make(floor, &["num"])); |
|
|
|
|
math.set_field_slice("log", Function::make(log, &["num1", "num2"])); |
|
|
|
|
math.set_field_slice("max", Function::make(max, &["num1", "num2"])); |
|
|
|
|
math.set_field_slice("min", Function::make(min, &["num1", "num2"])); |
|
|
|
|
math.set_field_slice("pow", Function::make(pow, &["num1", "num2"])); |
|
|
|
|
math.set_field_slice("random", Function::make(_random, &[])); |
|
|
|
|
math.set_field_slice("round", Function::make(round, &["num"])); |
|
|
|
|
math.set_field_slice("sin", Function::make(sin, &["num"])); |
|
|
|
|
math.set_field_slice("sqrt", Function::make(sqrt, &["num"])); |
|
|
|
|
math.set_field_slice("tan", Function::make(tan, &["num"])); |
|
|
|
|
math |
|
|
|
|
} |
|
|
|
|
/// Initialise the `Math` object on the global object
|
|
|
|
|
pub fn init(global: Value) { |
|
|
|
|
global.set_field_slice("Math", _create(global)); |
|
|
|
|
global.set_field_slice("Math", _create(global.clone())); |
|
|
|
|
} |
|
|
|
|