Skip to content

Replace puruspe to pymath #5733

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .cspell.dict/rust-more.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ powf
powi
prepended
punct
puruspe
replacen
rmatch
rposition
Expand Down
22 changes: 6 additions & 16 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ once_cell = "1.20.3"
parking_lot = "0.12.3"
paste = "1.0.15"
proc-macro2 = "1.0.93"
pymath = "0.0.2"
quote = "1.0.38"
rand = "0.9"
rand_core = { version = "0.9", features = ["os_rng"] }
Expand Down
3 changes: 2 additions & 1 deletion Lib/test/test_math.py
Original file line number Diff line number Diff line change
Expand Up @@ -1374,6 +1374,7 @@ def test_sumprod_accuracy(self):
self.assertEqual(sumprod([True, False] * 10, [0.1] * 20), 1.0)
self.assertEqual(sumprod([1.0, 10E100, 1.0, -10E100], [1.0]*4), 2.0)

@unittest.skip("TODO: RUSTPYTHON, Taking a few minutes.")
@support.requires_resource('cpu')
def test_sumprod_stress(self):
sumprod = math.sumprod
Expand Down Expand Up @@ -2079,7 +2080,7 @@ def test_testfile(self):
self.fail('Failures in test_testfile:\n ' +
'\n '.join(failures))

@unittest.skip("TODO: RUSTPYTHON, Currently hangs. Function never finishes.")
@unittest.skip("TODO: RUSTPYTHON, Taking a few minutes.")
@requires_IEEE_754
def test_mtestfile(self):
fail_fmt = "{}: {}({!r}): {}"
Expand Down
2 changes: 1 addition & 1 deletion stdlib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ memchr = { workspace = true }
base64 = "0.22"
csv-core = "0.1.11"
dyn-clone = "1.0.10"
puruspe = "0.4.0"
pymath = { workspace = true }
xml-rs = "0.8.14"

# random
Expand Down
39 changes: 17 additions & 22 deletions stdlib/src/math.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
pub(crate) use math::make_module;

use crate::{builtins::PyBaseExceptionRef, vm::VirtualMachine};

#[pymodule]
mod math {
use crate::vm::{
Expand All @@ -17,6 +19,8 @@ mod math {
// Constants
#[pyattr]
use std::f64::consts::{E as e, PI as pi, TAU as tau};

use super::pymath_error_to_exception;
#[pyattr(name = "inf")]
const INF: f64 = f64::INFINITY;
#[pyattr(name = "nan")]
Expand Down Expand Up @@ -475,38 +479,22 @@ mod math {
// Special functions:
#[pyfunction]
fn erf(x: ArgIntoFloat) -> f64 {
let x = *x;
if x.is_nan() { x } else { puruspe::erf(x) }
pymath::erf(*x)
}

#[pyfunction]
fn erfc(x: ArgIntoFloat) -> f64 {
let x = *x;
if x.is_nan() { x } else { puruspe::erfc(x) }
pymath::erfc(*x)
}

#[pyfunction]
fn gamma(x: ArgIntoFloat) -> f64 {
let x = *x;
if x.is_finite() {
puruspe::gamma(x)
} else if x.is_nan() || x.is_sign_positive() {
x
} else {
f64::NAN
}
fn gamma(x: ArgIntoFloat, vm: &VirtualMachine) -> PyResult<f64> {
pymath::gamma(*x).map_err(|err| pymath_error_to_exception(err, vm))
}

#[pyfunction]
fn lgamma(x: ArgIntoFloat) -> f64 {
let x = *x;
if x.is_finite() {
puruspe::ln_gamma(x)
} else if x.is_nan() {
x
} else {
f64::INFINITY
}
fn lgamma(x: ArgIntoFloat, vm: &VirtualMachine) -> PyResult<f64> {
pymath::lgamma(*x).map_err(|err| pymath_error_to_exception(err, vm))
}

fn try_magic_method(
Expand Down Expand Up @@ -1000,3 +988,10 @@ mod math {
Ok(result)
}
}

fn pymath_error_to_exception(err: pymath::Error, vm: &VirtualMachine) -> PyBaseExceptionRef {
match err {
pymath::Error::EDOM => vm.new_value_error("math domain error".to_owned()),
pymath::Error::ERANGE => vm.new_overflow_error("math range error".to_owned()),
}
}
Loading