Skip to content

Commit 46ee731

Browse files
committed
Changing to_f method for real_double
1 parent 0e4bfd4 commit 46ee731

File tree

7 files changed

+33
-14
lines changed

7 files changed

+33
-14
lines changed

ext/symengine/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ set(RUBY_WRAPPER_SRC
22
ruby_basic.c
33
ruby_symbol.c
44
ruby_integer.c
5+
ruby_real_double.c
56
ruby_complex.c
67
ruby_constant.c
78
ruby_function.c

ext/symengine/ruby_real_double.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include "ruby_real_double.h"
2+
3+
4+
VALUE crealdouble_to_float(VALUE self)
5+
{
6+
VALUE result;
7+
8+
basic cbasic_operand1;
9+
basic_new_stack(cbasic_operand1);
10+
sympify(self, cbasic_operand1);
11+
12+
result = rb_float_new(real_double_get_d(cbasic_operand1));
13+
basic_free_stack(cbasic_operand1);
14+
15+
return result;
16+
}

ext/symengine/ruby_real_double.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#ifndef RUBY_REAL_DOUBLE_H_
2+
#define RUBY_REAL_DOUBLE_H_
3+
4+
#include <ruby.h>
5+
#include <symengine/cwrapper.h>
6+
7+
#include "symengine.h"
8+
#include "symengine_utils.h"
9+
10+
VALUE crealdouble_to_float(VALUE self);
11+
12+
#endif //RUBY_REAL_DOUBLE_H_

ext/symengine/symengine.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "ruby_basic.h"
33
#include "ruby_symbol.h"
44
#include "ruby_integer.h"
5+
#include "ruby_real_double.h"
56
#include "ruby_constant.h"
67
#include "ruby_complex.h"
78
#include "ruby_function.h"
@@ -65,6 +66,7 @@ void Init_symengine() {
6566
//RealDouble Class
6667
c_real_double = rb_define_class_under(m_symengine, "RealDouble", c_basic);
6768
rb_define_alloc_func(c_real_double, cbasic_alloc);
69+
rb_define_method(c_real_double, "to_f", crealdouble_to_float, 0);
6870

6971
//Rational class
7072
c_rational = rb_define_class_under(m_symengine, "Rational", c_basic);

ext/symengine/symengine_utils.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ void sympify(VALUE operand2, basic_struct *cbasic_operand2) {
66
basic_struct *temp;
77
VALUE new_operand2, num, den;
88
VALUE real, imag;
9-
VALUE Rb_Temp_String;
10-
char *s;
9+
double f;
1110

1211
switch(TYPE(operand2)) {
1312
case T_FIXNUM:
@@ -16,10 +15,7 @@ void sympify(VALUE operand2, basic_struct *cbasic_operand2) {
1615
break;
1716

1817
case T_FLOAT:
19-
Rb_Temp_String = rb_funcall(operand2, rb_intern("to_s"), 0, NULL);
20-
21-
s = StringValueCStr(Rb_Temp_String);
22-
double f = atof(s);
18+
f = rb_float_value(operand2);
2319
real_double_set_d(cbasic_operand2, f);
2420
break;
2521

lib/symengine.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,4 @@ def symbols ary_or_string, *params
3636
require 'symengine/basic'
3737
require 'symengine/integer'
3838
require 'symengine/complex'
39-
require 'symengine/real_double'
4039
require 'symengine/complex_double'

lib/symengine/real_double.rb

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)