Skip to content

Commit 0c83ff1

Browse files
committed
try
1 parent 8313a32 commit 0c83ff1

3 files changed

Lines changed: 42 additions & 14 deletions

File tree

src/cmath/exponential.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use super::{
44
CM_LARGE_DOUBLE, CM_LOG_LARGE_DOUBLE, INF, M_LN2, N, P, P12, P14, P34, U, c, special_type,
55
special_value,
66
};
7-
use crate::{Error, Result, m};
7+
use crate::{Error, Result, m, mul_add};
88
use num_complex::Complex64;
99

1010
// Local constants
@@ -160,7 +160,8 @@ pub(crate) fn ln(z: Complex64) -> Result<Complex64> {
160160
if (0.71..=1.73).contains(&h) {
161161
let am = if ax > ay { ax } else { ay }; // max(ax, ay)
162162
let an = if ax > ay { ay } else { ax }; // min(ax, ay)
163-
m::log1p((am - 1.0) * (am + 1.0) + an * an) / 2.0
163+
let log1p_arg = mul_add(am - 1.0, am + 1.0, an * an);
164+
m::log1p(log1p_arg) / 2.0
164165
} else {
165166
m::log(h)
166167
}

src/cmath/misc.rs

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,41 @@ pub fn phase(z: Complex64) -> Result<f64> {
2828
}
2929
}
3030

31+
#[inline]
32+
fn c_abs_raw(z: Complex64) -> f64 {
33+
if !z.re.is_finite() || !z.im.is_finite() {
34+
// C99 rules: if either part is infinite, return infinity,
35+
// even if the other part is NaN.
36+
if z.re.is_infinite() {
37+
return m::fabs(z.re);
38+
}
39+
if z.im.is_infinite() {
40+
return m::fabs(z.im);
41+
}
42+
return f64::NAN;
43+
}
44+
m::hypot(z.re, z.im)
45+
}
46+
47+
#[inline]
48+
fn c_abs_checked(z: Complex64) -> Result<f64> {
49+
if !z.re.is_finite() || !z.im.is_finite() {
50+
return Ok(c_abs_raw(z));
51+
}
52+
crate::err::set_errno(0);
53+
let r = m::hypot(z.re, z.im);
54+
if r.is_infinite() {
55+
Err(Error::ERANGE)
56+
} else {
57+
Ok(r)
58+
}
59+
}
60+
3161
/// Convert z to polar coordinates (r, phi).
3262
#[inline]
3363
pub fn polar(z: Complex64) -> Result<(f64, f64)> {
3464
let phi = m::atan2(z.im, z.re);
35-
let r = m::hypot(z.re, z.im);
36-
if r.is_infinite() && z.re.is_finite() && z.im.is_finite() {
37-
return Err(Error::ERANGE);
38-
}
65+
let r = c_abs_checked(z)?;
3966
Ok((r, phi))
4067
}
4168

@@ -94,7 +121,7 @@ pub fn isinf(z: Complex64) -> bool {
94121
/// Complex absolute value (magnitude).
95122
#[inline]
96123
pub fn abs(z: Complex64) -> f64 {
97-
m::hypot(z.re, z.im)
124+
c_abs_raw(z)
98125
}
99126

100127
/// Determine whether two complex numbers are close in value.

src/math/integer.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -715,7 +715,7 @@ mod tests {
715715
use pyo3::prelude::*;
716716

717717
/// Edge i64 values for testing integer math functions (gcd, lcm, isqrt, factorial, comb, perm)
718-
const EDGE_I64: [i64; 44] = [
718+
const EDGE_I64: &[i64] = &[
719719
// Zero and small values
720720
0,
721721
1,
@@ -982,9 +982,9 @@ mod tests {
982982
#[test]
983983
fn edgetest_gcd() {
984984
// Test all edge values - gcd handles arbitrary large integers
985-
for &a in &EDGE_I64 {
985+
for &a in EDGE_I64 {
986986
test_gcd_impl(&[a]);
987-
for &b in &EDGE_I64 {
987+
for &b in EDGE_I64 {
988988
test_gcd_impl(&[a, b]);
989989
}
990990
}
@@ -997,9 +997,9 @@ mod tests {
997997
#[test]
998998
fn edgetest_lcm() {
999999
// Test all edge values - lcm handles arbitrary large integers
1000-
for &a in &EDGE_I64 {
1000+
for &a in EDGE_I64 {
10011001
test_lcm_impl(&[a]);
1002-
for &b in &EDGE_I64 {
1002+
for &b in EDGE_I64 {
10031003
test_lcm_impl(&[a, b]);
10041004
}
10051005
}
@@ -1011,7 +1011,7 @@ mod tests {
10111011

10121012
#[test]
10131013
fn edgetest_isqrt() {
1014-
for &n in &EDGE_I64 {
1014+
for &n in EDGE_I64 {
10151015
test_isqrt_impl(n);
10161016
}
10171017
// Additional boundary cases
@@ -1031,7 +1031,7 @@ mod tests {
10311031

10321032
#[test]
10331033
fn edgetest_factorial() {
1034-
for &n in &EDGE_I64 {
1034+
for &n in EDGE_I64 {
10351035
// factorial only makes sense for reasonable n values
10361036
if n >= -10 && n <= 170 {
10371037
test_factorial_impl(n);

0 commit comments

Comments
 (0)