Skip to content

Commit 3432fed

Browse files
committed
Fixes #47
1 parent 46ee731 commit 3432fed

File tree

3 files changed

+22
-16
lines changed

3 files changed

+22
-16
lines changed

ext/symengine/ruby_integer.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
VALUE cinteger_init(VALUE self, VALUE num_value) {
44
basic_struct *this;
55
Data_Get_Struct(self, basic_struct, this);
6-
GET_SYMINTFROMVAL(num_value, this);
6+
get_symintfromval(num_value, this);
77
return self;
88
}
99

ext/symengine/symengine_utils.c

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ void sympify(VALUE operand2, basic_struct *cbasic_operand2) {
1111
switch(TYPE(operand2)) {
1212
case T_FIXNUM:
1313
case T_BIGNUM:
14-
GET_SYMINTFROMVAL(operand2, cbasic_operand2);
14+
get_symintfromval(operand2, cbasic_operand2);
1515
break;
1616

1717
case T_FLOAT:
@@ -27,8 +27,8 @@ void sympify(VALUE operand2, basic_struct *cbasic_operand2) {
2727
basic_new_stack(num_basic);
2828
basic_new_stack(den_basic);
2929

30-
GET_SYMINTFROMVAL(num, num_basic);
31-
GET_SYMINTFROMVAL(den, den_basic);
30+
get_symintfromval(num, num_basic);
31+
get_symintfromval(den, den_basic);
3232

3333
rational_set(cbasic_operand2, num_basic, den_basic);
3434

@@ -66,6 +66,19 @@ void sympify(VALUE operand2, basic_struct *cbasic_operand2) {
6666
}
6767
}
6868

69+
void get_symintfromval(VALUE operand2, basic_struct *cbasic_operand2)
70+
{
71+
if ( TYPE(operand2) == T_FIXNUM ){
72+
int i = NUM2INT( operand2 );
73+
integer_set_si(cbasic_operand2, i);
74+
} else if ( TYPE(operand2) == T_BIGNUM ){
75+
VALUE Rb_Temp_String = rb_funcall(operand2, rb_intern("to_s"), 0, NULL);
76+
integer_set_str(cbasic_operand2, StringValueCStr(Rb_Temp_String));
77+
} else {
78+
rb_raise(rb_eTypeError, "Invalid Type: Fixnum or Bignum required");
79+
}
80+
}
81+
6982
VALUE Klass_of_Basic(const basic_struct *basic_ptr) {
7083
switch(basic_get_type(basic_ptr)) {
7184
case SYMENGINE_SYMBOL:

ext/symengine/symengine_utils.h

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
#ifndef SYMENGINE_MACROS_H_
2-
#define SYMENGINE_MACROS_H_
1+
#ifndef SYMENGINE_UTILS_H_
2+
#define SYMENGINE_UTILS_H_
33

44
#include "ruby.h"
55
#include "ruby_basic.h"
66
#include "symengine/cwrapper.h"
77

88
//Returns the pointer wrapped inside the Ruby VALUE
99
void sympify(VALUE operand2, basic_struct *cbasic_operand2);
10+
//Returns the pointer wrapped inside the Ruby Fixnum or Bignum
11+
void get_symintfromval(VALUE operand2, basic_struct *cbasic_operand2);
1012
//Returns the Ruby class of the corresponding basic_struct pointer
1113
VALUE Klass_of_Basic(const basic_struct *basic_ptr);
1214
//Returns the result from the function pointed by cwfunc_ptr: for one argument functions
@@ -15,13 +17,4 @@ VALUE function_onearg(void (*cwfunc_ptr)(basic_struct*, const basic_struct*), VA
1517
VALUE function_twoarg(void (*cwfunc_ptr)(basic_struct*, const basic_struct*, const basic_struct*),
1618
VALUE operand1, VALUE operand2);
1719

18-
//Obtains the value from Ruby Fixnum or Bignum to an already allocated basic_struct
19-
#define GET_SYMINTFROMVAL(num_value, this) { \
20-
if ( ! RB_TYPE_P(num_value, T_FIXNUM) && ! RB_TYPE_P(num_value, T_BIGNUM) ) { \
21-
rb_raise(rb_eTypeError, "Invalid Type: Fixnum or Bignum required"); \
22-
} \
23-
VALUE Rb_Temp_String = rb_funcall(num_value, rb_intern("to_s"), 0, NULL); \
24-
integer_set_str(this, StringValueCStr(Rb_Temp_String)); \
25-
}
26-
27-
#endif //SYMENGINE_MACROS_H_
20+
#endif //SYMENGINE_UTILS_H_

0 commit comments

Comments
 (0)