From 8a87549315d0c7fd984a8ad239ebe3dbab4d2855 Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Fri, 14 Aug 2020 14:34:53 +0900 Subject: Rename float configuration option names. - `MRB_WITHOUT_FLOAT` => `MRB_NO_FLOAT` - `MRB_USE_FLOAT` => `MRB_USE_FLOAT32` The former is to use `USE_XXX` naming convention. The latter is to make sure `float` is 32bit float and not floating point number in general. --- test/t/float.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'test/t/float.rb') diff --git a/test/t/float.rb b/test/t/float.rb index dc989636f..1cbc90532 100644 --- a/test/t/float.rb +++ b/test/t/float.rb @@ -207,7 +207,7 @@ assert('Float#>>') do end assert('Float#to_s') do - uses_float = 4e38.infinite? # enable MRB_USE_FLOAT? + uses_float = 4e38.infinite? # enable MRB_USE_FLOAT32? assert_equal("Infinity", Float::INFINITY.to_s) assert_equal("-Infinity", (-Float::INFINITY).to_s) -- cgit v1.2.3 From 8bd1bc47b3e92bfc1c21c698dd7392d8b49a02ef Mon Sep 17 00:00:00 2001 From: KOBAYASHI Shuji Date: Fri, 25 Oct 2019 19:29:19 +0900 Subject: Always add trailing `.0` in `Float#inspect`; ref #4225 Trailing `.0` is removed from `Float#to_s` and `Float#inspect` at 9d08025b. However, I think the more human-readable format is better for `Float#inspect`. For example, in the `Float#to_s` format, the failure message is not well understood when testing values including types by `eql?` (e.g. `Numeric#step` test). ```ruby assert "example" do exp = 1.0 act = 1 assert_operator(exp, :eql?, act) #=> Expected 1 to be eql? 1. end ``` --- src/numeric.c | 61 +++++++++++++++++++++++++++++++++++++++++++++------------ test/t/float.rb | 5 +++++ 2 files changed, 53 insertions(+), 13 deletions(-) (limited to 'test/t/float.rb') diff --git a/src/numeric.c b/src/numeric.c index 40114c07d..617348254 100644 --- a/src/numeric.c +++ b/src/numeric.c @@ -203,19 +203,8 @@ integral_coerce_step_counter(mrb_state *mrb, mrb_value self) * representation. */ -/* 15.2.9.3.16(x) */ -/* - * call-seq: - * flt.to_s -> string - * - * Returns a string containing a representation of self. As well as a - * fixed or exponential form of the number, the call may return - * "NaN", "Infinity", and - * "-Infinity". - */ - static mrb_value -flo_to_s(mrb_state *mrb, mrb_value flt) +flo_to_str(mrb_state *mrb, mrb_value flt, mrb_bool add_dot_zero) { mrb_float f = mrb_float(flt); mrb_value str; @@ -258,6 +247,9 @@ flo_to_s(mrb_state *mrb, mrb_value flt) str = mrb_float_to_str(mrb, flt, fmt); goto insert_dot_zero; } + else if (add_dot_zero) { + mrb_str_cat(mrb, str, ".0", 2); + } goto exit; } @@ -267,6 +259,49 @@ flo_to_s(mrb_state *mrb, mrb_value flt) return str; } +/* 15.2.9.3.16(x) */ +/* + * call-seq: + * flt.to_s -> string + * + * Returns a string containing a representation of self. As well as a + * fixed or exponential form of the number, the call may return + * "NaN", "Infinity", and + * "-Infinity". + * + * Trailing .0 is removed. + * + * 3.0.to_s #=> 3 + * 3.25.to_s #=> 3.25 + */ + +static mrb_value +flo_to_s(mrb_state *mrb, mrb_value flt) +{ + return flo_to_str(mrb, flt, FALSE); +} + +/* + * call-seq: + * flt.inspect -> string + * + * Returns a string containing a representation of self. As well as a + * fixed or exponential form of the number, the call may return + * "NaN", "Infinity", and + * "-Infinity". + * + * Trailing .0 is added. + * + * 3.0.to_s #=> 3.0 + * 3.25.to_s #=> 3.25 + */ + +static mrb_value +flo_inspect(mrb_state *mrb, mrb_value flt) +{ + return flo_to_str(mrb, flt, TRUE); +} + /* 15.2.9.3.2 */ /* * call-seq: @@ -1684,7 +1719,7 @@ mrb_init_numeric(mrb_state *mrb) mrb_define_method(mrb, fl, "eql?", flo_eql, MRB_ARGS_REQ(1)); /* 15.2.8.3.16 */ 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, "inspect", flo_inspect, MRB_ARGS_NONE()); mrb_define_method(mrb, fl, "nan?", flo_nan_p, MRB_ARGS_NONE()); #ifdef INFINITY diff --git a/test/t/float.rb b/test/t/float.rb index 1cbc90532..e2a8a5088 100644 --- a/test/t/float.rb +++ b/test/t/float.rb @@ -239,6 +239,11 @@ assert('Float#to_s') do end end +assert('Float#inspect') do + assert_equal("-3.25", -3.25.inspect) + assert_equal("50.0", 50.0.inspect) +end + assert('Float#eql?') do assert_operator(5.0, :eql?, 5.0) assert_not_operator(5.0, :eql?, 5) -- cgit v1.2.3