Skip to content

Commit 727b895

Browse files
committed
Use py_module for math.
1 parent 0175780 commit 727b895

File tree

1 file changed

+51
-53
lines changed

1 file changed

+51
-53
lines changed

vm/src/stdlib/math.rs

Lines changed: 51 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -172,57 +172,55 @@ fn math_lgamma(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
172172
}
173173

174174
pub fn mk_module(ctx: &PyContext) -> PyObjectRef {
175-
let py_mod = ctx.new_module(&"math".to_string(), ctx.new_scope(None));
176-
177-
// Number theory functions:
178-
ctx.set_attr(&py_mod, "fabs", ctx.new_rustfunc(math_fabs));
179-
ctx.set_attr(&py_mod, "isfinite", ctx.new_rustfunc(math_isfinite));
180-
ctx.set_attr(&py_mod, "isinf", ctx.new_rustfunc(math_isinf));
181-
ctx.set_attr(&py_mod, "isnan", ctx.new_rustfunc(math_isnan));
182-
183-
// Power and logarithmic functions:
184-
ctx.set_attr(&py_mod, "exp", ctx.new_rustfunc(math_exp));
185-
ctx.set_attr(&py_mod, "expm1", ctx.new_rustfunc(math_expm1));
186-
ctx.set_attr(&py_mod, "log", ctx.new_rustfunc(math_log));
187-
ctx.set_attr(&py_mod, "log1p", ctx.new_rustfunc(math_log1p));
188-
ctx.set_attr(&py_mod, "log2", ctx.new_rustfunc(math_log2));
189-
ctx.set_attr(&py_mod, "log10", ctx.new_rustfunc(math_log10));
190-
ctx.set_attr(&py_mod, "pow", ctx.new_rustfunc(math_pow));
191-
ctx.set_attr(&py_mod, "sqrt", ctx.new_rustfunc(math_sqrt));
192-
193-
// Trigonometric functions:
194-
ctx.set_attr(&py_mod, "acos", ctx.new_rustfunc(math_acos));
195-
ctx.set_attr(&py_mod, "asin", ctx.new_rustfunc(math_asin));
196-
ctx.set_attr(&py_mod, "atan", ctx.new_rustfunc(math_atan));
197-
ctx.set_attr(&py_mod, "atan2", ctx.new_rustfunc(math_atan2));
198-
ctx.set_attr(&py_mod, "cos", ctx.new_rustfunc(math_cos));
199-
ctx.set_attr(&py_mod, "hypot", ctx.new_rustfunc(math_hypot));
200-
ctx.set_attr(&py_mod, "sin", ctx.new_rustfunc(math_sin));
201-
ctx.set_attr(&py_mod, "tan", ctx.new_rustfunc(math_tan));
202-
203-
ctx.set_attr(&py_mod, "degrees", ctx.new_rustfunc(math_degrees));
204-
ctx.set_attr(&py_mod, "radians", ctx.new_rustfunc(math_radians));
205-
206-
// Hyperbolic functions:
207-
ctx.set_attr(&py_mod, "acosh", ctx.new_rustfunc(math_acosh));
208-
ctx.set_attr(&py_mod, "asinh", ctx.new_rustfunc(math_asinh));
209-
ctx.set_attr(&py_mod, "atanh", ctx.new_rustfunc(math_atanh));
210-
ctx.set_attr(&py_mod, "cosh", ctx.new_rustfunc(math_cosh));
211-
ctx.set_attr(&py_mod, "sinh", ctx.new_rustfunc(math_sinh));
212-
ctx.set_attr(&py_mod, "tanh", ctx.new_rustfunc(math_tanh));
213-
214-
// Special functions:
215-
ctx.set_attr(&py_mod, "erf", ctx.new_rustfunc(math_erf));
216-
ctx.set_attr(&py_mod, "erfc", ctx.new_rustfunc(math_erfc));
217-
ctx.set_attr(&py_mod, "gamma", ctx.new_rustfunc(math_gamma));
218-
ctx.set_attr(&py_mod, "lgamma", ctx.new_rustfunc(math_lgamma));
219-
220-
// Constants:
221-
ctx.set_attr(&py_mod, "pi", ctx.new_float(std::f64::consts::PI)); // 3.14159...
222-
ctx.set_attr(&py_mod, "e", ctx.new_float(std::f64::consts::E)); // 2.71..
223-
ctx.set_attr(&py_mod, "tau", ctx.new_float(2.0 * std::f64::consts::PI));
224-
ctx.set_attr(&py_mod, "inf", ctx.new_float(std::f64::INFINITY));
225-
ctx.set_attr(&py_mod, "nan", ctx.new_float(std::f64::NAN));
226-
227-
py_mod
175+
py_module!(ctx, "math", {
176+
// Number theory functions:
177+
"fabs" => ctx.new_rustfunc(math_fabs),
178+
"isfinite" => ctx.new_rustfunc(math_isfinite),
179+
"isinf" => ctx.new_rustfunc(math_isinf),
180+
"isnan" => ctx.new_rustfunc(math_isnan),
181+
182+
// Power and logarithmic functions:
183+
"exp" => ctx.new_rustfunc(math_exp),
184+
"expm1" => ctx.new_rustfunc(math_expm1),
185+
"log" => ctx.new_rustfunc(math_log),
186+
"log1p" => ctx.new_rustfunc(math_log1p),
187+
"log2" => ctx.new_rustfunc(math_log2),
188+
"log10" => ctx.new_rustfunc(math_log10),
189+
"pow" => ctx.new_rustfunc(math_pow),
190+
"sqrt" => ctx.new_rustfunc(math_sqrt),
191+
192+
// Trigonometric functions:
193+
"acos" => ctx.new_rustfunc(math_acos),
194+
"asin" => ctx.new_rustfunc(math_asin),
195+
"atan" => ctx.new_rustfunc(math_atan),
196+
"atan2" => ctx.new_rustfunc(math_atan2),
197+
"cos" => ctx.new_rustfunc(math_cos),
198+
"hypot" => ctx.new_rustfunc(math_hypot),
199+
"sin" => ctx.new_rustfunc(math_sin),
200+
"tan" => ctx.new_rustfunc(math_tan),
201+
202+
"degrees" => ctx.new_rustfunc(math_degrees),
203+
"radians" => ctx.new_rustfunc(math_radians),
204+
205+
// Hyperbolic functions:
206+
"acosh" => ctx.new_rustfunc(math_acosh),
207+
"asinh" => ctx.new_rustfunc(math_asinh),
208+
"atanh" => ctx.new_rustfunc(math_atanh),
209+
"cosh" => ctx.new_rustfunc(math_cosh),
210+
"sinh" => ctx.new_rustfunc(math_sinh),
211+
"tanh" => ctx.new_rustfunc(math_tanh),
212+
213+
// Special functions:
214+
"erf" => ctx.new_rustfunc(math_erf),
215+
"erfc" => ctx.new_rustfunc(math_erfc),
216+
"gamma" => ctx.new_rustfunc(math_gamma),
217+
"lgamma" => ctx.new_rustfunc(math_lgamma),
218+
219+
// Constants:
220+
"pi" => ctx.new_float(std::f64::consts::PI), // 3.14159...
221+
"e" => ctx.new_float(std::f64::consts::E), // 2.71..
222+
"tau" => ctx.new_float(2.0 * std::f64::consts::PI),
223+
"inf" => ctx.new_float(std::f64::INFINITY),
224+
"nan" => ctx.new_float(std::f64::NAN)
225+
})
228226
}

0 commit comments

Comments
 (0)