ext/mpc/ruby_mpc.c in ruby-mpc-0.0.2 vs ext/mpc/ruby_mpc.c in ruby-mpc-0.0.3
- old
+ new
@@ -118,10 +118,11 @@
void r_mpc_free(void *ptr){
mpc_clear(ptr);
free(ptr);
}
+/* Allocation function. */
static VALUE r_mpc_alloc(VALUE self)
{
MPC *ptr;
r_mpc_make_struct(self, ptr);
return self;
@@ -167,20 +168,12 @@
break;
}
}
}
-/* Arguments may be the following types by number. */
-/* 0. none */
-/* 1. MPC or array having two elements */
-/* 2. real part and imaginary part*/
-/* 3. real part, imaginary part, and rounding mode */
-/* 4. real part, imaginary part, rounding mode and precision */
-static VALUE r_mpc_initialize(int argc, VALUE *argv, VALUE self)
+static void r_mpc_set_initial_value(MPC *ptr, int argc, VALUE *argv)
{
- MPC *ptr;
- r_mpc_get_struct(ptr, self);
switch(argc){
case 0:
mpc_init2(ptr, mpfr_get_default_prec());
break;
case 1:
@@ -200,14 +193,41 @@
r_mpc_set_from_two_objects(ptr, argv[0], argv[1], r_mpc_rnd_from_value(argv[2]));
break;
default:
rb_raise(rb_eArgError, "Invalid number of arguments.");
break;
- }
+ }
+}
+
+/* Return new MPC instance. The same arguments as MPC.new is acceptable. */
+static VALUE r_mpc_global_new(int argc, VALUE *argv, VALUE self)
+{
+ MPC *ptr;
+ VALUE val;
+ r_mpc_make_struct(val, ptr);
+ r_mpc_set_initial_value(ptr, argc, argv);
+ return val;
+}
+
+
+/*
+ Arguments may be the following types by number.
+ 0. none
+ 1. MPC or array having two elements
+ 2. real part and imaginary part
+ 3. real part, imaginary part, and rounding mode
+ 4. real part, imaginary part, rounding mode and precision
+*/
+static VALUE r_mpc_initialize(int argc, VALUE *argv, VALUE self)
+{
+ MPC *ptr;
+ r_mpc_get_struct(ptr, self);
+ r_mpc_set_initial_value(ptr, argc, argv);
return Qtrue;
}
+/* initialize_copy */
static VALUE r_mpc_initialize_copy(VALUE self, VALUE other)
{
MPC *ptr_self, *ptr_other;
r_mpc_get_struct(ptr_self, self);
r_mpc_get_struct(ptr_other, other);
@@ -227,10 +247,11 @@
return rb_ary_new3(2, val_other, self);
}
/* String */
+/* inspect */
static VALUE r_mpc_inspect(VALUE self)
{
MPC *ptr_s;
r_mpc_get_struct(ptr_s, self);
char *ret_str;
@@ -298,10 +319,12 @@
r_mpc_set_c_function_state(mpc_proj(ptr_ret, ptr_self, rnd));
return ret_val;
}
/* Basic Arithmetic Functions */
+
+/* Return self + p1. */
static VALUE r_mpc_add(VALUE self, VALUE other)
{
MPC *ptr_self, *ptr_return;
VALUE val_ret;
r_mpc_get_struct(ptr_self, self);
@@ -320,10 +343,11 @@
mpc_add_fr(ptr_return, ptr_self, ptr_other, rb_mpc_extended_get_default_rounding_mode());
}
return val_ret;
}
+/* Return self / p1. */
static VALUE r_mpc_sub(VALUE self, VALUE other)
{
MPC *ptr_self, *ptr_return;
VALUE val_ret;
r_mpc_get_struct(ptr_self, self);
@@ -343,10 +367,11 @@
mpc_sub_fr(ptr_return, ptr_self, ptr_other, rb_mpc_extended_get_default_rounding_mode());
}
return val_ret;
}
+/* Return self * p1. */
static VALUE r_mpc_mul(VALUE self, VALUE other)
{
MPC *ptr_self, *ptr_return;
VALUE val_ret;
r_mpc_get_struct(ptr_self, self);
@@ -366,11 +391,11 @@
mpc_mul_fr(ptr_return, ptr_self, ptr_other, rb_mpc_extended_get_default_rounding_mode());
}
return val_ret;
}
-
+/* Return self / p1. */
static VALUE r_mpc_div(VALUE self, VALUE other)
{
MPC *ptr_self, *ptr_return;
VALUE val_ret;
r_mpc_get_struct(ptr_self, self);
@@ -390,10 +415,11 @@
mpc_div_fr(ptr_return, ptr_self, ptr_other, rb_mpc_extended_get_default_rounding_mode());
}
return val_ret;
}
+/* Multiply self by _i_. */
static VALUE r_mpc_mul_i (int argc, VALUE *argv, VALUE self)
{
mpc_rnd_t rnd;
mp_prec_t prec;
r_mpc_get_rnd_prec_from_optional_arguments(&rnd, &prec, 1, 3, argc, argv);
@@ -403,10 +429,11 @@
r_mpc_make_struct_init2(val_ret, ptr_return, prec);
r_mpc_set_c_function_state(mpc_mul_i(ptr_return, ptr_self, NUM2INT(argv[0]), rnd));
return val_ret;
}
+/* Multiply self by -1. */
static VALUE r_mpc_neg (int argc, VALUE *argv, VALUE self)
{
mpc_rnd_t rnd;
mp_prec_t prec;
r_mpc_get_rnd_prec_from_optional_arguments(&rnd, &prec, 0, 2, argc, argv);
@@ -416,10 +443,11 @@
r_mpc_make_struct_init2(val_ret, ptr_return, prec);
r_mpc_set_c_function_state(mpc_neg(ptr_return, ptr_self, rnd));
return val_ret;
}
+/* Return conjugate of self. */
static VALUE r_mpc_conj (int argc, VALUE *argv, VALUE self)
{
mpc_rnd_t rnd;
mp_prec_t prec;
r_mpc_get_rnd_prec_from_optional_arguments(&rnd, &prec, 0, 2, argc, argv);
@@ -429,10 +457,11 @@
r_mpc_make_struct_init2(val_ret, ptr_return, prec);
r_mpc_set_c_function_state(mpc_conj(ptr_return, ptr_self, rnd));
return val_ret;
}
+/* Return absolute value. */
static VALUE r_mpc_abs (int argc, VALUE *argv, VALUE self)
{
mpc_rnd_t rnd;
mp_prec_t prec;
r_mpc_get_rnd_prec_from_optional_arguments(&rnd, &prec, 0, 2, argc, argv);
@@ -443,10 +472,11 @@
r_mpfr_make_struct_init2(val_ret, ptr_return, prec);
r_mpc_set_fr_function_state(mpc_abs(ptr_return, ptr_self, rnd));
return val_ret;
}
+/* Return the norm (the square of absolute value). */
static VALUE r_mpc_norm (int argc, VALUE *argv, VALUE self)
{
mpc_rnd_t rnd;
mp_prec_t prec;
r_mpc_get_rnd_prec_from_optional_arguments(&rnd, &prec, 0, 2, argc, argv);
@@ -458,10 +488,12 @@
r_mpc_set_fr_function_state(mpc_norm(ptr_return, ptr_self, rnd));
return val_ret;
}
/* module function */
+
+/* Return self + p1. */
static VALUE r_mpc_math_add(int argc, VALUE *argv, VALUE self)
{
mp_rnd_t rnd;
mp_prec_t prec;
r_mpc_get_rnd_prec_from_optional_arguments(&rnd, &prec, 2, 4, argc, argv);
@@ -487,10 +519,11 @@
mpc_add(ptr_return, ptr_arg1, ptr_arg2, rnd);
}
return val_ret;
}
+/* Return self - p1. */
static VALUE r_mpc_math_sub(int argc, VALUE *argv, VALUE self)
{
mp_rnd_t rnd;
mp_prec_t prec;
r_mpc_get_rnd_prec_from_optional_arguments(&rnd, &prec, 2, 4, argc, argv);
@@ -521,10 +554,11 @@
mpc_sub(ptr_return, ptr_arg1, ptr_arg2, rnd);
}
return val_ret;
}
+/* Return self * p1. */
static VALUE r_mpc_math_mul(int argc, VALUE *argv, VALUE self)
{
mp_rnd_t rnd;
mp_prec_t prec;
r_mpc_get_rnd_prec_from_optional_arguments(&rnd, &prec, 2, 4, argc, argv);
@@ -550,10 +584,11 @@
mpc_mul(ptr_return, ptr_arg1, ptr_arg2, rnd);
}
return val_ret;
}
+/* Return self / p1. */
static VALUE r_mpc_math_div(int argc, VALUE *argv, VALUE self)
{
mp_rnd_t rnd;
mp_prec_t prec;
r_mpc_get_rnd_prec_from_optional_arguments(&rnd, &prec, 2, 4, argc, argv);
@@ -577,10 +612,12 @@
mpc_div(ptr_return, ptr_arg1, ptr_arg2, rnd);
}
return val_ret;
}
+
+/* mpc_mul_2exp(ret, p1, p2, rnd) */
static VALUE r_mpc_math_mul_2exp (int argc, VALUE *argv, VALUE self)
{
mpc_rnd_t rnd;
mp_prec_t prec;
r_mpc_get_rnd_prec_from_optional_arguments(&rnd, &prec, 2, 4, argc, argv);
@@ -591,10 +628,11 @@
r_mpc_make_struct_init2(val_ret, ptr_return, prec);
r_mpc_set_c_function_state(mpc_mul_2exp(ptr_return, ptr_arg, NUM2LONG(argv[1]), rnd));
return val_ret;
}
+/* mpc_div_2exp(ret, p1, p2, rnd) */
static VALUE r_mpc_math_div_2exp (int argc, VALUE *argv, VALUE self)
{
mpc_rnd_t rnd;
mp_prec_t prec;
r_mpc_get_rnd_prec_from_optional_arguments(&rnd, &prec, 2, 4, argc, argv);
@@ -606,10 +644,11 @@
r_mpc_set_c_function_state(mpc_div_2exp(ptr_return, ptr_arg, NUM2LONG(argv[1]), rnd));
return val_ret;
}
+/* mpc_sqr(ret, p1, rnd) */
static VALUE r_mpc_math_sqr (int argc, VALUE *argv, VALUE self)
{
mpc_rnd_t rnd;
mp_prec_t prec;
r_mpc_get_rnd_prec_from_optional_arguments(&rnd, &prec, 1, 3, argc, argv);
@@ -620,10 +659,11 @@
r_mpc_make_struct_init2(val_ret, ptr_return, prec);
r_mpc_set_c_function_state(mpc_sqr(ptr_return, ptr_arg, rnd));
return val_ret;
}
+/* mpc_sqrt(ret, p1, rnd) */
static VALUE r_mpc_math_sqrt (int argc, VALUE *argv, VALUE self)
{
mpc_rnd_t rnd;
mp_prec_t prec;
r_mpc_get_rnd_prec_from_optional_arguments(&rnd, &prec, 1, 3, argc, argv);
@@ -634,10 +674,11 @@
r_mpc_make_struct_init2(val_ret, ptr_return, prec);
r_mpc_set_c_function_state(mpc_sqrt(ptr_return, ptr_arg, rnd));
return val_ret;
}
+/* mpc_pow(ret, p1, p2, rnd), mpc_pow_fr(ret, p1, p2, rnd) etc. */
static VALUE r_mpc_math_pow(int argc, VALUE *argv, VALUE self)
{
mp_rnd_t rnd;
mp_prec_t prec;
r_mpc_get_rnd_prec_from_optional_arguments(&rnd, &prec, 2, 4, argc, argv);
@@ -666,11 +707,11 @@
}
return val_ret;
}
-
+/* mpc_exp(ret, p1, rnd) */
static VALUE r_mpc_math_exp (int argc, VALUE *argv, VALUE self)
{
mpc_rnd_t rnd;
mp_prec_t prec;
r_mpc_get_rnd_prec_from_optional_arguments(&rnd, &prec, 1, 3, argc, argv);
@@ -681,10 +722,11 @@
r_mpc_make_struct_init2(val_ret, ptr_return, prec);
r_mpc_set_c_function_state(mpc_exp(ptr_return, ptr_arg, rnd));
return val_ret;
}
+/* mpc_log(ret, p1, rnd) */
static VALUE r_mpc_math_log (int argc, VALUE *argv, VALUE self)
{
mpc_rnd_t rnd;
mp_prec_t prec;
r_mpc_get_rnd_prec_from_optional_arguments(&rnd, &prec, 1, 3, argc, argv);
@@ -695,10 +737,11 @@
r_mpc_make_struct_init2(val_ret, ptr_return, prec);
r_mpc_set_c_function_state(mpc_log(ptr_return, ptr_arg, rnd));
return val_ret;
}
+/* mpc_sin(ret, p1, rnd) */
static VALUE r_mpc_math_sin (int argc, VALUE *argv, VALUE self)
{
mpc_rnd_t rnd;
mp_prec_t prec;
r_mpc_get_rnd_prec_from_optional_arguments(&rnd, &prec, 1, 3, argc, argv);
@@ -709,10 +752,11 @@
r_mpc_make_struct_init2(val_ret, ptr_return, prec);
r_mpc_set_c_function_state(mpc_sin(ptr_return, ptr_arg, rnd));
return val_ret;
}
+/* mpc_cos(ret, p1, rnd) */
static VALUE r_mpc_math_cos (int argc, VALUE *argv, VALUE self)
{
mpc_rnd_t rnd;
mp_prec_t prec;
r_mpc_get_rnd_prec_from_optional_arguments(&rnd, &prec, 1, 3, argc, argv);
@@ -723,10 +767,11 @@
r_mpc_make_struct_init2(val_ret, ptr_return, prec);
r_mpc_set_c_function_state(mpc_cos(ptr_return, ptr_arg, rnd));
return val_ret;
}
+/* mpc_tan(ret, p1, rnd) */
static VALUE r_mpc_math_tan (int argc, VALUE *argv, VALUE self)
{
mpc_rnd_t rnd;
mp_prec_t prec;
r_mpc_get_rnd_prec_from_optional_arguments(&rnd, &prec, 1, 3, argc, argv);
@@ -737,10 +782,11 @@
r_mpc_make_struct_init2(val_ret, ptr_return, prec);
r_mpc_set_c_function_state(mpc_tan(ptr_return, ptr_arg, rnd));
return val_ret;
}
+/* mpc_sinh(ret, p1, rnd) */
static VALUE r_mpc_math_sinh (int argc, VALUE *argv, VALUE self)
{
mpc_rnd_t rnd;
mp_prec_t prec;
r_mpc_get_rnd_prec_from_optional_arguments(&rnd, &prec, 1, 3, argc, argv);
@@ -751,10 +797,11 @@
r_mpc_make_struct_init2(val_ret, ptr_return, prec);
r_mpc_set_c_function_state(mpc_sinh(ptr_return, ptr_arg, rnd));
return val_ret;
}
+/* mpc_cosh(ret, p1, rnd) */
static VALUE r_mpc_math_cosh (int argc, VALUE *argv, VALUE self)
{
mpc_rnd_t rnd;
mp_prec_t prec;
r_mpc_get_rnd_prec_from_optional_arguments(&rnd, &prec, 1, 3, argc, argv);
@@ -765,10 +812,11 @@
r_mpc_make_struct_init2(val_ret, ptr_return, prec);
r_mpc_set_c_function_state(mpc_cosh(ptr_return, ptr_arg, rnd));
return val_ret;
}
+/* mpc_tanh(ret, p1, rnd) */
static VALUE r_mpc_math_tanh (int argc, VALUE *argv, VALUE self)
{
mpc_rnd_t rnd;
mp_prec_t prec;
r_mpc_get_rnd_prec_from_optional_arguments(&rnd, &prec, 1, 3, argc, argv);
@@ -779,10 +827,11 @@
r_mpc_make_struct_init2(val_ret, ptr_return, prec);
r_mpc_set_c_function_state(mpc_tanh(ptr_return, ptr_arg, rnd));
return val_ret;
}
+/* mpc_asin(ret, p1, rnd) */
static VALUE r_mpc_math_asin (int argc, VALUE *argv, VALUE self)
{
mpc_rnd_t rnd;
mp_prec_t prec;
r_mpc_get_rnd_prec_from_optional_arguments(&rnd, &prec, 1, 3, argc, argv);
@@ -793,10 +842,11 @@
r_mpc_make_struct_init2(val_ret, ptr_return, prec);
r_mpc_set_c_function_state(mpc_asin(ptr_return, ptr_arg, rnd));
return val_ret;
}
+/* mpc_acos(ret, p1, rnd) */
static VALUE r_mpc_math_acos (int argc, VALUE *argv, VALUE self)
{
mpc_rnd_t rnd;
mp_prec_t prec;
r_mpc_get_rnd_prec_from_optional_arguments(&rnd, &prec, 1, 3, argc, argv);
@@ -807,10 +857,11 @@
r_mpc_make_struct_init2(val_ret, ptr_return, prec);
r_mpc_set_c_function_state(mpc_acos(ptr_return, ptr_arg, rnd));
return val_ret;
}
+/* mpc_atan(ret, p1, rnd) */
static VALUE r_mpc_math_atan (int argc, VALUE *argv, VALUE self)
{
mpc_rnd_t rnd;
mp_prec_t prec;
r_mpc_get_rnd_prec_from_optional_arguments(&rnd, &prec, 1, 3, argc, argv);
@@ -821,10 +872,11 @@
r_mpc_make_struct_init2(val_ret, ptr_return, prec);
r_mpc_set_c_function_state(mpc_atan(ptr_return, ptr_arg, rnd));
return val_ret;
}
+/* mpc_asinh(ret, p1, rnd) */
static VALUE r_mpc_math_asinh (int argc, VALUE *argv, VALUE self)
{
mpc_rnd_t rnd;
mp_prec_t prec;
r_mpc_get_rnd_prec_from_optional_arguments(&rnd, &prec, 1, 3, argc, argv);
@@ -835,10 +887,11 @@
r_mpc_make_struct_init2(val_ret, ptr_return, prec);
r_mpc_set_c_function_state(mpc_asinh(ptr_return, ptr_arg, rnd));
return val_ret;
}
+/* mpc_acosh(ret, p1, rnd) */
static VALUE r_mpc_math_acosh (int argc, VALUE *argv, VALUE self)
{
mpc_rnd_t rnd;
mp_prec_t prec;
r_mpc_get_rnd_prec_from_optional_arguments(&rnd, &prec, 1, 3, argc, argv);
@@ -849,10 +902,11 @@
r_mpc_make_struct_init2(val_ret, ptr_return, prec);
r_mpc_set_c_function_state(mpc_acosh(ptr_return, ptr_arg, rnd));
return val_ret;
}
+/* mpc_atanh(ret, p1, rnd) */
static VALUE r_mpc_math_atanh (int argc, VALUE *argv, VALUE self)
{
mpc_rnd_t rnd;
mp_prec_t prec;
r_mpc_get_rnd_prec_from_optional_arguments(&rnd, &prec, 1, 3, argc, argv);
@@ -870,39 +924,57 @@
r_mpc_class = rb_define_class("MPC", rb_cNumeric);
rb_define_singleton_method(r_mpc_class, "set_default_rounding_mode", r_mpc_set_default_rounding_mode, 1);
rb_define_singleton_method(r_mpc_class, "get_default_rounding_mode", r_mpc_get_default_rounding_mode, 0);
+ /* Integer which is macro MPC_RNDNN. */
rb_define_const(r_mpc_class, "RNDNN", INT2NUM(MPC_RNDNN));
+ /* Integer which is macro MPC_RNDNZ. */
rb_define_const(r_mpc_class, "RNDNZ", INT2NUM(MPC_RNDNZ));
+ /* Integer which is macro MPC_RNDNU. */
rb_define_const(r_mpc_class, "RNDNU", INT2NUM(MPC_RNDNU));
+ /* Integer which is macro MPC_RNDND. */
rb_define_const(r_mpc_class, "RNDND", INT2NUM(MPC_RNDND));
+ /* Integer which is macro MPC_RNDZN. */
rb_define_const(r_mpc_class, "RNDZN", INT2NUM(MPC_RNDZN));
+ /* Integer which is macro MPC_RNDZZ. */
rb_define_const(r_mpc_class, "RNDZZ", INT2NUM(MPC_RNDZZ));
+ /* Integer which is macro MPC_RNDZU. */
rb_define_const(r_mpc_class, "RNDZU", INT2NUM(MPC_RNDZU));
+ /* Integer which is macro MPC_RNDZD. */
rb_define_const(r_mpc_class, "RNDZD", INT2NUM(MPC_RNDZD));
+ /* Integer which is macro MPC_RNDUN */
rb_define_const(r_mpc_class, "RNDUN", INT2NUM(MPC_RNDUN));
+ /* Integer which is macro MPC_RNDUZ. */
rb_define_const(r_mpc_class, "RNDUZ", INT2NUM(MPC_RNDUZ));
+ /* Integer which is macro MPC_RNDUU. */
rb_define_const(r_mpc_class, "RNDUU", INT2NUM(MPC_RNDUU));
+ /* Integer which is macro MPC_RNDUD. */
rb_define_const(r_mpc_class, "RNDUD", INT2NUM(MPC_RNDUD));
+ /* Integer which is macro MPC_RNDDN. */
rb_define_const(r_mpc_class, "RNDDN", INT2NUM(MPC_RNDDN));
+ /* Integer which is macro MPC_RNDDZ. */
rb_define_const(r_mpc_class, "RNDDZ", INT2NUM(MPC_RNDDZ));
+ /* Integer which is macro MPC_RNDDU. */
rb_define_const(r_mpc_class, "RNDDU", INT2NUM(MPC_RNDDU));
+ /* Integer which is macro MPC_RNDDD. */
rb_define_const(r_mpc_class, "RNDDD", INT2NUM(MPC_RNDDD));
+ /* Integer which is macro MPC_VERSION_MAJOR. */
rb_define_const(r_mpc_class, "VERSION_MAJOR", INT2NUM(MPC_VERSION_MAJOR));
+ /* Integer which is macro MPC_VERSION_MINOR. */
rb_define_const(r_mpc_class, "VERSION_MINOR", INT2NUM(MPC_VERSION_MINOR));
+ /* Integer which is macro MPC_VERSION_PATCHLEVEL. */
rb_define_const(r_mpc_class, "VERSION_PATCHLEVEL", INT2NUM(MPC_VERSION_PATCHLEVEL));
+ /* Stirng which is macro MPC_VERSION_STRING. */
rb_define_const(r_mpc_class, "VERSION_STRING", rb_str_new2(MPC_VERSION_STRING));
-
rb_define_singleton_method(r_mpc_class, "get_function_state", r_mpc_get_function_state, 0);
-
+ rb_define_global_function("MPC", r_mpc_global_new, -1);
rb_define_alloc_func(r_mpc_class, r_mpc_alloc);
-
rb_define_private_method(r_mpc_class, "initialize", r_mpc_initialize, -1);
rb_define_private_method(r_mpc_class, "initialize_copy", r_mpc_initialize_copy, 1);
rb_define_method(r_mpc_class, "coerce", r_mpc_coerce, 1);
@@ -960,9 +1032,8 @@
eqq = rb_intern("===");
new = rb_intern("new");
object_id = rb_intern("object_id");
__mpc_class__ = rb_eval_string("MPC");
__mpfr_class__ = rb_eval_string("MPFR");
-
}