diatom_std_core/math/
mod.rs1use super::*;
2
3macro_rules! math_op_float {
4 ($funcs: ident, $name: ident) => {
5 $funcs.insert(
6 stringify!($name).to_string(),
7 Arc::new(|_, parameters, _| {
8 assure_para_len!(parameters, 1);
9 let f = match parameters[0] {
10 DiatomValue::Int(i) => i as f64,
11 DiatomValue::Float(f) => f,
12 _ => return Err("Expected `Int` or `Float to operate!`".to_string()),
13 };
14 Ok(DiatomValue::Float(f.$name()))
15 }),
16 );
17 };
18}
19
20pub fn math_extension<Buffer: IoWrite>() -> Extension<Buffer> {
21 let mut funcs: AHashMap<String, Arc<ForeignFunction<Buffer>>> = AHashMap::default();
22 math_op_float!(funcs, sqrt);
23 math_op_float!(funcs, cbrt);
24 math_op_float!(funcs, sin);
25 math_op_float!(funcs, cos);
26 math_op_float!(funcs, tan);
27 math_op_float!(funcs, sinh);
28 math_op_float!(funcs, cosh);
29 math_op_float!(funcs, tanh);
30 math_op_float!(funcs, ln);
31 math_op_float!(funcs, log2);
32 math_op_float!(funcs, log10);
33 math_op_float!(funcs, asin);
34 math_op_float!(funcs, acos);
35 math_op_float!(funcs, atan);
36 math_op_float!(funcs, asinh);
37 math_op_float!(funcs, acosh);
38 math_op_float!(funcs, atanh);
39 Extension {
40 name: "math".to_string(),
41 kind: ExtensionKind::ForeignFunctions(funcs),
42 }
43}