numeric.c: fix: `-0.0.abs` returned `-0.0`.
MODIFY
src/numeric.c
src/numeric.c
@@ -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));

MODIFY
test/t/float.rb
test/t/float.rb
@@ -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)

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