@@ -421,15 +421,22 @@ f_complex_new_bang2(VALUE klass, VALUE x, VALUE y)
421421 return nucomp_s_new_internal (klass , x , y );
422422}
423423
424- inline static void
424+ WARN_UNUSED_RESULT (inline static VALUE nucomp_real_check (VALUE num ));
425+ inline static VALUE
425426nucomp_real_check (VALUE num )
426427{
427428 if (!RB_INTEGER_TYPE_P (num ) &&
428429 !RB_FLOAT_TYPE_P (num ) &&
429430 !RB_TYPE_P (num , T_RATIONAL )) {
431+ if (RB_TYPE_P (num , T_COMPLEX ) && nucomp_real_p (num )) {
432+ VALUE real = RCOMPLEX (num )-> real ;
433+ assert (!RB_TYPE_P (real , T_COMPLEX ));
434+ return real ;
435+ }
430436 if (!k_numeric_p (num ) || !f_real_p (num ))
431437 rb_raise (rb_eTypeError , "not a real" );
432438 }
439+ return num ;
433440}
434441
435442inline static VALUE
@@ -480,16 +487,16 @@ nucomp_s_new(int argc, VALUE *argv, VALUE klass)
480487
481488 switch (rb_scan_args (argc , argv , "11" , & real , & imag )) {
482489 case 1 :
483- nucomp_real_check (real );
490+ real = nucomp_real_check (real );
484491 imag = ZERO ;
485492 break ;
486493 default :
487- nucomp_real_check (real );
488- nucomp_real_check (imag );
494+ real = nucomp_real_check (real );
495+ imag = nucomp_real_check (imag );
489496 break ;
490497 }
491498
492- return nucomp_s_canonicalize_internal (klass , real , imag );
499+ return nucomp_s_new_internal (klass , real , imag );
493500}
494501
495502inline static VALUE
@@ -611,16 +618,8 @@ m_sin(VALUE x)
611618}
612619
613620static VALUE
614- f_complex_polar (VALUE klass , VALUE x , VALUE y )
621+ f_complex_polar_real (VALUE klass , VALUE x , VALUE y )
615622{
616- if (RB_TYPE_P (x , T_COMPLEX )) {
617- get_dat1 (x );
618- x = dat -> real ;
619- }
620- if (RB_TYPE_P (y , T_COMPLEX )) {
621- get_dat1 (y );
622- y = dat -> real ;
623- }
624623 if (f_zero_p (x ) || f_zero_p (y )) {
625624 return nucomp_s_new_internal (klass , x , RFLOAT_0 );
626625 }
@@ -656,6 +655,14 @@ f_complex_polar(VALUE klass, VALUE x, VALUE y)
656655 f_mul (x , m_sin (y )));
657656}
658657
658+ static VALUE
659+ f_complex_polar (VALUE klass , VALUE x , VALUE y )
660+ {
661+ x = nucomp_real_check (x );
662+ y = nucomp_real_check (y );
663+ return f_complex_polar_real (klass , x , y );
664+ }
665+
659666#ifdef HAVE___COSPI
660667# define cospi (x ) __cospi(x)
661668#else
@@ -705,14 +712,14 @@ nucomp_s_polar(int argc, VALUE *argv, VALUE klass)
705712 VALUE abs , arg ;
706713
707714 argc = rb_scan_args (argc , argv , "11" , & abs , & arg );
708- nucomp_real_check (abs );
715+ abs = nucomp_real_check (abs );
709716 if (argc == 2 ) {
710- nucomp_real_check (arg );
717+ arg = nucomp_real_check (arg );
711718 }
712719 else {
713720 arg = ZERO ;
714721 }
715- return f_complex_polar (klass , abs , arg );
722+ return f_complex_polar_real (klass , abs , arg );
716723}
717724
718725/*
0 commit comments