diff --git a/complex.c b/complex.c index 2a37c42126..95b1abbd45 100644 --- a/complex.c +++ b/complex.c @@ -661,8 +661,9 @@ f_complex_polar(VALUE klass, VALUE x, VALUE y) y = DBL2NUM(imag); } else { - y = f_mul(x, DBL2NUM(sin(arg))); - x = f_mul(x, DBL2NUM(cos(arg))); + const double ax = sin(arg), ay = cos(arg); + y = f_mul(x, DBL2NUM(ax)); + x = f_mul(x, DBL2NUM(ay)); if (canonicalization && f_zero_p(y)) return x; } return nucomp_s_new_internal(klass, x, y); @@ -672,6 +673,16 @@ f_complex_polar(VALUE klass, VALUE x, VALUE y) f_mul(x, m_sin(y))); } +#ifdef HAVE___COSPI +# define cospi(x) __cospi(x) +#else +# define cospi(x) cos((x) * M_PI) +#endif +#ifdef HAVE___SINPI +# define sinpi(x) __sinpi(x) +#else +# define sinpi(x) sin((x) * M_PI) +#endif /* returns a Complex or Float of ang*PI-rotated abs */ VALUE rb_dbl_complex_new_polar_pi(double abs, double ang) @@ -689,8 +700,8 @@ rb_dbl_complex_new_polar_pi(double abs, double ang) return DBL2NUM(abs); } else { - ang *= M_PI; - return rb_complex_new(DBL2NUM(abs * cos(ang)), DBL2NUM(abs * sin(ang))); + const double real = abs * cospi(ang), imag = abs * sinpi(ang); + return rb_complex_new(DBL2NUM(real), DBL2NUM(imag)); } } diff --git a/configure.ac b/configure.ac index e16c3e44ed..f78eb38c93 100644 --- a/configure.ac +++ b/configure.ac @@ -1991,6 +1991,8 @@ AC_CHECK_FUNCS(utimensat) AC_CHECK_FUNCS(utimes) AC_CHECK_FUNCS(wait4) AC_CHECK_FUNCS(waitpid) +AC_CHECK_FUNCS(__cospi) +AC_CHECK_FUNCS(__sinpi) AS_IF([test "x$ac_cv_member_struct_statx_stx_btime" = xyes], [AC_CHECK_FUNCS(statx)])