numeric.c: fix: -0.0.abs returned -0.0.

This commit is contained in:
Yukihiro "Matz" Matsumoto
2021-08-23 10:44:04 +09:00
parent 11cfe77eb1
commit 6f46f88873
2 changed files with 22 additions and 0 deletions
+10
View File
@@ -978,6 +978,15 @@ flo_nan_p(mrb_state *mrb, mrb_value num)
{
return mrb_bool_value(isnan(mrb_float(num)));
}
static mrb_value
flo_abs(mrb_state *mrb, mrb_value num)
{
mrb_float f = mrb_float(num);
if (signbit(f)) return mrb_float_value(mrb, -f);
return num;
}
#endif
/*
@@ -1910,6 +1919,7 @@ mrb_init_numeric(mrb_state *mrb)
mrb_define_method(mrb, fl, "to_s", flo_to_s, MRB_ARGS_NONE()); /* 15.2.9.3.16(x) */
mrb_define_method(mrb, fl, "inspect", flo_to_s, MRB_ARGS_NONE());
mrb_define_method(mrb, fl, "nan?", flo_nan_p, MRB_ARGS_NONE());
mrb_define_method(mrb, fl, "abs", flo_abs, MRB_ARGS_NONE()); /* 15.2.7.4.3 */
#ifdef INFINITY
mrb_define_const_id(mrb, fl, MRB_SYM(INFINITY), mrb_float_value(mrb, INFINITY));
+12
View File
@@ -303,4 +303,16 @@ assert('Float#eql?') do
assert_not_operator(5.0, :eql?, "5.0")
end
assert('Float#abs') do
f = 1.0
assert_equal(1.0, f.abs)
f = -1.0
assert_equal(1.0, f.abs)
f = 0.0
assert_equal(0.0, f.abs)
# abs(negative zero) should be positive zero
f = -0.0
assert_equal(0.0, f.abs)
end
end # const_defined?(:Float)