Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions complex.c
Original file line number Diff line number Diff line change
Expand Up @@ -1498,6 +1498,7 @@ string_to_c_internal(VALUE self)
m = f_match(comp_pat2, s);
if (NIL_P(m))
return rb_assoc_new(Qnil, self);
// string is of form "x+yi"
sr = f_aref(m, INT2FIX(1));
if (NIL_P(f_aref(m, INT2FIX(2))))
si = Qnil;
Expand All @@ -1518,15 +1519,15 @@ string_to_c_internal(VALUE self)
if (!NIL_P(sr)) {
if (strchr(RSTRING_PTR(sr), '/'))
r = f_to_r(sr);
else if (strchr(RSTRING_PTR(sr), '.'))
else if (strchr(RSTRING_PTR(sr), '.') || strchr(RSTRING_PTR(sr), 'e') || strchr(RSTRING_PTR(sr), 'E'))
r = f_to_f(sr);
else
r = f_to_i(sr);
}
if (!NIL_P(si)) {
if (strchr(RSTRING_PTR(si), '/'))
i = f_to_r(si);
else if (strchr(RSTRING_PTR(si), '.'))
else if (strchr(RSTRING_PTR(si), '.') || strchr(RSTRING_PTR(si), 'e') || strchr(RSTRING_PTR(si), 'E'))
i = f_to_f(si);
else
i = f_to_i(si);
Expand Down
4 changes: 4 additions & 0 deletions test/ruby/test_complex.rb
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,10 @@ def test_parse
assert_equal(Complex(-0.33), '-0.33'.to_c)
assert_equal(Complex(-0.33), '-0.3_3'.to_c)

assert_equal(Complex(2, 2e4), '2+2e4i'.to_c)
assert_equal(Complex(2e3, 2), '2e3+2i'.to_c)
assert_equal(Complex(2e3, 2e4), '2e3+2e4i'.to_c)

assert_equal(Complex.polar(10,10), '10@10'.to_c)
assert_equal(Complex.polar(-10,-10), '-10@-10'.to_c)
assert_equal(Complex.polar(10.5,10.5), '[email protected]'.to_c)
Expand Down