Ruby 3.5.0dev (2025-06-27 revision 4965954556b1db71fba6ce090cc217e97641687e)
complex.c (4965954556b1db71fba6ce090cc217e97641687e)
1/*
2 complex.c: Coded by Tadayoshi Funaba 2008-2012
3
4 This implementation is based on Keiju Ishitsuka's Complex library
5 which is written in ruby.
6*/
7
8#include "ruby/internal/config.h"
9
10#if defined _MSC_VER
11/* Microsoft Visual C does not define M_PI and others by default */
12# define _USE_MATH_DEFINES 1
13#endif
14
15#include <ctype.h>
16#include <math.h>
17
18#include "id.h"
19#include "internal.h"
20#include "internal/array.h"
21#include "internal/class.h"
22#include "internal/complex.h"
23#include "internal/math.h"
24#include "internal/numeric.h"
25#include "internal/object.h"
26#include "internal/rational.h"
27#include "internal/string.h"
28#include "ruby_assert.h"
29
30#define ZERO INT2FIX(0)
31#define ONE INT2FIX(1)
32#define TWO INT2FIX(2)
33#if USE_FLONUM
34#define RFLOAT_0 DBL2NUM(0)
35#else
36static VALUE RFLOAT_0;
37#endif
38
40
41static ID id_abs, id_arg,
42 id_denominator, id_numerator,
43 id_real_p, id_i_real, id_i_imag,
44 id_finite_p, id_infinite_p, id_rationalize,
45 id_PI;
46#define id_to_i idTo_i
47#define id_to_r idTo_r
48#define id_negate idUMinus
49#define id_expt idPow
50#define id_to_f idTo_f
51#define id_quo idQuo
52#define id_fdiv idFdiv
53
54#define fun1(n) \
55inline static VALUE \
56f_##n(VALUE x)\
57{\
58 return rb_funcall(x, id_##n, 0);\
59}
60
61#define fun2(n) \
62inline static VALUE \
63f_##n(VALUE x, VALUE y)\
64{\
65 return rb_funcall(x, id_##n, 1, y);\
66}
67
68#define PRESERVE_SIGNEDZERO
69
70inline static VALUE
71f_add(VALUE x, VALUE y)
72{
73 if (RB_INTEGER_TYPE_P(x) &&
74 LIKELY(rb_method_basic_definition_p(rb_cInteger, idPLUS))) {
75 if (FIXNUM_ZERO_P(x))
76 return y;
77 if (FIXNUM_ZERO_P(y))
78 return x;
79 return rb_int_plus(x, y);
80 }
81 else if (RB_FLOAT_TYPE_P(x) &&
82 LIKELY(rb_method_basic_definition_p(rb_cFloat, idPLUS))) {
83 if (FIXNUM_ZERO_P(y))
84 return x;
85 return rb_float_plus(x, y);
86 }
87 else if (RB_TYPE_P(x, T_RATIONAL) &&
88 LIKELY(rb_method_basic_definition_p(rb_cRational, idPLUS))) {
89 if (FIXNUM_ZERO_P(y))
90 return x;
91 return rb_rational_plus(x, y);
92 }
93
94 return rb_funcall(x, '+', 1, y);
95}
96
97inline static VALUE
98f_div(VALUE x, VALUE y)
99{
100 if (FIXNUM_P(y) && FIX2LONG(y) == 1)
101 return x;
102 return rb_funcall(x, '/', 1, y);
103}
104
105inline static int
106f_gt_p(VALUE x, VALUE y)
107{
108 if (RB_INTEGER_TYPE_P(x)) {
109 if (FIXNUM_P(x) && FIXNUM_P(y))
110 return (SIGNED_VALUE)x > (SIGNED_VALUE)y;
111 return RTEST(rb_int_gt(x, y));
112 }
113 else if (RB_FLOAT_TYPE_P(x))
114 return RTEST(rb_float_gt(x, y));
115 else if (RB_TYPE_P(x, T_RATIONAL)) {
116 int const cmp = rb_cmpint(rb_rational_cmp(x, y), x, y);
117 return cmp > 0;
118 }
119 return RTEST(rb_funcall(x, '>', 1, y));
120}
121
122inline static VALUE
123f_mul(VALUE x, VALUE y)
124{
125 if (RB_INTEGER_TYPE_P(x) &&
126 LIKELY(rb_method_basic_definition_p(rb_cInteger, idMULT))) {
127 if (FIXNUM_ZERO_P(y))
128 return ZERO;
129 if (FIXNUM_ZERO_P(x) && RB_INTEGER_TYPE_P(y))
130 return ZERO;
131 if (x == ONE) return y;
132 if (y == ONE) return x;
133 return rb_int_mul(x, y);
134 }
135 else if (RB_FLOAT_TYPE_P(x) &&
136 LIKELY(rb_method_basic_definition_p(rb_cFloat, idMULT))) {
137 if (y == ONE) return x;
138 return rb_float_mul(x, y);
139 }
140 else if (RB_TYPE_P(x, T_RATIONAL) &&
141 LIKELY(rb_method_basic_definition_p(rb_cRational, idMULT))) {
142 if (y == ONE) return x;
143 return rb_rational_mul(x, y);
144 }
145 else if (LIKELY(rb_method_basic_definition_p(CLASS_OF(x), idMULT))) {
146 if (y == ONE) return x;
147 }
148 return rb_funcall(x, '*', 1, y);
149}
150
151inline static VALUE
152f_sub(VALUE x, VALUE y)
153{
154 if (FIXNUM_ZERO_P(y) &&
155 LIKELY(rb_method_basic_definition_p(CLASS_OF(x), idMINUS))) {
156 return x;
157 }
158 return rb_funcall(x, '-', 1, y);
159}
160
161inline static VALUE
162f_abs(VALUE x)
163{
164 if (RB_INTEGER_TYPE_P(x)) {
165 return rb_int_abs(x);
166 }
167 else if (RB_FLOAT_TYPE_P(x)) {
168 return rb_float_abs(x);
169 }
170 else if (RB_TYPE_P(x, T_RATIONAL)) {
171 return rb_rational_abs(x);
172 }
173 else if (RB_TYPE_P(x, T_COMPLEX)) {
174 return rb_complex_abs(x);
175 }
176 return rb_funcall(x, id_abs, 0);
177}
178
179static VALUE numeric_arg(VALUE self);
180static VALUE float_arg(VALUE self);
181
182inline static VALUE
183f_arg(VALUE x)
184{
185 if (RB_INTEGER_TYPE_P(x)) {
186 return numeric_arg(x);
187 }
188 else if (RB_FLOAT_TYPE_P(x)) {
189 return float_arg(x);
190 }
191 else if (RB_TYPE_P(x, T_RATIONAL)) {
192 return numeric_arg(x);
193 }
194 else if (RB_TYPE_P(x, T_COMPLEX)) {
195 return rb_complex_arg(x);
196 }
197 return rb_funcall(x, id_arg, 0);
198}
199
200inline static VALUE
201f_numerator(VALUE x)
202{
203 if (RB_TYPE_P(x, T_RATIONAL)) {
204 return RRATIONAL(x)->num;
205 }
206 if (RB_FLOAT_TYPE_P(x)) {
207 return rb_float_numerator(x);
208 }
209 return x;
210}
211
212inline static VALUE
213f_denominator(VALUE x)
214{
215 if (RB_TYPE_P(x, T_RATIONAL)) {
216 return RRATIONAL(x)->den;
217 }
218 if (RB_FLOAT_TYPE_P(x)) {
219 return rb_float_denominator(x);
220 }
221 return INT2FIX(1);
222}
223
224inline static VALUE
225f_negate(VALUE x)
226{
227 if (RB_INTEGER_TYPE_P(x)) {
228 return rb_int_uminus(x);
229 }
230 else if (RB_FLOAT_TYPE_P(x)) {
231 return rb_float_uminus(x);
232 }
233 else if (RB_TYPE_P(x, T_RATIONAL)) {
234 return rb_rational_uminus(x);
235 }
236 else if (RB_TYPE_P(x, T_COMPLEX)) {
237 return rb_complex_uminus(x);
238 }
239 return rb_funcall(x, id_negate, 0);
240}
241
242static bool nucomp_real_p(VALUE self);
243
244static inline bool
245f_real_p(VALUE x)
246{
247 if (RB_INTEGER_TYPE_P(x)) {
248 return true;
249 }
250 else if (RB_FLOAT_TYPE_P(x)) {
251 return true;
252 }
253 else if (RB_TYPE_P(x, T_RATIONAL)) {
254 return true;
255 }
256 else if (RB_TYPE_P(x, T_COMPLEX)) {
257 return nucomp_real_p(x);
258 }
259 return rb_funcall(x, id_real_p, 0);
260}
261
262inline static VALUE
263f_to_i(VALUE x)
264{
265 if (RB_TYPE_P(x, T_STRING))
266 return rb_str_to_inum(x, 10, 0);
267 return rb_funcall(x, id_to_i, 0);
268}
269
270inline static VALUE
271f_to_f(VALUE x)
272{
273 if (RB_TYPE_P(x, T_STRING))
274 return DBL2NUM(rb_str_to_dbl(x, 0));
275 return rb_funcall(x, id_to_f, 0);
276}
277
278fun1(to_r)
279
280inline static int
281f_eqeq_p(VALUE x, VALUE y)
282{
283 if (FIXNUM_P(x) && FIXNUM_P(y))
284 return x == y;
285 else if (RB_FLOAT_TYPE_P(x) || RB_FLOAT_TYPE_P(y))
286 return NUM2DBL(x) == NUM2DBL(y);
287 return (int)rb_equal(x, y);
288}
289
290fun2(expt)
291fun2(fdiv)
292
293static VALUE
294f_quo(VALUE x, VALUE y)
295{
296 if (RB_INTEGER_TYPE_P(x))
297 return rb_numeric_quo(x, y);
298 if (RB_FLOAT_TYPE_P(x))
299 return rb_float_div(x, y);
300 if (RB_TYPE_P(x, T_RATIONAL))
301 return rb_numeric_quo(x, y);
302
303 return rb_funcallv(x, id_quo, 1, &y);
304}
305
306inline static int
307f_negative_p(VALUE x)
308{
309 if (RB_INTEGER_TYPE_P(x))
310 return INT_NEGATIVE_P(x);
311 else if (RB_FLOAT_TYPE_P(x))
312 return RFLOAT_VALUE(x) < 0.0;
313 else if (RB_TYPE_P(x, T_RATIONAL))
314 return INT_NEGATIVE_P(RRATIONAL(x)->num);
315 return rb_num_negative_p(x);
316}
317
318#define f_positive_p(x) (!f_negative_p(x))
319
320inline static bool
321f_zero_p(VALUE x)
322{
323 if (RB_FLOAT_TYPE_P(x)) {
324 return FLOAT_ZERO_P(x);
325 }
326 else if (RB_INTEGER_TYPE_P(x)) {
327 return FIXNUM_ZERO_P(x);
328 }
329 else if (RB_TYPE_P(x, T_RATIONAL)) {
330 const VALUE num = RRATIONAL(x)->num;
331 return FIXNUM_ZERO_P(num);
332 }
333 return rb_equal(x, ZERO) != 0;
334}
335
336#define f_nonzero_p(x) (!f_zero_p(x))
337
338static inline bool
339always_finite_type_p(VALUE x)
340{
341 if (FIXNUM_P(x)) return true;
342 if (FLONUM_P(x)) return true; /* Infinity can't be a flonum */
343 return (RB_INTEGER_TYPE_P(x) || RB_TYPE_P(x, T_RATIONAL));
344}
345
346inline static int
347f_finite_p(VALUE x)
348{
349 if (always_finite_type_p(x)) {
350 return TRUE;
351 }
352 else if (RB_FLOAT_TYPE_P(x)) {
353 return isfinite(RFLOAT_VALUE(x));
354 }
355 return RTEST(rb_funcallv(x, id_finite_p, 0, 0));
356}
357
358inline static int
359f_infinite_p(VALUE x)
360{
361 if (always_finite_type_p(x)) {
362 return FALSE;
363 }
364 else if (RB_FLOAT_TYPE_P(x)) {
365 return isinf(RFLOAT_VALUE(x));
366 }
367 return RTEST(rb_funcallv(x, id_infinite_p, 0, 0));
368}
369
370inline static int
371f_kind_of_p(VALUE x, VALUE c)
372{
373 return (int)rb_obj_is_kind_of(x, c);
374}
375
376inline static int
377k_numeric_p(VALUE x)
378{
379 return f_kind_of_p(x, rb_cNumeric);
380}
381
382#define k_exact_p(x) (!RB_FLOAT_TYPE_P(x))
383
384#define k_exact_zero_p(x) (k_exact_p(x) && f_zero_p(x))
385
386#define get_dat1(x) \
387 struct RComplex *dat = RCOMPLEX(x)
388
389#define get_dat2(x,y) \
390 struct RComplex *adat = RCOMPLEX(x), *bdat = RCOMPLEX(y)
391
392inline static VALUE
393nucomp_s_new_internal(VALUE klass, VALUE real, VALUE imag)
394{
395 NEWOBJ_OF(obj, struct RComplex, klass,
397
398 RCOMPLEX_SET_REAL(obj, real);
399 RCOMPLEX_SET_IMAG(obj, imag);
400 OBJ_FREEZE((VALUE)obj);
401
402 return (VALUE)obj;
403}
404
405static VALUE
406nucomp_s_alloc(VALUE klass)
407{
408 return nucomp_s_new_internal(klass, ZERO, ZERO);
409}
410
411inline static VALUE
412f_complex_new_bang1(VALUE klass, VALUE x)
413{
415 return nucomp_s_new_internal(klass, x, ZERO);
416}
417
418inline static VALUE
419f_complex_new_bang2(VALUE klass, VALUE x, VALUE y)
420{
423 return nucomp_s_new_internal(klass, x, y);
424}
425
426WARN_UNUSED_RESULT(inline static VALUE nucomp_real_check(VALUE num));
427inline static VALUE
428nucomp_real_check(VALUE num)
429{
430 if (!RB_INTEGER_TYPE_P(num) &&
431 !RB_FLOAT_TYPE_P(num) &&
432 !RB_TYPE_P(num, T_RATIONAL)) {
433 if (RB_TYPE_P(num, T_COMPLEX) && nucomp_real_p(num)) {
434 VALUE real = RCOMPLEX(num)->real;
436 return real;
437 }
438 if (!k_numeric_p(num) || !f_real_p(num))
439 rb_raise(rb_eTypeError, "not a real");
440 }
441 return num;
442}
443
444inline static VALUE
445nucomp_s_canonicalize_internal(VALUE klass, VALUE real, VALUE imag)
446{
447 int complex_r, complex_i;
448 complex_r = RB_TYPE_P(real, T_COMPLEX);
449 complex_i = RB_TYPE_P(imag, T_COMPLEX);
450 if (!complex_r && !complex_i) {
451 return nucomp_s_new_internal(klass, real, imag);
452 }
453 else if (!complex_r) {
454 get_dat1(imag);
455
456 return nucomp_s_new_internal(klass,
457 f_sub(real, dat->imag),
458 f_add(ZERO, dat->real));
459 }
460 else if (!complex_i) {
461 get_dat1(real);
462
463 return nucomp_s_new_internal(klass,
464 dat->real,
465 f_add(dat->imag, imag));
466 }
467 else {
468 get_dat2(real, imag);
469
470 return nucomp_s_new_internal(klass,
471 f_sub(adat->real, bdat->imag),
472 f_add(adat->imag, bdat->real));
473 }
474}
475
476/*
477 * call-seq:
478 * Complex.rect(real, imag = 0) -> complex
479 *
480 * Returns a new \Complex object formed from the arguments,
481 * each of which must be an instance of Numeric,
482 * or an instance of one of its subclasses:
483 * \Complex, Float, Integer, Rational;
484 * see {Rectangular Coordinates}[rdoc-ref:Complex@Rectangular+Coordinates]:
485 *
486 * Complex.rect(3) # => (3+0i)
487 * Complex.rect(3, Math::PI) # => (3+3.141592653589793i)
488 * Complex.rect(-3, -Math::PI) # => (-3-3.141592653589793i)
489 *
490 * \Complex.rectangular is an alias for \Complex.rect.
491 */
492static VALUE
493nucomp_s_new(int argc, VALUE *argv, VALUE klass)
494{
495 VALUE real, imag;
496
497 switch (rb_scan_args(argc, argv, "11", &real, &imag)) {
498 case 1:
499 real = nucomp_real_check(real);
500 imag = ZERO;
501 break;
502 default:
503 real = nucomp_real_check(real);
504 imag = nucomp_real_check(imag);
505 break;
506 }
507
508 return nucomp_s_new_internal(klass, real, imag);
509}
510
511inline static VALUE
512f_complex_new2(VALUE klass, VALUE x, VALUE y)
513{
514 if (RB_TYPE_P(x, T_COMPLEX)) {
515 get_dat1(x);
516 x = dat->real;
517 y = f_add(dat->imag, y);
518 }
519 return nucomp_s_canonicalize_internal(klass, x, y);
520}
521
522static VALUE nucomp_convert(VALUE klass, VALUE a1, VALUE a2, int raise);
523static VALUE nucomp_s_convert(int argc, VALUE *argv, VALUE klass);
524
525/*
526 * call-seq:
527 * Complex(real, imag = 0, exception: true) -> complex or nil
528 * Complex(s, exception: true) -> complex or nil
529 *
530 * Returns a new \Complex object if the arguments are valid;
531 * otherwise raises an exception if +exception+ is +true+;
532 * otherwise returns +nil+.
533 *
534 * With Numeric arguments +real+ and +imag+,
535 * returns <tt>Complex.rect(real, imag)</tt> if the arguments are valid.
536 *
537 * With string argument +s+, returns a new \Complex object if the argument is valid;
538 * the string may have:
539 *
540 * - One or two numeric substrings,
541 * each of which specifies a Complex, Float, Integer, Numeric, or Rational value,
542 * specifying {rectangular coordinates}[rdoc-ref:Complex@Rectangular+Coordinates]:
543 *
544 * - Sign-separated real and imaginary numeric substrings
545 * (with trailing character <tt>'i'</tt>):
546 *
547 * Complex('1+2i') # => (1+2i)
548 * Complex('+1+2i') # => (1+2i)
549 * Complex('+1-2i') # => (1-2i)
550 * Complex('-1+2i') # => (-1+2i)
551 * Complex('-1-2i') # => (-1-2i)
552 *
553 * - Real-only numeric string (without trailing character <tt>'i'</tt>):
554 *
555 * Complex('1') # => (1+0i)
556 * Complex('+1') # => (1+0i)
557 * Complex('-1') # => (-1+0i)
558 *
559 * - Imaginary-only numeric string (with trailing character <tt>'i'</tt>):
560 *
561 * Complex('1i') # => (0+1i)
562 * Complex('+1i') # => (0+1i)
563 * Complex('-1i') # => (0-1i)
564 *
565 * - At-sign separated real and imaginary rational substrings,
566 * each of which specifies a Rational value,
567 * specifying {polar coordinates}[rdoc-ref:Complex@Polar+Coordinates]:
568 *
569 * Complex('1/2@3/4') # => (0.36584443443691045+0.34081938001166706i)
570 * Complex('+1/2@+3/4') # => (0.36584443443691045+0.34081938001166706i)
571 * Complex('+1/2@-3/4') # => (0.36584443443691045-0.34081938001166706i)
572 * Complex('-1/2@+3/4') # => (-0.36584443443691045-0.34081938001166706i)
573 * Complex('-1/2@-3/4') # => (-0.36584443443691045+0.34081938001166706i)
574 *
575 */
576static VALUE
577nucomp_f_complex(int argc, VALUE *argv, VALUE klass)
578{
579 VALUE a1, a2, opts = Qnil;
580 int raise = TRUE;
581
582 if (rb_scan_args(argc, argv, "11:", &a1, &a2, &opts) == 1) {
583 a2 = Qundef;
584 }
585 if (!NIL_P(opts)) {
586 raise = rb_opts_exception_p(opts, raise);
587 }
588 if (argc > 0 && CLASS_OF(a1) == rb_cComplex && UNDEF_P(a2)) {
589 return a1;
590 }
591 return nucomp_convert(rb_cComplex, a1, a2, raise);
592}
593
594#define imp1(n) \
595inline static VALUE \
596m_##n##_bang(VALUE x)\
597{\
598 return rb_math_##n(x);\
599}
600
601imp1(cos)
602imp1(cosh)
603imp1(exp)
604
605static VALUE
606m_log_bang(VALUE x)
607{
608 return rb_math_log(1, &x);
609}
610
611imp1(sin)
612imp1(sinh)
613
614static VALUE
615m_cos(VALUE x)
616{
617 if (!RB_TYPE_P(x, T_COMPLEX))
618 return m_cos_bang(x);
619 {
620 get_dat1(x);
621 return f_complex_new2(rb_cComplex,
622 f_mul(m_cos_bang(dat->real),
623 m_cosh_bang(dat->imag)),
624 f_mul(f_negate(m_sin_bang(dat->real)),
625 m_sinh_bang(dat->imag)));
626 }
627}
628
629static VALUE
630m_sin(VALUE x)
631{
632 if (!RB_TYPE_P(x, T_COMPLEX))
633 return m_sin_bang(x);
634 {
635 get_dat1(x);
636 return f_complex_new2(rb_cComplex,
637 f_mul(m_sin_bang(dat->real),
638 m_cosh_bang(dat->imag)),
639 f_mul(m_cos_bang(dat->real),
640 m_sinh_bang(dat->imag)));
641 }
642}
643
644static VALUE
645f_complex_polar_real(VALUE klass, VALUE x, VALUE y)
646{
647 if (f_zero_p(x) || f_zero_p(y)) {
648 return nucomp_s_new_internal(klass, x, RFLOAT_0);
649 }
650 if (RB_FLOAT_TYPE_P(y)) {
651 const double arg = RFLOAT_VALUE(y);
652 if (arg == M_PI) {
653 x = f_negate(x);
654 y = RFLOAT_0;
655 }
656 else if (arg == M_PI_2) {
657 y = x;
658 x = RFLOAT_0;
659 }
660 else if (arg == M_PI_2+M_PI) {
661 y = f_negate(x);
662 x = RFLOAT_0;
663 }
664 else if (RB_FLOAT_TYPE_P(x)) {
665 const double abs = RFLOAT_VALUE(x);
666 const double real = abs * cos(arg), imag = abs * sin(arg);
667 x = DBL2NUM(real);
668 y = DBL2NUM(imag);
669 }
670 else {
671 const double ax = sin(arg), ay = cos(arg);
672 y = f_mul(x, DBL2NUM(ax));
673 x = f_mul(x, DBL2NUM(ay));
674 }
675 return nucomp_s_new_internal(klass, x, y);
676 }
677 return nucomp_s_canonicalize_internal(klass,
678 f_mul(x, m_cos(y)),
679 f_mul(x, m_sin(y)));
680}
681
682static VALUE
683f_complex_polar(VALUE klass, VALUE x, VALUE y)
684{
685 x = nucomp_real_check(x);
686 y = nucomp_real_check(y);
687 return f_complex_polar_real(klass, x, y);
688}
689
690#ifdef HAVE___COSPI
691# define cospi(x) __cospi(x)
692#else
693# define cospi(x) cos((x) * M_PI)
694#endif
695#ifdef HAVE___SINPI
696# define sinpi(x) __sinpi(x)
697#else
698# define sinpi(x) sin((x) * M_PI)
699#endif
700/* returns a Complex or Float of ang*PI-rotated abs */
701VALUE
702rb_dbl_complex_new_polar_pi(double abs, double ang)
703{
704 double fi;
705 const double fr = modf(ang, &fi);
706 int pos = fr == +0.5;
707
708 if (pos || fr == -0.5) {
709 if ((modf(fi / 2.0, &fi) != fr) ^ pos) abs = -abs;
710 return rb_complex_new(RFLOAT_0, DBL2NUM(abs));
711 }
712 else if (fr == 0.0) {
713 if (modf(fi / 2.0, &fi) != 0.0) abs = -abs;
714 return DBL2NUM(abs);
715 }
716 else {
717 const double real = abs * cospi(ang), imag = abs * sinpi(ang);
718 return rb_complex_new(DBL2NUM(real), DBL2NUM(imag));
719 }
720}
721
722/*
723 * call-seq:
724 * Complex.polar(abs, arg = 0) -> complex
725 *
726 * Returns a new \Complex object formed from the arguments,
727 * each of which must be an instance of Numeric,
728 * or an instance of one of its subclasses:
729 * \Complex, Float, Integer, Rational.
730 * Argument +arg+ is given in radians;
731 * see {Polar Coordinates}[rdoc-ref:Complex@Polar+Coordinates]:
732 *
733 * Complex.polar(3) # => (3+0i)
734 * Complex.polar(3, 2.0) # => (-1.2484405096414273+2.727892280477045i)
735 * Complex.polar(-3, -2.0) # => (1.2484405096414273+2.727892280477045i)
736 *
737 */
738static VALUE
739nucomp_s_polar(int argc, VALUE *argv, VALUE klass)
740{
741 VALUE abs, arg;
742
743 argc = rb_scan_args(argc, argv, "11", &abs, &arg);
744 abs = nucomp_real_check(abs);
745 if (argc == 2) {
746 arg = nucomp_real_check(arg);
747 }
748 else {
749 arg = ZERO;
750 }
751 return f_complex_polar_real(klass, abs, arg);
752}
753
754/*
755 * call-seq:
756 * real -> numeric
757 *
758 * Returns the real value for +self+:
759 *
760 * Complex.rect(7).real # => 7
761 * Complex.rect(9, -4).real # => 9
762 *
763 * If +self+ was created with
764 * {polar coordinates}[rdoc-ref:Complex@Polar+Coordinates], the returned value
765 * is computed, and may be inexact:
766 *
767 * Complex.polar(1, Math::PI/4).real # => 0.7071067811865476 # Square root of 2.
768 *
769 */
770VALUE
771rb_complex_real(VALUE self)
772{
773 get_dat1(self);
774 return dat->real;
775}
776
777/*
778 * call-seq:
779 * imag -> numeric
780 *
781 * Returns the imaginary value for +self+:
782 *
783 * Complex.rect(7).imag # => 0
784 * Complex.rect(9, -4).imag # => -4
785 *
786 * If +self+ was created with
787 * {polar coordinates}[rdoc-ref:Complex@Polar+Coordinates], the returned value
788 * is computed, and may be inexact:
789 *
790 * Complex.polar(1, Math::PI/4).imag # => 0.7071067811865476 # Square root of 2.
791 *
792 */
793VALUE
794rb_complex_imag(VALUE self)
795{
796 get_dat1(self);
797 return dat->imag;
798}
799
800/*
801 * call-seq:
802 * -complex -> new_complex
803 *
804 * Returns the negation of +self+, which is the negation of each of its parts:
805 *
806 * -Complex.rect(1, 2) # => (-1-2i)
807 * -Complex.rect(-1, -2) # => (1+2i)
808 *
809 */
810VALUE
811rb_complex_uminus(VALUE self)
812{
813 get_dat1(self);
814 return f_complex_new2(CLASS_OF(self),
815 f_negate(dat->real), f_negate(dat->imag));
816}
817
818/*
819 * call-seq:
820 * complex + numeric -> new_complex
821 *
822 * Returns the sum of +self+ and +numeric+:
823 *
824 * Complex.rect(2, 3) + Complex.rect(2, 3) # => (4+6i)
825 * Complex.rect(900) + Complex.rect(1) # => (901+0i)
826 * Complex.rect(-2, 9) + Complex.rect(-9, 2) # => (-11+11i)
827 * Complex.rect(9, 8) + 4 # => (13+8i)
828 * Complex.rect(20, 9) + 9.8 # => (29.8+9i)
829 *
830 */
831VALUE
832rb_complex_plus(VALUE self, VALUE other)
833{
834 if (RB_TYPE_P(other, T_COMPLEX)) {
835 VALUE real, imag;
836
837 get_dat2(self, other);
838
839 real = f_add(adat->real, bdat->real);
840 imag = f_add(adat->imag, bdat->imag);
841
842 return f_complex_new2(CLASS_OF(self), real, imag);
843 }
844 if (k_numeric_p(other) && f_real_p(other)) {
845 get_dat1(self);
846
847 return f_complex_new2(CLASS_OF(self),
848 f_add(dat->real, other), dat->imag);
849 }
850 return rb_num_coerce_bin(self, other, '+');
851}
852
853/*
854 * call-seq:
855 * complex - numeric -> new_complex
856 *
857 * Returns the difference of +self+ and +numeric+:
858 *
859 * Complex.rect(2, 3) - Complex.rect(2, 3) # => (0+0i)
860 * Complex.rect(900) - Complex.rect(1) # => (899+0i)
861 * Complex.rect(-2, 9) - Complex.rect(-9, 2) # => (7+7i)
862 * Complex.rect(9, 8) - 4 # => (5+8i)
863 * Complex.rect(20, 9) - 9.8 # => (10.2+9i)
864 *
865 */
866VALUE
867rb_complex_minus(VALUE self, VALUE other)
868{
869 if (RB_TYPE_P(other, T_COMPLEX)) {
870 VALUE real, imag;
871
872 get_dat2(self, other);
873
874 real = f_sub(adat->real, bdat->real);
875 imag = f_sub(adat->imag, bdat->imag);
876
877 return f_complex_new2(CLASS_OF(self), real, imag);
878 }
879 if (k_numeric_p(other) && f_real_p(other)) {
880 get_dat1(self);
881
882 return f_complex_new2(CLASS_OF(self),
883 f_sub(dat->real, other), dat->imag);
884 }
885 return rb_num_coerce_bin(self, other, '-');
886}
887
888static VALUE
889safe_mul(VALUE a, VALUE b, bool az, bool bz)
890{
891 double v;
892 if (!az && bz && RB_FLOAT_TYPE_P(a) && (v = RFLOAT_VALUE(a), !isnan(v))) {
893 a = signbit(v) ? DBL2NUM(-1.0) : DBL2NUM(1.0);
894 }
895 if (!bz && az && RB_FLOAT_TYPE_P(b) && (v = RFLOAT_VALUE(b), !isnan(v))) {
896 b = signbit(v) ? DBL2NUM(-1.0) : DBL2NUM(1.0);
897 }
898 return f_mul(a, b);
899}
900
901static void
902comp_mul(VALUE areal, VALUE aimag, VALUE breal, VALUE bimag, VALUE *real, VALUE *imag)
903{
904 bool arzero = f_zero_p(areal);
905 bool aizero = f_zero_p(aimag);
906 bool brzero = f_zero_p(breal);
907 bool bizero = f_zero_p(bimag);
908 *real = f_sub(safe_mul(areal, breal, arzero, brzero),
909 safe_mul(aimag, bimag, aizero, bizero));
910 *imag = f_add(safe_mul(areal, bimag, arzero, bizero),
911 safe_mul(aimag, breal, aizero, brzero));
912}
913
914/*
915 * call-seq:
916 * complex * numeric -> new_complex
917 *
918 * Returns the product of +self+ and +numeric+:
919 *
920 * Complex.rect(2, 3) * Complex.rect(2, 3) # => (-5+12i)
921 * Complex.rect(900) * Complex.rect(1) # => (900+0i)
922 * Complex.rect(-2, 9) * Complex.rect(-9, 2) # => (0-85i)
923 * Complex.rect(9, 8) * 4 # => (36+32i)
924 * Complex.rect(20, 9) * 9.8 # => (196.0+88.2i)
925 *
926 */
927VALUE
928rb_complex_mul(VALUE self, VALUE other)
929{
930 if (RB_TYPE_P(other, T_COMPLEX)) {
931 VALUE real, imag;
932 get_dat2(self, other);
933
934 comp_mul(adat->real, adat->imag, bdat->real, bdat->imag, &real, &imag);
935
936 return f_complex_new2(CLASS_OF(self), real, imag);
937 }
938 if (k_numeric_p(other) && f_real_p(other)) {
939 get_dat1(self);
940
941 return f_complex_new2(CLASS_OF(self),
942 f_mul(dat->real, other),
943 f_mul(dat->imag, other));
944 }
945 return rb_num_coerce_bin(self, other, '*');
946}
947
948inline static VALUE
949f_divide(VALUE self, VALUE other,
950 VALUE (*func)(VALUE, VALUE), ID id)
951{
952 if (RB_TYPE_P(other, T_COMPLEX)) {
953 VALUE r, n, x, y;
954 int flo;
955 get_dat2(self, other);
956
957 flo = (RB_FLOAT_TYPE_P(adat->real) || RB_FLOAT_TYPE_P(adat->imag) ||
958 RB_FLOAT_TYPE_P(bdat->real) || RB_FLOAT_TYPE_P(bdat->imag));
959
960 if (f_gt_p(f_abs(bdat->real), f_abs(bdat->imag))) {
961 r = (*func)(bdat->imag, bdat->real);
962 n = f_mul(bdat->real, f_add(ONE, f_mul(r, r)));
963 x = (*func)(f_add(adat->real, f_mul(adat->imag, r)), n);
964 y = (*func)(f_sub(adat->imag, f_mul(adat->real, r)), n);
965 }
966 else {
967 r = (*func)(bdat->real, bdat->imag);
968 n = f_mul(bdat->imag, f_add(ONE, f_mul(r, r)));
969 x = (*func)(f_add(f_mul(adat->real, r), adat->imag), n);
970 y = (*func)(f_sub(f_mul(adat->imag, r), adat->real), n);
971 }
972 if (!flo) {
973 x = rb_rational_canonicalize(x);
974 y = rb_rational_canonicalize(y);
975 }
976 return f_complex_new2(CLASS_OF(self), x, y);
977 }
978 if (k_numeric_p(other) && f_real_p(other)) {
979 VALUE x, y;
980 get_dat1(self);
981 x = rb_rational_canonicalize((*func)(dat->real, other));
982 y = rb_rational_canonicalize((*func)(dat->imag, other));
983 return f_complex_new2(CLASS_OF(self), x, y);
984 }
985 return rb_num_coerce_bin(self, other, id);
986}
987
988#define rb_raise_zerodiv() rb_raise(rb_eZeroDivError, "divided by 0")
989
990/*
991 * call-seq:
992 * complex / numeric -> new_complex
993 *
994 * Returns the quotient of +self+ and +numeric+:
995 *
996 * Complex.rect(2, 3) / Complex.rect(2, 3) # => (1+0i)
997 * Complex.rect(900) / Complex.rect(1) # => (900+0i)
998 * Complex.rect(-2, 9) / Complex.rect(-9, 2) # => ((36/85)-(77/85)*i)
999 * Complex.rect(9, 8) / 4 # => ((9/4)+2i)
1000 * Complex.rect(20, 9) / 9.8 # => (2.0408163265306123+0.9183673469387754i)
1001 *
1002 */
1003VALUE
1004rb_complex_div(VALUE self, VALUE other)
1005{
1006 return f_divide(self, other, f_quo, id_quo);
1007}
1008
1009#define nucomp_quo rb_complex_div
1010
1011/*
1012 * call-seq:
1013 * fdiv(numeric) -> new_complex
1014 *
1015 * Returns <tt>Complex.rect(self.real/numeric, self.imag/numeric)</tt>:
1016 *
1017 * Complex.rect(11, 22).fdiv(3) # => (3.6666666666666665+7.333333333333333i)
1018 *
1019 */
1020static VALUE
1021nucomp_fdiv(VALUE self, VALUE other)
1022{
1023 return f_divide(self, other, f_fdiv, id_fdiv);
1024}
1025
1026inline static VALUE
1027f_reciprocal(VALUE x)
1028{
1029 return f_quo(ONE, x);
1030}
1031
1032static VALUE
1033zero_for(VALUE x)
1034{
1035 if (RB_FLOAT_TYPE_P(x))
1036 return DBL2NUM(0);
1037 if (RB_TYPE_P(x, T_RATIONAL))
1038 return rb_rational_new(INT2FIX(0), INT2FIX(1));
1039
1040 return INT2FIX(0);
1041}
1042
1043static VALUE
1044complex_pow_for_special_angle(VALUE self, VALUE other)
1045{
1046 if (!rb_integer_type_p(other)) {
1047 return Qundef;
1048 }
1049
1050 get_dat1(self);
1051 VALUE x = Qundef;
1052 int dir;
1053 if (f_zero_p(dat->imag)) {
1054 x = dat->real;
1055 dir = 0;
1056 }
1057 else if (f_zero_p(dat->real)) {
1058 x = dat->imag;
1059 dir = 2;
1060 }
1061 else if (f_eqeq_p(dat->real, dat->imag)) {
1062 x = dat->real;
1063 dir = 1;
1064 }
1065 else if (f_eqeq_p(dat->real, f_negate(dat->imag))) {
1066 x = dat->imag;
1067 dir = 3;
1068 }
1069 else {
1070 dir = 0;
1071 }
1072
1073 if (UNDEF_P(x)) return x;
1074
1075 if (f_negative_p(x)) {
1076 x = f_negate(x);
1077 dir += 4;
1078 }
1079
1080 VALUE zx;
1081 if (dir % 2 == 0) {
1082 zx = rb_num_pow(x, other);
1083 }
1084 else {
1085 zx = rb_num_pow(
1086 rb_funcall(rb_int_mul(TWO, x), '*', 1, x),
1087 rb_int_div(other, TWO)
1088 );
1089 if (rb_int_odd_p(other)) {
1090 zx = rb_funcall(zx, '*', 1, x);
1091 }
1092 }
1093 static const int dirs[][2] = {
1094 {1, 0}, {1, 1}, {0, 1}, {-1, 1}, {-1, 0}, {-1, -1}, {0, -1}, {1, -1}
1095 };
1096 int z_dir = FIX2INT(rb_int_modulo(rb_int_mul(INT2FIX(dir), other), INT2FIX(8)));
1097
1098 VALUE zr = Qfalse, zi = Qfalse;
1099 switch (dirs[z_dir][0]) {
1100 case 0: zr = zero_for(zx); break;
1101 case 1: zr = zx; break;
1102 case -1: zr = f_negate(zx); break;
1103 }
1104 switch (dirs[z_dir][1]) {
1105 case 0: zi = zero_for(zx); break;
1106 case 1: zi = zx; break;
1107 case -1: zi = f_negate(zx); break;
1108 }
1109 return nucomp_s_new_internal(CLASS_OF(self), zr, zi);
1110}
1111
1112
1113/*
1114 * call-seq:
1115 * complex ** numeric -> new_complex
1116 *
1117 * Returns +self+ raised to power +numeric+:
1118 *
1119 * Complex.rect(0, 1) ** 2 # => (-1+0i)
1120 * Complex.rect(-8) ** Rational(1, 3) # => (1.0000000000000002+1.7320508075688772i)
1121 *
1122 */
1123VALUE
1124rb_complex_pow(VALUE self, VALUE other)
1125{
1126 if (k_numeric_p(other) && k_exact_zero_p(other))
1127 return f_complex_new_bang1(CLASS_OF(self), ONE);
1128
1129 if (RB_TYPE_P(other, T_RATIONAL) && RRATIONAL(other)->den == LONG2FIX(1))
1130 other = RRATIONAL(other)->num; /* c14n */
1131
1132 if (RB_TYPE_P(other, T_COMPLEX)) {
1133 get_dat1(other);
1134
1135 if (k_exact_zero_p(dat->imag))
1136 other = dat->real; /* c14n */
1137 }
1138
1139 if (other == ONE) {
1140 get_dat1(self);
1141 return nucomp_s_new_internal(CLASS_OF(self), dat->real, dat->imag);
1142 }
1143
1144 VALUE result = complex_pow_for_special_angle(self, other);
1145 if (!UNDEF_P(result)) return result;
1146
1147 if (RB_TYPE_P(other, T_COMPLEX)) {
1148 VALUE r, theta, nr, ntheta;
1149
1150 get_dat1(other);
1151
1152 r = f_abs(self);
1153 theta = f_arg(self);
1154
1155 nr = m_exp_bang(f_sub(f_mul(dat->real, m_log_bang(r)),
1156 f_mul(dat->imag, theta)));
1157 ntheta = f_add(f_mul(theta, dat->real),
1158 f_mul(dat->imag, m_log_bang(r)));
1159 return f_complex_polar(CLASS_OF(self), nr, ntheta);
1160 }
1161 if (FIXNUM_P(other)) {
1162 long n = FIX2LONG(other);
1163 if (n == 0) {
1164 return nucomp_s_new_internal(CLASS_OF(self), ONE, ZERO);
1165 }
1166 if (n < 0) {
1167 self = f_reciprocal(self);
1168 other = rb_int_uminus(other);
1169 n = -n;
1170 }
1171 {
1172 get_dat1(self);
1173 VALUE xr = dat->real, xi = dat->imag, zr = xr, zi = xi;
1174
1175 if (f_zero_p(xi)) {
1176 zr = rb_num_pow(zr, other);
1177 }
1178 else if (f_zero_p(xr)) {
1179 zi = rb_num_pow(zi, other);
1180 if (n & 2) zi = f_negate(zi);
1181 if (!(n & 1)) {
1182 VALUE tmp = zr;
1183 zr = zi;
1184 zi = tmp;
1185 }
1186 }
1187 else {
1188 while (--n) {
1189 long q, r;
1190
1191 for (; q = n / 2, r = n % 2, r == 0; n = q) {
1192 VALUE tmp = f_sub(f_mul(xr, xr), f_mul(xi, xi));
1193 xi = f_mul(f_mul(TWO, xr), xi);
1194 xr = tmp;
1195 }
1196 comp_mul(zr, zi, xr, xi, &zr, &zi);
1197 }
1198 }
1199 return nucomp_s_new_internal(CLASS_OF(self), zr, zi);
1200 }
1201 }
1202 if (k_numeric_p(other) && f_real_p(other)) {
1203 VALUE r, theta;
1204
1205 if (RB_BIGNUM_TYPE_P(other))
1206 rb_warn("in a**b, b may be too big");
1207
1208 r = f_abs(self);
1209 theta = f_arg(self);
1210
1211 return f_complex_polar(CLASS_OF(self), f_expt(r, other),
1212 f_mul(theta, other));
1213 }
1214 return rb_num_coerce_bin(self, other, id_expt);
1215}
1216
1217/*
1218 * call-seq:
1219 * complex == object -> true or false
1220 *
1221 * Returns +true+ if <tt>self.real == object.real</tt>
1222 * and <tt>self.imag == object.imag</tt>:
1223 *
1224 * Complex.rect(2, 3) == Complex.rect(2.0, 3.0) # => true
1225 *
1226 */
1227static VALUE
1228nucomp_eqeq_p(VALUE self, VALUE other)
1229{
1230 if (RB_TYPE_P(other, T_COMPLEX)) {
1231 get_dat2(self, other);
1232
1233 return RBOOL(f_eqeq_p(adat->real, bdat->real) &&
1234 f_eqeq_p(adat->imag, bdat->imag));
1235 }
1236 if (k_numeric_p(other) && f_real_p(other)) {
1237 get_dat1(self);
1238
1239 return RBOOL(f_eqeq_p(dat->real, other) && f_zero_p(dat->imag));
1240 }
1241 return RBOOL(f_eqeq_p(other, self));
1242}
1243
1244static bool
1245nucomp_real_p(VALUE self)
1246{
1247 get_dat1(self);
1248 return f_zero_p(dat->imag);
1249}
1250
1251/*
1252 * call-seq:
1253 * complex <=> object -> -1, 0, 1, or nil
1254 *
1255 * Returns:
1256 *
1257 * - <tt>self.real <=> object.real</tt> if both of the following are true:
1258 *
1259 * - <tt>self.imag == 0</tt>.
1260 * - <tt>object.imag == 0</tt>. # Always true if object is numeric but not complex.
1261 *
1262 * - +nil+ otherwise.
1263 *
1264 * Examples:
1265 *
1266 * Complex.rect(2) <=> 3 # => -1
1267 * Complex.rect(2) <=> 2 # => 0
1268 * Complex.rect(2) <=> 1 # => 1
1269 * Complex.rect(2, 1) <=> 1 # => nil # self.imag not zero.
1270 * Complex.rect(1) <=> Complex.rect(1, 1) # => nil # object.imag not zero.
1271 * Complex.rect(1) <=> 'Foo' # => nil # object.imag not defined.
1272 *
1273 */
1274static VALUE
1275nucomp_cmp(VALUE self, VALUE other)
1276{
1277 if (!k_numeric_p(other)) {
1278 return rb_num_coerce_cmp(self, other, idCmp);
1279 }
1280 if (!nucomp_real_p(self)) {
1281 return Qnil;
1282 }
1283 if (RB_TYPE_P(other, T_COMPLEX)) {
1284 if (nucomp_real_p(other)) {
1285 get_dat2(self, other);
1286 return rb_funcall(adat->real, idCmp, 1, bdat->real);
1287 }
1288 }
1289 else {
1290 get_dat1(self);
1291 if (f_real_p(other)) {
1292 return rb_funcall(dat->real, idCmp, 1, other);
1293 }
1294 else {
1295 return rb_num_coerce_cmp(dat->real, other, idCmp);
1296 }
1297 }
1298 return Qnil;
1299}
1300
1301/* :nodoc: */
1302static VALUE
1303nucomp_coerce(VALUE self, VALUE other)
1304{
1305 if (RB_TYPE_P(other, T_COMPLEX))
1306 return rb_assoc_new(other, self);
1307 if (k_numeric_p(other) && f_real_p(other))
1308 return rb_assoc_new(f_complex_new_bang1(CLASS_OF(self), other), self);
1309
1310 rb_raise(rb_eTypeError, "%"PRIsVALUE" can't be coerced into %"PRIsVALUE,
1311 rb_obj_class(other), rb_obj_class(self));
1312 return Qnil;
1313}
1314
1315/*
1316 * call-seq:
1317 * abs -> float
1318 *
1319 * Returns the absolute value (magnitude) for +self+;
1320 * see {polar coordinates}[rdoc-ref:Complex@Polar+Coordinates]:
1321 *
1322 * Complex.polar(-1, 0).abs # => 1.0
1323 *
1324 * If +self+ was created with
1325 * {rectangular coordinates}[rdoc-ref:Complex@Rectangular+Coordinates], the returned value
1326 * is computed, and may be inexact:
1327 *
1328 * Complex.rectangular(1, 1).abs # => 1.4142135623730951 # The square root of 2.
1329 *
1330 */
1331VALUE
1332rb_complex_abs(VALUE self)
1333{
1334 get_dat1(self);
1335
1336 if (f_zero_p(dat->real)) {
1337 VALUE a = f_abs(dat->imag);
1338 if (RB_FLOAT_TYPE_P(dat->real) && !RB_FLOAT_TYPE_P(dat->imag))
1339 a = f_to_f(a);
1340 return a;
1341 }
1342 if (f_zero_p(dat->imag)) {
1343 VALUE a = f_abs(dat->real);
1344 if (!RB_FLOAT_TYPE_P(dat->real) && RB_FLOAT_TYPE_P(dat->imag))
1345 a = f_to_f(a);
1346 return a;
1347 }
1348 return rb_math_hypot(dat->real, dat->imag);
1349}
1350
1351/*
1352 * call-seq:
1353 * abs2 -> float
1354 *
1355 * Returns square of the absolute value (magnitude) for +self+;
1356 * see {polar coordinates}[rdoc-ref:Complex@Polar+Coordinates]:
1357 *
1358 * Complex.polar(2, 2).abs2 # => 4.0
1359 *
1360 * If +self+ was created with
1361 * {rectangular coordinates}[rdoc-ref:Complex@Rectangular+Coordinates], the returned value
1362 * is computed, and may be inexact:
1363 *
1364 * Complex.rectangular(1.0/3, 1.0/3).abs2 # => 0.2222222222222222
1365 *
1366 */
1367static VALUE
1368nucomp_abs2(VALUE self)
1369{
1370 get_dat1(self);
1371 return f_add(f_mul(dat->real, dat->real),
1372 f_mul(dat->imag, dat->imag));
1373}
1374
1375/*
1376 * call-seq:
1377 * arg -> float
1378 *
1379 * Returns the argument (angle) for +self+ in radians;
1380 * see {polar coordinates}[rdoc-ref:Complex@Polar+Coordinates]:
1381 *
1382 * Complex.polar(3, Math::PI/2).arg # => 1.57079632679489660
1383 *
1384 * If +self+ was created with
1385 * {rectangular coordinates}[rdoc-ref:Complex@Rectangular+Coordinates], the returned value
1386 * is computed, and may be inexact:
1387 *
1388 * Complex.polar(1, 1.0/3).arg # => 0.33333333333333326
1389 *
1390 */
1391VALUE
1392rb_complex_arg(VALUE self)
1393{
1394 get_dat1(self);
1395 return rb_math_atan2(dat->imag, dat->real);
1396}
1397
1398/*
1399 * call-seq:
1400 * rect -> array
1401 *
1402 * Returns the array <tt>[self.real, self.imag]</tt>:
1403 *
1404 * Complex.rect(1, 2).rect # => [1, 2]
1405 *
1406 * See {Rectangular Coordinates}[rdoc-ref:Complex@Rectangular+Coordinates].
1407 *
1408 * If +self+ was created with
1409 * {polar coordinates}[rdoc-ref:Complex@Polar+Coordinates], the returned value
1410 * is computed, and may be inexact:
1411 *
1412 * Complex.polar(1.0, 1.0).rect # => [0.5403023058681398, 0.8414709848078965]
1413 *
1414 *
1415 * Complex#rectangular is an alias for Complex#rect.
1416 */
1417static VALUE
1418nucomp_rect(VALUE self)
1419{
1420 get_dat1(self);
1421 return rb_assoc_new(dat->real, dat->imag);
1422}
1423
1424/*
1425 * call-seq:
1426 * polar -> array
1427 *
1428 * Returns the array <tt>[self.abs, self.arg]</tt>:
1429 *
1430 * Complex.polar(1, 2).polar # => [1.0, 2.0]
1431 *
1432 * See {Polar Coordinates}[rdoc-ref:Complex@Polar+Coordinates].
1433 *
1434 * If +self+ was created with
1435 * {rectangular coordinates}[rdoc-ref:Complex@Rectangular+Coordinates], the returned value
1436 * is computed, and may be inexact:
1437 *
1438 * Complex.rect(1, 1).polar # => [1.4142135623730951, 0.7853981633974483]
1439 *
1440 */
1441static VALUE
1442nucomp_polar(VALUE self)
1443{
1444 return rb_assoc_new(f_abs(self), f_arg(self));
1445}
1446
1447/*
1448 * call-seq:
1449 * conj -> complex
1450 *
1451 * Returns the conjugate of +self+, <tt>Complex.rect(self.imag, self.real)</tt>:
1452 *
1453 * Complex.rect(1, 2).conj # => (1-2i)
1454 *
1455 */
1456VALUE
1457rb_complex_conjugate(VALUE self)
1458{
1459 get_dat1(self);
1460 return f_complex_new2(CLASS_OF(self), dat->real, f_negate(dat->imag));
1461}
1462
1463/*
1464 * call-seq:
1465 * real? -> false
1466 *
1467 * Returns +false+; for compatibility with Numeric#real?.
1468 */
1469static VALUE
1470nucomp_real_p_m(VALUE self)
1471{
1472 return Qfalse;
1473}
1474
1475/*
1476 * call-seq:
1477 * denominator -> integer
1478 *
1479 * Returns the denominator of +self+, which is
1480 * the {least common multiple}[https://en.wikipedia.org/wiki/Least_common_multiple]
1481 * of <tt>self.real.denominator</tt> and <tt>self.imag.denominator</tt>:
1482 *
1483 * Complex.rect(Rational(1, 2), Rational(2, 3)).denominator # => 6
1484 *
1485 * Note that <tt>n.denominator</tt> of a non-rational numeric is +1+.
1486 *
1487 * Related: Complex#numerator.
1488 */
1489static VALUE
1490nucomp_denominator(VALUE self)
1491{
1492 get_dat1(self);
1493 return rb_lcm(f_denominator(dat->real), f_denominator(dat->imag));
1494}
1495
1496/*
1497 * call-seq:
1498 * numerator -> new_complex
1499 *
1500 * Returns the \Complex object created from the numerators
1501 * of the real and imaginary parts of +self+,
1502 * after converting each part to the
1503 * {lowest common denominator}[https://en.wikipedia.org/wiki/Lowest_common_denominator]
1504 * of the two:
1505 *
1506 * c = Complex.rect(Rational(2, 3), Rational(3, 4)) # => ((2/3)+(3/4)*i)
1507 * c.numerator # => (8+9i)
1508 *
1509 * In this example, the lowest common denominator of the two parts is 12;
1510 * the two converted parts may be thought of as \Rational(8, 12) and \Rational(9, 12),
1511 * whose numerators, respectively, are 8 and 9;
1512 * so the returned value of <tt>c.numerator</tt> is <tt>Complex.rect(8, 9)</tt>.
1513 *
1514 * Related: Complex#denominator.
1515 */
1516static VALUE
1517nucomp_numerator(VALUE self)
1518{
1519 VALUE cd;
1520
1521 get_dat1(self);
1522
1523 cd = nucomp_denominator(self);
1524 return f_complex_new2(CLASS_OF(self),
1525 f_mul(f_numerator(dat->real),
1526 f_div(cd, f_denominator(dat->real))),
1527 f_mul(f_numerator(dat->imag),
1528 f_div(cd, f_denominator(dat->imag))));
1529}
1530
1531/* :nodoc: */
1532st_index_t
1533rb_complex_hash(VALUE self)
1534{
1535 st_index_t v, h[2];
1536 VALUE n;
1537
1538 get_dat1(self);
1539 n = rb_hash(dat->real);
1540 h[0] = NUM2LONG(n);
1541 n = rb_hash(dat->imag);
1542 h[1] = NUM2LONG(n);
1543 v = rb_memhash(h, sizeof(h));
1544 return v;
1545}
1546
1547/*
1548 * :call-seq:
1549 * hash -> integer
1550 *
1551 * Returns the integer hash value for +self+.
1552 *
1553 * Two \Complex objects created from the same values will have the same hash value
1554 * (and will compare using #eql?):
1555 *
1556 * Complex.rect(1, 2).hash == Complex.rect(1, 2).hash # => true
1557 *
1558 */
1559static VALUE
1560nucomp_hash(VALUE self)
1561{
1562 return ST2FIX(rb_complex_hash(self));
1563}
1564
1565/* :nodoc: */
1566static VALUE
1567nucomp_eql_p(VALUE self, VALUE other)
1568{
1569 if (RB_TYPE_P(other, T_COMPLEX)) {
1570 get_dat2(self, other);
1571
1572 return RBOOL((CLASS_OF(adat->real) == CLASS_OF(bdat->real)) &&
1573 (CLASS_OF(adat->imag) == CLASS_OF(bdat->imag)) &&
1574 f_eqeq_p(self, other));
1575
1576 }
1577 return Qfalse;
1578}
1579
1580inline static int
1581f_signbit(VALUE x)
1582{
1583 if (RB_FLOAT_TYPE_P(x)) {
1584 double f = RFLOAT_VALUE(x);
1585 return !isnan(f) && signbit(f);
1586 }
1587 return f_negative_p(x);
1588}
1589
1590inline static int
1591f_tpositive_p(VALUE x)
1592{
1593 return !f_signbit(x);
1594}
1595
1596static VALUE
1597f_format(VALUE self, VALUE s, VALUE (*func)(VALUE))
1598{
1599 int impos;
1600
1601 get_dat1(self);
1602
1603 impos = f_tpositive_p(dat->imag);
1604
1605 rb_str_concat(s, (*func)(dat->real));
1606 rb_str_cat2(s, !impos ? "-" : "+");
1607
1608 rb_str_concat(s, (*func)(f_abs(dat->imag)));
1609 if (!rb_isdigit(RSTRING_PTR(s)[RSTRING_LEN(s) - 1]))
1610 rb_str_cat2(s, "*");
1611 rb_str_cat2(s, "i");
1612
1613 return s;
1614}
1615
1616/*
1617 * call-seq:
1618 * to_s -> string
1619 *
1620 * Returns a string representation of +self+:
1621 *
1622 * Complex.rect(2).to_s # => "2+0i"
1623 * Complex.rect(-8, 6).to_s # => "-8+6i"
1624 * Complex.rect(0, Rational(1, 2)).to_s # => "0+1/2i"
1625 * Complex.rect(0, Float::INFINITY).to_s # => "0+Infinity*i"
1626 * Complex.rect(Float::NAN, Float::NAN).to_s # => "NaN+NaN*i"
1627 *
1628 */
1629static VALUE
1630nucomp_to_s(VALUE self)
1631{
1632 return f_format(self, rb_usascii_str_new2(""), rb_String);
1633}
1634
1635/*
1636 * call-seq:
1637 * inspect -> string
1638 *
1639 * Returns a string representation of +self+:
1640 *
1641 * Complex.rect(2).inspect # => "(2+0i)"
1642 * Complex.rect(-8, 6).inspect # => "(-8+6i)"
1643 * Complex.rect(0, Rational(1, 2)).inspect # => "(0+(1/2)*i)"
1644 * Complex.rect(0, Float::INFINITY).inspect # => "(0+Infinity*i)"
1645 * Complex.rect(Float::NAN, Float::NAN).inspect # => "(NaN+NaN*i)"
1646 *
1647 */
1648static VALUE
1649nucomp_inspect(VALUE self)
1650{
1651 VALUE s;
1652
1653 s = rb_usascii_str_new2("(");
1654 f_format(self, s, rb_inspect);
1655 rb_str_cat2(s, ")");
1656
1657 return s;
1658}
1659
1660#define FINITE_TYPE_P(v) (RB_INTEGER_TYPE_P(v) || RB_TYPE_P(v, T_RATIONAL))
1661
1662/*
1663 * call-seq:
1664 * finite? -> true or false
1665 *
1666 * Returns +true+ if both <tt>self.real.finite?</tt> and <tt>self.imag.finite?</tt>
1667 * are true, +false+ otherwise:
1668 *
1669 * Complex.rect(1, 1).finite? # => true
1670 * Complex.rect(Float::INFINITY, 0).finite? # => false
1671 *
1672 * Related: Numeric#finite?, Float#finite?.
1673 */
1674static VALUE
1675rb_complex_finite_p(VALUE self)
1676{
1677 get_dat1(self);
1678
1679 return RBOOL(f_finite_p(dat->real) && f_finite_p(dat->imag));
1680}
1681
1682/*
1683 * call-seq:
1684 * infinite? -> 1 or nil
1685 *
1686 * Returns +1+ if either <tt>self.real.infinite?</tt> or <tt>self.imag.infinite?</tt>
1687 * is true, +nil+ otherwise:
1688 *
1689 * Complex.rect(Float::INFINITY, 0).infinite? # => 1
1690 * Complex.rect(1, 1).infinite? # => nil
1691 *
1692 * Related: Numeric#infinite?, Float#infinite?.
1693 */
1694static VALUE
1695rb_complex_infinite_p(VALUE self)
1696{
1697 get_dat1(self);
1698
1699 if (!f_infinite_p(dat->real) && !f_infinite_p(dat->imag)) {
1700 return Qnil;
1701 }
1702 return ONE;
1703}
1704
1705/* :nodoc: */
1706static VALUE
1707nucomp_dumper(VALUE self)
1708{
1709 return self;
1710}
1711
1712/* :nodoc: */
1713static VALUE
1714nucomp_loader(VALUE self, VALUE a)
1715{
1716 get_dat1(self);
1717
1718 RCOMPLEX_SET_REAL(dat, rb_ivar_get(a, id_i_real));
1719 RCOMPLEX_SET_IMAG(dat, rb_ivar_get(a, id_i_imag));
1720 OBJ_FREEZE(self);
1721
1722 return self;
1723}
1724
1725/* :nodoc: */
1726static VALUE
1727nucomp_marshal_dump(VALUE self)
1728{
1729 VALUE a;
1730 get_dat1(self);
1731
1732 a = rb_assoc_new(dat->real, dat->imag);
1733 rb_copy_generic_ivar(a, self);
1734 return a;
1735}
1736
1737/* :nodoc: */
1738static VALUE
1739nucomp_marshal_load(VALUE self, VALUE a)
1740{
1741 Check_Type(a, T_ARRAY);
1742 if (RARRAY_LEN(a) != 2)
1743 rb_raise(rb_eArgError, "marshaled complex must have an array whose length is 2 but %ld", RARRAY_LEN(a));
1744 rb_ivar_set(self, id_i_real, RARRAY_AREF(a, 0));
1745 rb_ivar_set(self, id_i_imag, RARRAY_AREF(a, 1));
1746 return self;
1747}
1748
1749VALUE
1750rb_complex_raw(VALUE x, VALUE y)
1751{
1752 return nucomp_s_new_internal(rb_cComplex, x, y);
1753}
1754
1755VALUE
1756rb_complex_new(VALUE x, VALUE y)
1757{
1758 return nucomp_s_canonicalize_internal(rb_cComplex, x, y);
1759}
1760
1761VALUE
1762rb_complex_new_polar(VALUE x, VALUE y)
1763{
1764 return f_complex_polar(rb_cComplex, x, y);
1765}
1766
1767VALUE
1769{
1770 return rb_complex_new_polar(x, y);
1771}
1772
1773VALUE
1774rb_Complex(VALUE x, VALUE y)
1775{
1776 VALUE a[2];
1777 a[0] = x;
1778 a[1] = y;
1779 return nucomp_s_convert(2, a, rb_cComplex);
1780}
1781
1782VALUE
1783rb_dbl_complex_new(double real, double imag)
1784{
1785 return rb_complex_raw(DBL2NUM(real), DBL2NUM(imag));
1786}
1787
1788/*
1789 * call-seq:
1790 * to_i -> integer
1791 *
1792 * Returns the value of <tt>self.real</tt> as an Integer, if possible:
1793 *
1794 * Complex.rect(1, 0).to_i # => 1
1795 * Complex.rect(1, Rational(0, 1)).to_i # => 1
1796 *
1797 * Raises RangeError if <tt>self.imag</tt> is not exactly zero
1798 * (either <tt>Integer(0)</tt> or <tt>Rational(0, _n_)</tt>).
1799 */
1800static VALUE
1801nucomp_to_i(VALUE self)
1802{
1803 get_dat1(self);
1804
1805 if (!k_exact_zero_p(dat->imag)) {
1806 rb_raise(rb_eRangeError, "can't convert %"PRIsVALUE" into Integer",
1807 self);
1808 }
1809 return f_to_i(dat->real);
1810}
1811
1812/*
1813 * call-seq:
1814 * to_f -> float
1815 *
1816 * Returns the value of <tt>self.real</tt> as a Float, if possible:
1817 *
1818 * Complex.rect(1, 0).to_f # => 1.0
1819 * Complex.rect(1, Rational(0, 1)).to_f # => 1.0
1820 *
1821 * Raises RangeError if <tt>self.imag</tt> is not exactly zero
1822 * (either <tt>Integer(0)</tt> or <tt>Rational(0, _n_)</tt>).
1823 */
1824static VALUE
1825nucomp_to_f(VALUE self)
1826{
1827 get_dat1(self);
1828
1829 if (!k_exact_zero_p(dat->imag)) {
1830 rb_raise(rb_eRangeError, "can't convert %"PRIsVALUE" into Float",
1831 self);
1832 }
1833 return f_to_f(dat->real);
1834}
1835
1836/*
1837 * call-seq:
1838 * to_r -> rational
1839 *
1840 * Returns the value of <tt>self.real</tt> as a Rational, if possible:
1841 *
1842 * Complex.rect(1, 0).to_r # => (1/1)
1843 * Complex.rect(1, Rational(0, 1)).to_r # => (1/1)
1844 * Complex.rect(1, 0.0).to_r # => (1/1)
1845 *
1846 * Raises RangeError if <tt>self.imag</tt> is not exactly zero
1847 * (either <tt>Integer(0)</tt> or <tt>Rational(0, _n_)</tt>)
1848 * and <tt>self.imag.to_r</tt> is not exactly zero.
1849 *
1850 * Related: Complex#rationalize.
1851 */
1852static VALUE
1853nucomp_to_r(VALUE self)
1854{
1855 get_dat1(self);
1856
1857 if (RB_FLOAT_TYPE_P(dat->imag) && FLOAT_ZERO_P(dat->imag)) {
1858 /* Do nothing here */
1859 }
1860 else if (!k_exact_zero_p(dat->imag)) {
1861 VALUE imag = rb_check_convert_type_with_id(dat->imag, T_RATIONAL, "Rational", idTo_r);
1862 if (NIL_P(imag) || !k_exact_zero_p(imag)) {
1863 rb_raise(rb_eRangeError, "can't convert %"PRIsVALUE" into Rational",
1864 self);
1865 }
1866 }
1867 return f_to_r(dat->real);
1868}
1869
1870/*
1871 * call-seq:
1872 * rationalize(epsilon = nil) -> rational
1873 *
1874 * Returns a Rational object whose value is exactly or approximately
1875 * equivalent to that of <tt>self.real</tt>.
1876 *
1877 * With no argument +epsilon+ given, returns a \Rational object
1878 * whose value is exactly equal to that of <tt>self.real.rationalize</tt>:
1879 *
1880 * Complex.rect(1, 0).rationalize # => (1/1)
1881 * Complex.rect(1, Rational(0, 1)).rationalize # => (1/1)
1882 * Complex.rect(3.14159, 0).rationalize # => (314159/100000)
1883 *
1884 * With argument +epsilon+ given, returns a \Rational object
1885 * whose value is exactly or approximately equal to that of <tt>self.real</tt>
1886 * to the given precision:
1887 *
1888 * Complex.rect(3.14159, 0).rationalize(0.1) # => (16/5)
1889 * Complex.rect(3.14159, 0).rationalize(0.01) # => (22/7)
1890 * Complex.rect(3.14159, 0).rationalize(0.001) # => (201/64)
1891 * Complex.rect(3.14159, 0).rationalize(0.0001) # => (333/106)
1892 * Complex.rect(3.14159, 0).rationalize(0.00001) # => (355/113)
1893 * Complex.rect(3.14159, 0).rationalize(0.000001) # => (7433/2366)
1894 * Complex.rect(3.14159, 0).rationalize(0.0000001) # => (9208/2931)
1895 * Complex.rect(3.14159, 0).rationalize(0.00000001) # => (47460/15107)
1896 * Complex.rect(3.14159, 0).rationalize(0.000000001) # => (76149/24239)
1897 * Complex.rect(3.14159, 0).rationalize(0.0000000001) # => (314159/100000)
1898 * Complex.rect(3.14159, 0).rationalize(0.0) # => (3537115888337719/1125899906842624)
1899 *
1900 * Related: Complex#to_r.
1901 */
1902static VALUE
1903nucomp_rationalize(int argc, VALUE *argv, VALUE self)
1904{
1905 get_dat1(self);
1906
1907 rb_check_arity(argc, 0, 1);
1908
1909 if (!k_exact_zero_p(dat->imag)) {
1910 rb_raise(rb_eRangeError, "can't convert %"PRIsVALUE" into Rational",
1911 self);
1912 }
1913 return rb_funcallv(dat->real, id_rationalize, argc, argv);
1914}
1915
1916/*
1917 * call-seq:
1918 * to_c -> self
1919 *
1920 * Returns +self+.
1921 */
1922static VALUE
1923nucomp_to_c(VALUE self)
1924{
1925 return self;
1926}
1927
1928/*
1929 * call-seq:
1930 * to_c -> complex
1931 *
1932 * Returns +self+ as a Complex object.
1933 */
1934static VALUE
1935numeric_to_c(VALUE self)
1936{
1937 return rb_complex_new1(self);
1938}
1939
1940inline static int
1941issign(int c)
1942{
1943 return (c == '-' || c == '+');
1944}
1945
1946static int
1947read_sign(const char **s,
1948 char **b)
1949{
1950 int sign = '?';
1951
1952 if (issign(**s)) {
1953 sign = **b = **s;
1954 (*s)++;
1955 (*b)++;
1956 }
1957 return sign;
1958}
1959
1960inline static int
1961isdecimal(int c)
1962{
1963 return isdigit((unsigned char)c);
1964}
1965
1966static int
1967read_digits(const char **s, int strict,
1968 char **b)
1969{
1970 int us = 1;
1971
1972 if (!isdecimal(**s))
1973 return 0;
1974
1975 while (isdecimal(**s) || **s == '_') {
1976 if (**s == '_') {
1977 if (us) {
1978 if (strict) return 0;
1979 break;
1980 }
1981 us = 1;
1982 }
1983 else {
1984 **b = **s;
1985 (*b)++;
1986 us = 0;
1987 }
1988 (*s)++;
1989 }
1990 if (us)
1991 do {
1992 (*s)--;
1993 } while (**s == '_');
1994 return 1;
1995}
1996
1997inline static int
1998islettere(int c)
1999{
2000 return (c == 'e' || c == 'E');
2001}
2002
2003static int
2004read_num(const char **s, int strict,
2005 char **b)
2006{
2007 if (**s != '.') {
2008 if (!read_digits(s, strict, b))
2009 return 0;
2010 }
2011
2012 if (**s == '.') {
2013 **b = **s;
2014 (*s)++;
2015 (*b)++;
2016 if (!read_digits(s, strict, b)) {
2017 (*b)--;
2018 return 0;
2019 }
2020 }
2021
2022 if (islettere(**s)) {
2023 **b = **s;
2024 (*s)++;
2025 (*b)++;
2026 read_sign(s, b);
2027 if (!read_digits(s, strict, b)) {
2028 (*b)--;
2029 return 0;
2030 }
2031 }
2032 return 1;
2033}
2034
2035inline static int
2036read_den(const char **s, int strict,
2037 char **b)
2038{
2039 if (!read_digits(s, strict, b))
2040 return 0;
2041 return 1;
2042}
2043
2044static int
2045read_rat_nos(const char **s, int strict,
2046 char **b)
2047{
2048 if (!read_num(s, strict, b))
2049 return 0;
2050 if (**s == '/') {
2051 **b = **s;
2052 (*s)++;
2053 (*b)++;
2054 if (!read_den(s, strict, b)) {
2055 (*b)--;
2056 return 0;
2057 }
2058 }
2059 return 1;
2060}
2061
2062static int
2063read_rat(const char **s, int strict,
2064 char **b)
2065{
2066 read_sign(s, b);
2067 if (!read_rat_nos(s, strict, b))
2068 return 0;
2069 return 1;
2070}
2071
2072inline static int
2073isimagunit(int c)
2074{
2075 return (c == 'i' || c == 'I' ||
2076 c == 'j' || c == 'J');
2077}
2078
2079static VALUE
2080str2num(char *s)
2081{
2082 if (strchr(s, '/'))
2083 return rb_cstr_to_rat(s, 0);
2084 if (strpbrk(s, ".eE"))
2085 return DBL2NUM(rb_cstr_to_dbl(s, 0));
2086 return rb_cstr_to_inum(s, 10, 0);
2087}
2088
2089static int
2090read_comp(const char **s, int strict,
2091 VALUE *ret, char **b)
2092{
2093 char *bb;
2094 int sign;
2095 VALUE num, num2;
2096
2097 bb = *b;
2098
2099 sign = read_sign(s, b);
2100
2101 if (isimagunit(**s)) {
2102 (*s)++;
2103 num = INT2FIX((sign == '-') ? -1 : + 1);
2104 *ret = rb_complex_new2(ZERO, num);
2105 return 1; /* e.g. "i" */
2106 }
2107
2108 if (!read_rat_nos(s, strict, b)) {
2109 **b = '\0';
2110 num = str2num(bb);
2111 *ret = rb_complex_new2(num, ZERO);
2112 return 0; /* e.g. "-" */
2113 }
2114 **b = '\0';
2115 num = str2num(bb);
2116
2117 if (isimagunit(**s)) {
2118 (*s)++;
2119 *ret = rb_complex_new2(ZERO, num);
2120 return 1; /* e.g. "3i" */
2121 }
2122
2123 if (**s == '@') {
2124 int st;
2125
2126 (*s)++;
2127 bb = *b;
2128 st = read_rat(s, strict, b);
2129 **b = '\0';
2130 if (strlen(bb) < 1 ||
2131 !isdecimal(*(bb + strlen(bb) - 1))) {
2132 *ret = rb_complex_new2(num, ZERO);
2133 return 0; /* e.g. "1@-" */
2134 }
2135 num2 = str2num(bb);
2136 *ret = rb_complex_new_polar(num, num2);
2137 if (!st)
2138 return 0; /* e.g. "1@2." */
2139 else
2140 return 1; /* e.g. "1@2" */
2141 }
2142
2143 if (issign(**s)) {
2144 bb = *b;
2145 sign = read_sign(s, b);
2146 if (isimagunit(**s))
2147 num2 = INT2FIX((sign == '-') ? -1 : + 1);
2148 else {
2149 if (!read_rat_nos(s, strict, b)) {
2150 *ret = rb_complex_new2(num, ZERO);
2151 return 0; /* e.g. "1+xi" */
2152 }
2153 **b = '\0';
2154 num2 = str2num(bb);
2155 }
2156 if (!isimagunit(**s)) {
2157 *ret = rb_complex_new2(num, ZERO);
2158 return 0; /* e.g. "1+3x" */
2159 }
2160 (*s)++;
2161 *ret = rb_complex_new2(num, num2);
2162 return 1; /* e.g. "1+2i" */
2163 }
2164 /* !(@, - or +) */
2165 {
2166 *ret = rb_complex_new2(num, ZERO);
2167 return 1; /* e.g. "3" */
2168 }
2169}
2170
2171inline static void
2172skip_ws(const char **s)
2173{
2174 while (isspace((unsigned char)**s))
2175 (*s)++;
2176}
2177
2178static int
2179parse_comp(const char *s, int strict, VALUE *num)
2180{
2181 char *buf, *b;
2182 VALUE tmp;
2183 int ret = 1;
2184
2185 buf = ALLOCV_N(char, tmp, strlen(s) + 1);
2186 b = buf;
2187
2188 skip_ws(&s);
2189 if (!read_comp(&s, strict, num, &b)) {
2190 ret = 0;
2191 }
2192 else {
2193 skip_ws(&s);
2194
2195 if (strict)
2196 if (*s != '\0')
2197 ret = 0;
2198 }
2199 ALLOCV_END(tmp);
2200
2201 return ret;
2202}
2203
2204static VALUE
2205string_to_c_strict(VALUE self, int raise)
2206{
2207 char *s;
2208 VALUE num;
2209
2210 rb_must_asciicompat(self);
2211
2212 if (raise) {
2213 s = StringValueCStr(self);
2214 }
2215 else if (!(s = rb_str_to_cstr(self))) {
2216 return Qnil;
2217 }
2218
2219 if (!parse_comp(s, TRUE, &num)) {
2220 if (!raise) return Qnil;
2221 rb_raise(rb_eArgError, "invalid value for convert(): %+"PRIsVALUE,
2222 self);
2223 }
2224
2225 return num;
2226}
2227
2228/*
2229 * call-seq:
2230 * to_c -> complex
2231 *
2232 * Returns +self+ interpreted as a Complex object;
2233 * leading whitespace and trailing garbage are ignored:
2234 *
2235 * '9'.to_c # => (9+0i)
2236 * '2.5'.to_c # => (2.5+0i)
2237 * '2.5/1'.to_c # => ((5/2)+0i)
2238 * '-3/2'.to_c # => ((-3/2)+0i)
2239 * '-i'.to_c # => (0-1i)
2240 * '45i'.to_c # => (0+45i)
2241 * '3-4i'.to_c # => (3-4i)
2242 * '-4e2-4e-2i'.to_c # => (-400.0-0.04i)
2243 * '-0.0-0.0i'.to_c # => (-0.0-0.0i)
2244 * '1/2+3/4i'.to_c # => ((1/2)+(3/4)*i)
2245 * '1.0@0'.to_c # => (1+0.0i)
2246 * "1.0@#{Math::PI/2}".to_c # => (0.0+1i)
2247 * "1.0@#{Math::PI}".to_c # => (-1+0.0i)
2248 *
2249 * Returns \Complex zero if the string cannot be converted:
2250 *
2251 * 'ruby'.to_c # => (0+0i)
2252 *
2253 * See Kernel#Complex.
2254 */
2255static VALUE
2256string_to_c(VALUE self)
2257{
2258 VALUE num;
2259
2260 rb_must_asciicompat(self);
2261
2262 (void)parse_comp(rb_str_fill_terminator(self, 1), FALSE, &num);
2263
2264 return num;
2265}
2266
2267static VALUE
2268to_complex(VALUE val)
2269{
2270 return rb_convert_type(val, T_COMPLEX, "Complex", "to_c");
2271}
2272
2273static VALUE
2274nucomp_convert(VALUE klass, VALUE a1, VALUE a2, int raise)
2275{
2276 if (NIL_P(a1) || NIL_P(a2)) {
2277 if (!raise) return Qnil;
2278 rb_raise(rb_eTypeError, "can't convert nil into Complex");
2279 }
2280
2281 if (RB_TYPE_P(a1, T_STRING)) {
2282 a1 = string_to_c_strict(a1, raise);
2283 if (NIL_P(a1)) return Qnil;
2284 }
2285
2286 if (RB_TYPE_P(a2, T_STRING)) {
2287 a2 = string_to_c_strict(a2, raise);
2288 if (NIL_P(a2)) return Qnil;
2289 }
2290
2291 if (RB_TYPE_P(a1, T_COMPLEX)) {
2292 {
2293 get_dat1(a1);
2294
2295 if (k_exact_zero_p(dat->imag))
2296 a1 = dat->real;
2297 }
2298 }
2299
2300 if (RB_TYPE_P(a2, T_COMPLEX)) {
2301 {
2302 get_dat1(a2);
2303
2304 if (k_exact_zero_p(dat->imag))
2305 a2 = dat->real;
2306 }
2307 }
2308
2309 if (RB_TYPE_P(a1, T_COMPLEX)) {
2310 if (UNDEF_P(a2) || (k_exact_zero_p(a2)))
2311 return a1;
2312 }
2313
2314 if (UNDEF_P(a2)) {
2315 if (k_numeric_p(a1) && !f_real_p(a1))
2316 return a1;
2317 /* should raise exception for consistency */
2318 if (!k_numeric_p(a1)) {
2319 if (!raise) {
2320 a1 = rb_protect(to_complex, a1, NULL);
2321 rb_set_errinfo(Qnil);
2322 return a1;
2323 }
2324 return to_complex(a1);
2325 }
2326 }
2327 else {
2328 if ((k_numeric_p(a1) && k_numeric_p(a2)) &&
2329 (!f_real_p(a1) || !f_real_p(a2)))
2330 return f_add(a1,
2331 f_mul(a2,
2332 f_complex_new_bang2(rb_cComplex, ZERO, ONE)));
2333 }
2334
2335 {
2336 int argc;
2337 VALUE argv2[2];
2338 argv2[0] = a1;
2339 if (UNDEF_P(a2)) {
2340 argv2[1] = Qnil;
2341 argc = 1;
2342 }
2343 else {
2344 if (!raise && !RB_INTEGER_TYPE_P(a2) && !RB_FLOAT_TYPE_P(a2) && !RB_TYPE_P(a2, T_RATIONAL))
2345 return Qnil;
2346 argv2[1] = a2;
2347 argc = 2;
2348 }
2349 return nucomp_s_new(argc, argv2, klass);
2350 }
2351}
2352
2353static VALUE
2354nucomp_s_convert(int argc, VALUE *argv, VALUE klass)
2355{
2356 VALUE a1, a2;
2357
2358 if (rb_scan_args(argc, argv, "11", &a1, &a2) == 1) {
2359 a2 = Qundef;
2360 }
2361
2362 return nucomp_convert(klass, a1, a2, TRUE);
2363}
2364
2365/*
2366 * call-seq:
2367 * abs2 -> real
2368 *
2369 * Returns the square of +self+.
2370 */
2371static VALUE
2372numeric_abs2(VALUE self)
2373{
2374 return f_mul(self, self);
2375}
2376
2377/*
2378 * call-seq:
2379 * arg -> 0 or Math::PI
2380 *
2381 * Returns zero if +self+ is positive, Math::PI otherwise.
2382 */
2383static VALUE
2384numeric_arg(VALUE self)
2385{
2386 if (f_positive_p(self))
2387 return INT2FIX(0);
2388 return DBL2NUM(M_PI);
2389}
2390
2391/*
2392 * call-seq:
2393 * rect -> array
2394 *
2395 * Returns array <tt>[self, 0]</tt>.
2396 */
2397static VALUE
2398numeric_rect(VALUE self)
2399{
2400 return rb_assoc_new(self, INT2FIX(0));
2401}
2402
2403/*
2404 * call-seq:
2405 * polar -> array
2406 *
2407 * Returns array <tt>[self.abs, self.arg]</tt>.
2408 */
2409static VALUE
2410numeric_polar(VALUE self)
2411{
2412 VALUE abs, arg;
2413
2414 if (RB_INTEGER_TYPE_P(self)) {
2415 abs = rb_int_abs(self);
2416 arg = numeric_arg(self);
2417 }
2418 else if (RB_FLOAT_TYPE_P(self)) {
2419 abs = rb_float_abs(self);
2420 arg = float_arg(self);
2421 }
2422 else if (RB_TYPE_P(self, T_RATIONAL)) {
2423 abs = rb_rational_abs(self);
2424 arg = numeric_arg(self);
2425 }
2426 else {
2427 abs = f_abs(self);
2428 arg = f_arg(self);
2429 }
2430 return rb_assoc_new(abs, arg);
2431}
2432
2433/*
2434 * call-seq:
2435 * arg -> 0 or Math::PI
2436 *
2437 * Returns 0 if +self+ is positive, Math::PI otherwise.
2438 */
2439static VALUE
2440float_arg(VALUE self)
2441{
2442 if (isnan(RFLOAT_VALUE(self)))
2443 return self;
2444 if (f_tpositive_p(self))
2445 return INT2FIX(0);
2446 return rb_const_get(rb_mMath, id_PI);
2447}
2448
2449/*
2450 * A \Complex object houses a pair of values,
2451 * given when the object is created as either <i>rectangular coordinates</i>
2452 * or <i>polar coordinates</i>.
2453 *
2454 * == Rectangular Coordinates
2455 *
2456 * The rectangular coordinates of a complex number
2457 * are called the _real_ and _imaginary_ parts;
2458 * see {Complex number definition}[https://en.wikipedia.org/wiki/Complex_number#Definition_and_basic_operations].
2459 *
2460 * You can create a \Complex object from rectangular coordinates with:
2461 *
2462 * - A {complex literal}[rdoc-ref:syntax/literals.rdoc@Complex+Literals].
2463 * - Method Complex.rect.
2464 * - Method Kernel#Complex, either with numeric arguments or with certain string arguments.
2465 * - Method String#to_c, for certain strings.
2466 *
2467 * Note that each of the stored parts may be a an instance one of the classes
2468 * Complex, Float, Integer, or Rational;
2469 * they may be retrieved:
2470 *
2471 * - Separately, with methods Complex#real and Complex#imaginary.
2472 * - Together, with method Complex#rect.
2473 *
2474 * The corresponding (computed) polar values may be retrieved:
2475 *
2476 * - Separately, with methods Complex#abs and Complex#arg.
2477 * - Together, with method Complex#polar.
2478 *
2479 * == Polar Coordinates
2480 *
2481 * The polar coordinates of a complex number
2482 * are called the _absolute_ and _argument_ parts;
2483 * see {Complex polar plane}[https://en.wikipedia.org/wiki/Complex_number#Polar_form].
2484 *
2485 * In this class, the argument part
2486 * in expressed {radians}[https://en.wikipedia.org/wiki/Radian]
2487 * (not {degrees}[https://en.wikipedia.org/wiki/Degree_(angle)]).
2488 *
2489 * You can create a \Complex object from polar coordinates with:
2490 *
2491 * - Method Complex.polar.
2492 * - Method Kernel#Complex, with certain string arguments.
2493 * - Method String#to_c, for certain strings.
2494 *
2495 * Note that each of the stored parts may be a an instance one of the classes
2496 * Complex, Float, Integer, or Rational;
2497 * they may be retrieved:
2498 *
2499 * - Separately, with methods Complex#abs and Complex#arg.
2500 * - Together, with method Complex#polar.
2501 *
2502 * The corresponding (computed) rectangular values may be retrieved:
2503 *
2504 * - Separately, with methods Complex#real and Complex#imag.
2505 * - Together, with method Complex#rect.
2506 *
2507 * == What's Here
2508 *
2509 * First, what's elsewhere:
2510 *
2511 * - Class \Complex inherits (directly or indirectly)
2512 * from classes {Numeric}[rdoc-ref:Numeric@What-27s+Here]
2513 * and {Object}[rdoc-ref:Object@What-27s+Here].
2514 * - Includes (indirectly) module {Comparable}[rdoc-ref:Comparable@What-27s+Here].
2515 *
2516 * Here, class \Complex has methods for:
2517 *
2518 * === Creating \Complex Objects
2519 *
2520 * - ::polar: Returns a new \Complex object based on given polar coordinates.
2521 * - ::rect (and its alias ::rectangular):
2522 * Returns a new \Complex object based on given rectangular coordinates.
2523 *
2524 * === Querying
2525 *
2526 * - #abs (and its alias #magnitude): Returns the absolute value for +self+.
2527 * - #arg (and its aliases #angle and #phase):
2528 * Returns the argument (angle) for +self+ in radians.
2529 * - #denominator: Returns the denominator of +self+.
2530 * - #finite?: Returns whether both +self.real+ and +self.image+ are finite.
2531 * - #hash: Returns the integer hash value for +self+.
2532 * - #imag (and its alias #imaginary): Returns the imaginary value for +self+.
2533 * - #infinite?: Returns whether +self.real+ or +self.image+ is infinite.
2534 * - #numerator: Returns the numerator of +self+.
2535 * - #polar: Returns the array <tt>[self.abs, self.arg]</tt>.
2536 * - #inspect: Returns a string representation of +self+.
2537 * - #real: Returns the real value for +self+.
2538 * - #real?: Returns +false+; for compatibility with Numeric#real?.
2539 * - #rect (and its alias #rectangular):
2540 * Returns the array <tt>[self.real, self.imag]</tt>.
2541 *
2542 * === Comparing
2543 *
2544 * - #<=>: Returns whether +self+ is less than, equal to, or greater than the given argument.
2545 * - #==: Returns whether +self+ is equal to the given argument.
2546 *
2547 * === Converting
2548 *
2549 * - #rationalize: Returns a Rational object whose value is exactly
2550 * or approximately equivalent to that of <tt>self.real</tt>.
2551 * - #to_c: Returns +self+.
2552 * - #to_d: Returns the value as a BigDecimal object.
2553 * - #to_f: Returns the value of <tt>self.real</tt> as a Float, if possible.
2554 * - #to_i: Returns the value of <tt>self.real</tt> as an Integer, if possible.
2555 * - #to_r: Returns the value of <tt>self.real</tt> as a Rational, if possible.
2556 * - #to_s: Returns a string representation of +self+.
2557 *
2558 * === Performing Complex Arithmetic
2559 *
2560 * - #*: Returns the product of +self+ and the given numeric.
2561 * - #**: Returns +self+ raised to power of the given numeric.
2562 * - #+: Returns the sum of +self+ and the given numeric.
2563 * - #-: Returns the difference of +self+ and the given numeric.
2564 * - #-@: Returns the negation of +self+.
2565 * - #/: Returns the quotient of +self+ and the given numeric.
2566 * - #abs2: Returns square of the absolute value (magnitude) for +self+.
2567 * - #conj (and its alias #conjugate): Returns the conjugate of +self+.
2568 * - #fdiv: Returns <tt>Complex.rect(self.real/numeric, self.imag/numeric)</tt>.
2569 *
2570 * === Working with JSON
2571 *
2572 * - ::json_create: Returns a new \Complex object,
2573 * deserialized from the given serialized hash.
2574 * - #as_json: Returns a serialized hash constructed from +self+.
2575 * - #to_json: Returns a JSON string representing +self+.
2576 *
2577 * These methods are provided by the {JSON gem}[https://github.com/ruby/json]. To make these methods available:
2578 *
2579 * require 'json/add/complex'
2580 *
2581 */
2582void
2583Init_Complex(void)
2584{
2585 VALUE compat;
2586 id_abs = rb_intern_const("abs");
2587 id_arg = rb_intern_const("arg");
2588 id_denominator = rb_intern_const("denominator");
2589 id_numerator = rb_intern_const("numerator");
2590 id_real_p = rb_intern_const("real?");
2591 id_i_real = rb_intern_const("@real");
2592 id_i_imag = rb_intern_const("@image"); /* @image, not @imag */
2593 id_finite_p = rb_intern_const("finite?");
2594 id_infinite_p = rb_intern_const("infinite?");
2595 id_rationalize = rb_intern_const("rationalize");
2596 id_PI = rb_intern_const("PI");
2597
2599
2600 rb_define_alloc_func(rb_cComplex, nucomp_s_alloc);
2601 rb_undef_method(CLASS_OF(rb_cComplex), "allocate");
2602
2604
2605 rb_define_singleton_method(rb_cComplex, "rectangular", nucomp_s_new, -1);
2606 rb_define_singleton_method(rb_cComplex, "rect", nucomp_s_new, -1);
2607 rb_define_singleton_method(rb_cComplex, "polar", nucomp_s_polar, -1);
2608
2609 rb_define_global_function("Complex", nucomp_f_complex, -1);
2610
2611 rb_undef_methods_from(rb_cComplex, RCLASS_ORIGIN(rb_mComparable));
2614 rb_undef_method(rb_cComplex, "divmod");
2615 rb_undef_method(rb_cComplex, "floor");
2617 rb_undef_method(rb_cComplex, "modulo");
2618 rb_undef_method(rb_cComplex, "remainder");
2619 rb_undef_method(rb_cComplex, "round");
2621 rb_undef_method(rb_cComplex, "truncate");
2623
2624 rb_define_method(rb_cComplex, "real", rb_complex_real, 0);
2625 rb_define_method(rb_cComplex, "imaginary", rb_complex_imag, 0);
2626 rb_define_method(rb_cComplex, "imag", rb_complex_imag, 0);
2627
2628 rb_define_method(rb_cComplex, "-@", rb_complex_uminus, 0);
2629 rb_define_method(rb_cComplex, "+", rb_complex_plus, 1);
2630 rb_define_method(rb_cComplex, "-", rb_complex_minus, 1);
2631 rb_define_method(rb_cComplex, "*", rb_complex_mul, 1);
2632 rb_define_method(rb_cComplex, "/", rb_complex_div, 1);
2633 rb_define_method(rb_cComplex, "quo", nucomp_quo, 1);
2634 rb_define_method(rb_cComplex, "fdiv", nucomp_fdiv, 1);
2635 rb_define_method(rb_cComplex, "**", rb_complex_pow, 1);
2636
2637 rb_define_method(rb_cComplex, "==", nucomp_eqeq_p, 1);
2638 rb_define_method(rb_cComplex, "<=>", nucomp_cmp, 1);
2639 rb_define_method(rb_cComplex, "coerce", nucomp_coerce, 1);
2640
2641 rb_define_method(rb_cComplex, "abs", rb_complex_abs, 0);
2642 rb_define_method(rb_cComplex, "magnitude", rb_complex_abs, 0);
2643 rb_define_method(rb_cComplex, "abs2", nucomp_abs2, 0);
2644 rb_define_method(rb_cComplex, "arg", rb_complex_arg, 0);
2645 rb_define_method(rb_cComplex, "angle", rb_complex_arg, 0);
2646 rb_define_method(rb_cComplex, "phase", rb_complex_arg, 0);
2647 rb_define_method(rb_cComplex, "rectangular", nucomp_rect, 0);
2648 rb_define_method(rb_cComplex, "rect", nucomp_rect, 0);
2649 rb_define_method(rb_cComplex, "polar", nucomp_polar, 0);
2650 rb_define_method(rb_cComplex, "conjugate", rb_complex_conjugate, 0);
2651 rb_define_method(rb_cComplex, "conj", rb_complex_conjugate, 0);
2652
2653 rb_define_method(rb_cComplex, "real?", nucomp_real_p_m, 0);
2654
2655 rb_define_method(rb_cComplex, "numerator", nucomp_numerator, 0);
2656 rb_define_method(rb_cComplex, "denominator", nucomp_denominator, 0);
2657
2658 rb_define_method(rb_cComplex, "hash", nucomp_hash, 0);
2659 rb_define_method(rb_cComplex, "eql?", nucomp_eql_p, 1);
2660
2661 rb_define_method(rb_cComplex, "to_s", nucomp_to_s, 0);
2662 rb_define_method(rb_cComplex, "inspect", nucomp_inspect, 0);
2663
2664 rb_undef_method(rb_cComplex, "positive?");
2665 rb_undef_method(rb_cComplex, "negative?");
2666
2667 rb_define_method(rb_cComplex, "finite?", rb_complex_finite_p, 0);
2668 rb_define_method(rb_cComplex, "infinite?", rb_complex_infinite_p, 0);
2669
2670 rb_define_private_method(rb_cComplex, "marshal_dump", nucomp_marshal_dump, 0);
2671 /* :nodoc: */
2672 compat = rb_define_class_under(rb_cComplex, "compatible", rb_cObject);
2673 rb_define_private_method(compat, "marshal_load", nucomp_marshal_load, 1);
2674 rb_marshal_define_compat(rb_cComplex, compat, nucomp_dumper, nucomp_loader);
2675
2676 rb_define_method(rb_cComplex, "to_i", nucomp_to_i, 0);
2677 rb_define_method(rb_cComplex, "to_f", nucomp_to_f, 0);
2678 rb_define_method(rb_cComplex, "to_r", nucomp_to_r, 0);
2679 rb_define_method(rb_cComplex, "rationalize", nucomp_rationalize, -1);
2680 rb_define_method(rb_cComplex, "to_c", nucomp_to_c, 0);
2681 rb_define_method(rb_cNumeric, "to_c", numeric_to_c, 0);
2682
2683 rb_define_method(rb_cString, "to_c", string_to_c, 0);
2684
2685 rb_define_private_method(CLASS_OF(rb_cComplex), "convert", nucomp_s_convert, -1);
2686
2687 rb_define_method(rb_cNumeric, "abs2", numeric_abs2, 0);
2688 rb_define_method(rb_cNumeric, "arg", numeric_arg, 0);
2689 rb_define_method(rb_cNumeric, "angle", numeric_arg, 0);
2690 rb_define_method(rb_cNumeric, "phase", numeric_arg, 0);
2691 rb_define_method(rb_cNumeric, "rectangular", numeric_rect, 0);
2692 rb_define_method(rb_cNumeric, "rect", numeric_rect, 0);
2693 rb_define_method(rb_cNumeric, "polar", numeric_polar, 0);
2694
2695 rb_define_method(rb_cFloat, "arg", float_arg, 0);
2696 rb_define_method(rb_cFloat, "angle", float_arg, 0);
2697 rb_define_method(rb_cFloat, "phase", float_arg, 0);
2698
2699 /*
2700 * Equivalent
2701 * to <tt>Complex.rect(0, 1)</tt>:
2702 *
2703 * Complex::I # => (0+1i)
2704 *
2705 */
2706 rb_define_const(rb_cComplex, "I",
2707 f_complex_new_bang2(rb_cComplex, ZERO, ONE));
2708
2709#if !USE_FLONUM
2710 rb_vm_register_global_object(RFLOAT_0 = DBL2NUM(0.0));
2711#endif
2712
2713 rb_provide("complex.so"); /* for backward compatibility */
2714}
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
Definition assert.h:219
static int rb_isdigit(int c)
Our own locale-insensitive version of isdigit(3).
Definition ctype.h:302
#define rb_define_method(klass, mid, func, arity)
Defines klass#mid.
#define rb_define_singleton_method(klass, mid, func, arity)
Defines klass.mid.
#define rb_define_private_method(klass, mid, func, arity)
Defines klass#mid and makes it private.
#define rb_define_global_function(mid, func, arity)
Defines rb_mKernel #mid.
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
Definition class.c:1484
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
Definition class.c:1520
void rb_undef_method(VALUE klass, const char *name)
Defines an undef of a method.
Definition class.c:2668
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Retrieves argument from argc and argv to given VALUE references according to the format string.
Definition class.c:3138
#define T_COMPLEX
Old name of RUBY_T_COMPLEX.
Definition value_type.h:59
#define RB_INTEGER_TYPE_P
Old name of rb_integer_type_p.
Definition value_type.h:87
#define RFLOAT_VALUE
Old name of rb_float_value.
Definition double.h:28
#define T_STRING
Old name of RUBY_T_STRING.
Definition value_type.h:78
#define Qundef
Old name of RUBY_Qundef.
#define INT2FIX
Old name of RB_INT2FIX.
Definition long.h:48
#define rb_str_cat2
Old name of rb_str_cat_cstr.
Definition string.h:1683
#define OBJ_FREEZE
Old name of RB_OBJ_FREEZE.
Definition fl_type.h:134
#define CLASS_OF
Old name of rb_class_of.
Definition globals.h:205
#define LONG2FIX
Old name of RB_INT2FIX.
Definition long.h:49
#define FIX2INT
Old name of RB_FIX2INT.
Definition int.h:41
#define T_RATIONAL
Old name of RUBY_T_RATIONAL.
Definition value_type.h:76
#define NUM2DBL
Old name of rb_num2dbl.
Definition double.h:27
VALUE rb_complex_polar(VALUE x, VALUE y)
Old name of rb_complex_new_polar.
Definition complex.c:1768
#define rb_usascii_str_new2
Old name of rb_usascii_str_new_cstr.
Definition string.h:1680
#define FLONUM_P
Old name of RB_FLONUM_P.
#define ST2FIX
Old name of RB_ST2FIX.
Definition st_data_t.h:33
#define Qnil
Old name of RUBY_Qnil.
#define Qfalse
Old name of RUBY_Qfalse.
#define FIX2LONG
Old name of RB_FIX2LONG.
Definition long.h:46
#define T_ARRAY
Old name of RUBY_T_ARRAY.
Definition value_type.h:56
#define NIL_P
Old name of RB_NIL_P.
#define ALLOCV_N
Old name of RB_ALLOCV_N.
Definition memory.h:405
#define FL_WB_PROTECTED
Old name of RUBY_FL_WB_PROTECTED.
Definition fl_type.h:59
#define DBL2NUM
Old name of rb_float_new.
Definition double.h:29
#define NUM2LONG
Old name of RB_NUM2LONG.
Definition long.h:51
#define FIXNUM_P
Old name of RB_FIXNUM_P.
#define ALLOCV_END
Old name of RB_ALLOCV_END.
Definition memory.h:406
VALUE rb_eRangeError
RangeError exception.
Definition error.c:1434
VALUE rb_eTypeError
TypeError exception.
Definition error.c:1430
void rb_warn(const char *fmt,...)
Identical to rb_warning(), except it reports unless $VERBOSE is nil.
Definition error.c:466
VALUE rb_cRational
Rational class.
Definition rational.c:53
VALUE rb_convert_type(VALUE val, int type, const char *name, const char *mid)
Converts an object into another type.
Definition object.c:3123
VALUE rb_cComplex
Complex class.
Definition complex.c:39
VALUE rb_mMath
Math module.
Definition math.c:29
VALUE rb_cInteger
Module class.
Definition numeric.c:198
double rb_str_to_dbl(VALUE str, int mode)
Identical to rb_cstr_to_dbl(), except it accepts a Ruby's string instead of C's.
Definition object.c:3539
VALUE rb_cNumeric
Numeric class.
Definition numeric.c:196
VALUE rb_obj_class(VALUE obj)
Queries the class of an object.
Definition object.c:243
VALUE rb_inspect(VALUE obj)
Generates a human-readable textual representation of the given object.
Definition object.c:657
VALUE rb_equal(VALUE lhs, VALUE rhs)
This function is an optimised version of calling #==.
Definition object.c:175
VALUE rb_obj_is_kind_of(VALUE obj, VALUE klass)
Queries if the given object is an instance (of possibly descendants) of the given class.
Definition object.c:880
double rb_cstr_to_dbl(const char *str, int mode)
Converts a textual representation of a real number into a numeric, which is the nearest value that th...
Definition object.c:3495
VALUE rb_mComparable
Comparable module.
Definition compar.c:19
VALUE rb_cFloat
Float class.
Definition numeric.c:197
VALUE rb_String(VALUE val)
This is the logic behind Kernel#String.
Definition object.c:3778
VALUE rb_cString
String class.
Definition string.c:83
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
Definition vm_eval.c:1117
#define RGENGC_WB_PROTECTED_COMPLEX
This is a compile-time flag to enable/disable write barrier for struct RComplex.
Definition gc.h:545
VALUE rb_assoc_new(VALUE car, VALUE cdr)
Identical to rb_ary_new_from_values(), except it expects exactly two parameters.
#define rb_complex_new2(x, y)
Just another name of rb_complex_new.
Definition complex.h:77
#define rb_complex_new1(x)
Shorthand of x+0i.
Definition complex.h:74
static int rb_check_arity(int argc, int min, int max)
Ensures that the passed integer is in the passed range.
Definition error.h:284
void rb_provide(const char *feature)
Declares that the given feature is already provided by someone else.
Definition load.c:765
VALUE rb_num_coerce_cmp(VALUE lhs, VALUE rhs, ID op)
Identical to rb_num_coerce_bin(), except for return values.
Definition numeric.c:484
VALUE rb_num_coerce_bin(VALUE lhs, VALUE rhs, ID op)
Coerced binary operation.
Definition numeric.c:477
VALUE rb_rational_new(VALUE num, VALUE den)
Constructs a Rational, with reduction.
Definition rational.c:1974
st_index_t rb_memhash(const void *ptr, long len)
This is a universal hash function.
Definition random.c:1768
void rb_must_asciicompat(VALUE obj)
Asserts that the given string's encoding is (Ruby's definition of) ASCII compatible.
Definition string.c:2748
VALUE rb_str_concat(VALUE dst, VALUE src)
Identical to rb_str_append(), except it also accepts an integer as a codepoint.
Definition string.c:4004
VALUE rb_const_get(VALUE space, ID name)
Identical to rb_const_defined(), except it returns the actual defined value.
Definition variable.c:3575
VALUE rb_ivar_set(VALUE obj, ID name, VALUE val)
Identical to rb_iv_set(), except it accepts the name as an ID instead of a C string.
Definition variable.c:2079
VALUE rb_ivar_get(VALUE obj, ID name)
Identical to rb_iv_get(), except it accepts the name as an ID instead of a C string.
Definition variable.c:1443
void rb_define_alloc_func(VALUE klass, rb_alloc_func_t func)
Sets the allocator function of a class.
static ID rb_intern_const(const char *str)
This is a "tiny optimisation" over rb_intern().
Definition symbol.h:284
void rb_marshal_define_compat(VALUE newclass, VALUE oldclass, VALUE(*dumper)(VALUE), VALUE(*loader)(VALUE, VALUE))
Marshal format compatibility layer.
Definition marshal.c:137
void rb_copy_generic_ivar(VALUE clone, VALUE obj)
Copies the list of instance variables.
Definition variable.c:2276
#define RARRAY_LEN
Just another name of rb_array_len.
Definition rarray.h:51
#define RARRAY_AREF(a, i)
Definition rarray.h:403
#define StringValueCStr(v)
Identical to StringValuePtr, except it additionally checks for the contents for viability as a C stri...
Definition rstring.h:89
#define RTEST
This is an old name of RB_TEST.
Internal header for Complex.
Definition complex.h:13
intptr_t SIGNED_VALUE
A signed integer type that has the same width with VALUE.
Definition value.h:63
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
Definition value.h:52
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40
static bool RB_FLOAT_TYPE_P(VALUE obj)
Queries if the object is an instance of rb_cFloat.
Definition value_type.h:264
static void Check_Type(VALUE v, enum ruby_value_type t)
Identical to RB_TYPE_P(), except it raises exceptions on predication failure.
Definition value_type.h:433
static bool rb_integer_type_p(VALUE obj)
Queries if the object is an instance of rb_cInteger.
Definition value_type.h:204
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.
Definition value_type.h:376