summaryrefslogtreecommitdiff
path: root/complex.c
diff options
context:
space:
mode:
authorNobuyoshi Nakada <nobu@ruby-lang.org>2022-08-31 13:20:40 +0900
committerNobuyoshi Nakada <nobu@ruby-lang.org>2022-09-02 14:33:23 +0900
commitb5cf3564471af6e11760bf381251f918cdcd7398 (patch)
treeb16c9e26caa7a1e3b79f4366083132dd334805db /complex.c
parent9212d963070612e669c40e5fde7954f19d648002 (diff)
downloadruby-b5cf3564471af6e11760bf381251f918cdcd7398.tar.gz
Consider Complex from Complex cases
The assertions that "an argument of a Complex constructor must not be a Complex" may not hold for some Numeric objects.
Diffstat (limited to 'complex.c')
-rw-r--r--complex.c24
1 files changed, 13 insertions, 11 deletions
diff --git a/complex.c b/complex.c
index 7d45b445db..07e5914d54 100644
--- a/complex.c
+++ b/complex.c
@@ -495,7 +495,11 @@ nucomp_s_new(int argc, VALUE *argv, VALUE klass)
inline static VALUE
f_complex_new2(VALUE klass, VALUE x, VALUE y)
{
- assert(!RB_TYPE_P(x, T_COMPLEX));
+ if (RB_TYPE_P(x, T_COMPLEX)) {
+ get_dat1(x);
+ x = dat->real;
+ y = f_add(dat->imag, y);
+ }
return nucomp_s_canonicalize_internal(klass, x, y);
}
@@ -609,8 +613,14 @@ m_sin(VALUE x)
static VALUE
f_complex_polar(VALUE klass, VALUE x, VALUE y)
{
- assert(!RB_TYPE_P(x, T_COMPLEX));
- assert(!RB_TYPE_P(y, T_COMPLEX));
+ if (RB_TYPE_P(x, T_COMPLEX)) {
+ get_dat1(x);
+ x = dat->real;
+ }
+ if (RB_TYPE_P(y, T_COMPLEX)) {
+ get_dat1(y);
+ y = dat->real;
+ }
if (f_zero_p(x) || f_zero_p(y)) {
return nucomp_s_new_internal(klass, x, RFLOAT_0);
}
@@ -703,14 +713,6 @@ nucomp_s_polar(int argc, VALUE *argv, VALUE klass)
nucomp_real_check(arg);
break;
}
- if (RB_TYPE_P(abs, T_COMPLEX)) {
- get_dat1(abs);
- abs = dat->real;
- }
- if (RB_TYPE_P(arg, T_COMPLEX)) {
- get_dat1(arg);
- arg = dat->real;
- }
return f_complex_polar(klass, abs, arg);
}