mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
rational.c: float denom
* rational.c (parse_rat): allow float as a denominator as well as a numerator. [ruby-core:79104] [Bug #13134] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@57990 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
62fb6147cd
commit
a2ac0982cd
2 changed files with 19 additions and 8 deletions
13
rational.c
13
rational.c
|
@ -2356,7 +2356,7 @@ static VALUE
|
||||||
parse_rat(const char *s, const char *const e, int strict)
|
parse_rat(const char *s, const char *const e, int strict)
|
||||||
{
|
{
|
||||||
int sign;
|
int sign;
|
||||||
VALUE num, den, ndiv;
|
VALUE num, den, ndiv, ddiv;
|
||||||
|
|
||||||
s = skip_ws(s, e);
|
s = skip_ws(s, e);
|
||||||
sign = read_sign(&s, e);
|
sign = read_sign(&s, e);
|
||||||
|
@ -2368,12 +2368,8 @@ parse_rat(const char *s, const char *const e, int strict)
|
||||||
nurat_reduce(&num, &ndiv);
|
nurat_reduce(&num, &ndiv);
|
||||||
den = ndiv;
|
den = ndiv;
|
||||||
if (s < e && *s == '/') {
|
if (s < e && *s == '/') {
|
||||||
char *t;
|
|
||||||
s++;
|
s++;
|
||||||
den = rb_int_parse_cstr(s, e-s, &t, NULL,
|
if (!read_num(&s, e, &den, &ddiv)) {
|
||||||
10, RB_INT_PARSE_UNDERSCORE);
|
|
||||||
s = t;
|
|
||||||
if (NIL_P(den)) {
|
|
||||||
if (strict) return Qnil;
|
if (strict) return Qnil;
|
||||||
den = ndiv;
|
den = ndiv;
|
||||||
}
|
}
|
||||||
|
@ -2384,8 +2380,11 @@ parse_rat(const char *s, const char *const e, int strict)
|
||||||
return Qnil;
|
return Qnil;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
nurat_reduce(&den, &ddiv);
|
||||||
nurat_reduce(&num, &den);
|
nurat_reduce(&num, &den);
|
||||||
den = rb_int_mul(den, ndiv);
|
nurat_reduce(&ndiv, &ddiv);
|
||||||
|
if (ndiv != ONE) den = rb_int_mul(den, ndiv);
|
||||||
|
if (ddiv != ONE) num = rb_int_mul(num, ddiv);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (strict && skip_ws(s, e) != e) {
|
else if (strict && skip_ws(s, e) != e) {
|
||||||
|
|
|
@ -747,6 +747,19 @@ class Rational_Test < Test::Unit::TestCase
|
||||||
ng[ 5, 1, '5e_1']
|
ng[ 5, 1, '5e_1']
|
||||||
ng[50, 1, '5e1_']
|
ng[50, 1, '5e1_']
|
||||||
|
|
||||||
|
ok[ 50, 33, '5/3.3']
|
||||||
|
ok[ 5, 3, '5/3e0']
|
||||||
|
ok[ 5, 30, '5/3e1']
|
||||||
|
ng[ 5, 3, '5/3._3']
|
||||||
|
ng[ 50, 33, '5/3.3_']
|
||||||
|
ok[500,333, '5/3.3_3']
|
||||||
|
ng[ 5, 3, '5/3e']
|
||||||
|
ng[ 5, 3, '5/3_e']
|
||||||
|
ng[ 5, 3, '5/3e_']
|
||||||
|
ng[ 5, 3, '5/3e_1']
|
||||||
|
ng[ 5, 30, '5/3e1_']
|
||||||
|
ok[ 5, 300000000000, '5/3e1_1']
|
||||||
|
|
||||||
ng[0, 1, '']
|
ng[0, 1, '']
|
||||||
ng[0, 1, ' ']
|
ng[0, 1, ' ']
|
||||||
ng[5, 1, "\f\n\r\t\v5\0"]
|
ng[5, 1, "\f\n\r\t\v5\0"]
|
||||||
|
@ -756,7 +769,6 @@ class Rational_Test < Test::Unit::TestCase
|
||||||
ng[5, 1, '5x']
|
ng[5, 1, '5x']
|
||||||
ng[5, 1, '5/_3']
|
ng[5, 1, '5/_3']
|
||||||
ng[5, 3, '5/3_']
|
ng[5, 3, '5/3_']
|
||||||
ng[5, 3, '5/3.3']
|
|
||||||
ng[5, 3, '5/3x']
|
ng[5, 3, '5/3x']
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue