Unverified Commit db3573e0 authored by ksss's avatar ksss

Should raise FloatDomainError

parent 1f6bf0e1
...@@ -211,16 +211,7 @@ mrb_time_wrap(mrb_state *mrb, struct RClass *tc, struct mrb_time *tm) ...@@ -211,16 +211,7 @@ mrb_time_wrap(mrb_state *mrb, struct RClass *tc, struct mrb_time *tm)
return mrb_obj_value(Data_Wrap_Struct(mrb, tc, &mrb_time_type, tm)); return mrb_obj_value(Data_Wrap_Struct(mrb, tc, &mrb_time_type, tm));
} }
static void void mrb_check_num_exact(mrb_state *mrb, mrb_float num);
check_num_exact(mrb_state *mrb, double num)
{
if (isinf(num)) {
mrb_raise(mrb, E_FLOATDOMAIN_ERROR, num < 0 ? "-Infinity" : "Infinity");
}
if (isnan(num)) {
mrb_raise(mrb, E_FLOATDOMAIN_ERROR, "NaN");
}
}
/* Allocates a mrb_time object and initializes it. */ /* Allocates a mrb_time object and initializes it. */
static struct mrb_time* static struct mrb_time*
...@@ -229,8 +220,8 @@ time_alloc(mrb_state *mrb, double sec, double usec, enum mrb_timezone timezone) ...@@ -229,8 +220,8 @@ time_alloc(mrb_state *mrb, double sec, double usec, enum mrb_timezone timezone)
struct mrb_time *tm; struct mrb_time *tm;
time_t tsec = 0; time_t tsec = 0;
check_num_exact(mrb, sec); mrb_check_num_exact(mrb, (mrb_float)sec);
check_num_exact(mrb, usec); mrb_check_num_exact(mrb, (mrb_float)usec);
if (sizeof(time_t) == 4 && (sec > (double)INT32_MAX || (double)INT32_MIN > sec)) { if (sizeof(time_t) == 4 && (sec > (double)INT32_MAX || (double)INT32_MIN > sec)) {
goto out_of_range; goto out_of_range;
......
...@@ -610,6 +610,17 @@ flo_round(mrb_state *mrb, mrb_value num) ...@@ -610,6 +610,17 @@ flo_round(mrb_state *mrb, mrb_value num)
return mrb_fixnum_value((mrb_int)number); return mrb_fixnum_value((mrb_int)number);
} }
void
mrb_check_num_exact(mrb_state *mrb, mrb_float num)
{
if (isinf(num)) {
mrb_raise(mrb, E_FLOATDOMAIN_ERROR, num < 0 ? "-Infinity" : "Infinity");
}
if (isnan(num)) {
mrb_raise(mrb, E_FLOATDOMAIN_ERROR, "NaN");
}
}
/* 15.2.9.3.14 */ /* 15.2.9.3.14 */
/* 15.2.9.3.15 */ /* 15.2.9.3.15 */
/* /*
...@@ -630,6 +641,7 @@ flo_truncate(mrb_state *mrb, mrb_value num) ...@@ -630,6 +641,7 @@ flo_truncate(mrb_state *mrb, mrb_value num)
if (f < 0.0) f = ceil(f); if (f < 0.0) f = ceil(f);
if (!FIXABLE(f)) { if (!FIXABLE(f)) {
mrb_check_num_exact(mrb, f);
return mrb_float_value(mrb, f); return mrb_float_value(mrb, f);
} }
return mrb_fixnum_value((mrb_int)f); return mrb_fixnum_value((mrb_int)f);
......
...@@ -148,6 +148,9 @@ end ...@@ -148,6 +148,9 @@ end
assert('Float#to_i', '15.2.9.3.14') do assert('Float#to_i', '15.2.9.3.14') do
assert_equal(3, 3.123456789.to_i) assert_equal(3, 3.123456789.to_i)
assert_raise(FloatDomainError) { Float::INFINITY.to_i }
assert_raise(FloatDomainError) { (-Float::INFINITY).to_i }
assert_raise(FloatDomainError) { Float::NAN.to_i }
end end
assert('Float#truncate', '15.2.9.3.15') do assert('Float#truncate', '15.2.9.3.15') do
......
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