Check `NaN` and `Infinity` before converting `Float` to `Rational`.

parent 392aff70
...@@ -105,6 +105,9 @@ typedef float rat_float; ...@@ -105,6 +105,9 @@ typedef float rat_float;
#else #else
typedef double rat_float; typedef double rat_float;
#endif #endif
void mrb_check_num_exact(mrb_state *mrb, mrb_float num);
static mrb_value static mrb_value
rational_new_f(mrb_state *mrb, mrb_float f0) rational_new_f(mrb_state *mrb, mrb_float f0)
{ {
...@@ -116,6 +119,7 @@ rational_new_f(mrb_state *mrb, mrb_float f0) ...@@ -116,6 +119,7 @@ rational_new_f(mrb_state *mrb, mrb_float f0)
int64_t n = 1; int64_t n = 1;
int i, neg = 0; int i, neg = 0;
mrb_check_num_exact(mrb, f0);
if (f < 0) { neg = 1; f = -f; } if (f < 0) { neg = 1; f = -f; }
while (f != floor(f)) { n <<= 1; f *= 2; } while (f != floor(f)) { n <<= 1; f *= 2; }
if (!TYPED_FIXABLE(f, rat_float)) { if (!TYPED_FIXABLE(f, rat_float)) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment