Skip to content

Commit 1f8cb65

Browse files
committed
to_f for RealMPFR
1 parent 90cbfec commit 1f8cb65

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

ext/symengine/ruby_real_mpfr.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "ruby_real_mpfr.h"
22

3-
VALUE crealmpfr_init(VALUE self, VALUE num_value, VALUE prec_value) {
3+
VALUE crealmpfr_init(VALUE self, VALUE num_value, VALUE prec_value)
4+
{
45
basic_struct *cresult;
56
double d;
67
char *c;
@@ -32,3 +33,16 @@ VALUE crealmpfr_init(VALUE self, VALUE num_value, VALUE prec_value) {
3233
return self;
3334
}
3435

36+
VALUE crealmpfr_to_float(VALUE self)
37+
{
38+
VALUE result;
39+
basic cbasic_operand1;
40+
basic_new_stack(cbasic_operand1);
41+
sympify(self, cbasic_operand1);
42+
43+
result = rb_float_new(real_mpfr_get_d(cbasic_operand1));
44+
45+
basic_free_stack(cbasic_operand1);
46+
return result;
47+
}
48+

ext/symengine/ruby_real_mpfr.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@
88
#include "symengine_utils.h"
99

1010
VALUE crealmpfr_init(VALUE self, VALUE num_value, VALUE prec_value);
11+
VALUE crealmpfr_to_float(VALUE self);
1112

1213
#endif //RUBY_REAL_MPFR_H_

ext/symengine/symengine.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ void Init_symengine() {
9090
c_real_mpfr = rb_define_class_under(m_symengine, "RealMPFR", c_basic);
9191
rb_define_alloc_func(c_real_mpfr, cbasic_alloc);
9292
rb_define_method(c_real_mpfr, "initialize", crealmpfr_init, 2);
93+
rb_define_method(c_real_mpfr, "to_f", crealmpfr_to_float, 0);
9394

9495
//ComplexMPC class
9596
c_complex_mpc = rb_define_class_under(m_symengine, "ComplexMPC", c_basic);

0 commit comments

Comments
 (0)