Skip to content

Commit ca927d6

Browse files
committed
* complex.c (string_to_c_internal): support scientific notation.
patched by Tinco Andringa. ruby/ruby#16 [ruby-core:36046][Bug #4655] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@31492 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
1 parent d3e5a29 commit ca927d6

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

ChangeLog

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
Mon May 9 13:49:00 2011 Kenta Murata <[email protected]>
2+
3+
* complex.c (string_to_c_internal): support scientific notation.
4+
patched by Tinco Andringa. https://github.com/ruby/ruby/pull/16
5+
[ruby-core:36046][Bug #4655]
6+
17
Mon May 9 11:52:48 2011 NARUSE, Yui <[email protected]>
28

39
* numeric.c (int_ord): remove K&R style.

complex.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1498,6 +1498,7 @@ string_to_c_internal(VALUE self)
14981498
m = f_match(comp_pat2, s);
14991499
if (NIL_P(m))
15001500
return rb_assoc_new(Qnil, self);
1501+
/* string is of form "x+yi" */
15011502
sr = f_aref(m, INT2FIX(1));
15021503
if (NIL_P(f_aref(m, INT2FIX(2))))
15031504
si = Qnil;
@@ -1518,15 +1519,15 @@ string_to_c_internal(VALUE self)
15181519
if (!NIL_P(sr)) {
15191520
if (strchr(RSTRING_PTR(sr), '/'))
15201521
r = f_to_r(sr);
1521-
else if (strchr(RSTRING_PTR(sr), '.'))
1522+
else if (strchr(RSTRING_PTR(sr), '.') || strchr(RSTRING_PTR(sr), 'e') || strchr(RSTRING_PTR(sr), 'E'))
15221523
r = f_to_f(sr);
15231524
else
15241525
r = f_to_i(sr);
15251526
}
15261527
if (!NIL_P(si)) {
15271528
if (strchr(RSTRING_PTR(si), '/'))
15281529
i = f_to_r(si);
1529-
else if (strchr(RSTRING_PTR(si), '.'))
1530+
else if (strchr(RSTRING_PTR(si), '.') || strchr(RSTRING_PTR(si), 'e') || strchr(RSTRING_PTR(si), 'E'))
15301531
i = f_to_f(si);
15311532
else
15321533
i = f_to_i(si);

test/ruby/test_complex.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,10 @@ def test_parse
704704
assert_equal(Complex(-0.33), '-0.33'.to_c)
705705
assert_equal(Complex(-0.33), '-0.3_3'.to_c)
706706

707+
assert_equal(Complex(2, 2e4), '2+2e4i'.to_c)
708+
assert_equal(Complex(2e3, 2), '2e3+2i'.to_c)
709+
assert_equal(Complex(2e3, 2e4), '2e3+2e4i'.to_c)
710+
707711
assert_equal(Complex.polar(10,10), '10@10'.to_c)
708712
assert_equal(Complex.polar(-10,-10), '-10@-10'.to_c)
709713
assert_equal(Complex.polar(10.5,10.5), '[email protected]'.to_c)

0 commit comments

Comments
 (0)