Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
M
mruby
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
mruby
Commits
1974aa0f
Unverified
Commit
1974aa0f
authored
Nov 18, 2020
by
Yukihiro "Matz" Matsumoto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update `Float#to_s` to keep trailing zero as CRuby does; ref
68cebb63
parent
5f7eb202
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
25 additions
and
53 deletions
+25
-53
src/numeric.c
src/numeric.c
+18
-46
test/t/float.rb
test/t/float.rb
+7
-7
No files found.
src/numeric.c
View file @
1974aa0f
...
@@ -249,8 +249,23 @@ flo_div(mrb_state *mrb, mrb_value xv)
...
@@ -249,8 +249,23 @@ flo_div(mrb_state *mrb, mrb_value xv)
return
mrb_float_value
(
mrb
,
x
);
return
mrb_float_value
(
mrb
,
x
);
}
}
/* 15.2.9.3.16(x) */
/*
* call-seq:
* flt.to_s -> string
* 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
* "<code>NaN</code>", "<code>Infinity</code>", and
* "<code>-Infinity</code>".
*
* 3.0.to_s #=> 3.0
* 3.25.to_s #=> 3.25
*/
static
mrb_value
static
mrb_value
flo_to_s
tr
(
mrb_state
*
mrb
,
mrb_value
flt
,
mrb_bool
add_dot_zero
)
flo_to_s
(
mrb_state
*
mrb
,
mrb_value
flt
)
{
{
mrb_float
f
=
mrb_float
(
flt
);
mrb_float
f
=
mrb_float
(
flt
);
mrb_value
str
;
mrb_value
str
;
...
@@ -293,7 +308,7 @@ flo_to_str(mrb_state *mrb, mrb_value flt, mrb_bool add_dot_zero)
...
@@ -293,7 +308,7 @@ flo_to_str(mrb_state *mrb, mrb_value flt, mrb_bool add_dot_zero)
str
=
mrb_float_to_str
(
mrb
,
flt
,
fmt
);
str
=
mrb_float_to_str
(
mrb
,
flt
,
fmt
);
goto
insert_dot_zero
;
goto
insert_dot_zero
;
}
}
else
if
(
add_dot_zero
)
{
else
{
mrb_str_cat
(
mrb
,
str
,
".0"
,
2
);
mrb_str_cat
(
mrb
,
str
,
".0"
,
2
);
}
}
...
@@ -305,49 +320,6 @@ flo_to_str(mrb_state *mrb, mrb_value flt, mrb_bool add_dot_zero)
...
@@ -305,49 +320,6 @@ flo_to_str(mrb_state *mrb, mrb_value flt, mrb_bool add_dot_zero)
return
str
;
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
* "<code>NaN</code>", "<code>Infinity</code>", and
* "<code>-Infinity</code>".
*
* Trailing <code>.0</code> 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
* "<code>NaN</code>", "<code>Infinity</code>", and
* "<code>-Infinity</code>".
*
* Trailing <code>.0</code> 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 */
/* 15.2.9.3.2 */
/*
/*
* call-seq:
* call-seq:
...
@@ -1725,7 +1697,7 @@ mrb_init_numeric(mrb_state *mrb)
...
@@ -1725,7 +1697,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
,
"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
,
"to_s"
,
flo_to_s
,
MRB_ARGS_NONE
());
/* 15.2.9.3.16(x) */
mrb_define_method
(
mrb
,
fl
,
"inspect"
,
flo_
inspect
,
MRB_ARGS_NONE
());
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
,
"nan?"
,
flo_nan_p
,
MRB_ARGS_NONE
());
#ifdef INFINITY
#ifdef INFINITY
...
...
test/t/float.rb
View file @
1974aa0f
...
@@ -212,10 +212,10 @@ assert('Float#to_s') do
...
@@ -212,10 +212,10 @@ assert('Float#to_s') do
assert_equal
(
"Infinity"
,
Float
::
INFINITY
.
to_s
)
assert_equal
(
"Infinity"
,
Float
::
INFINITY
.
to_s
)
assert_equal
(
"-Infinity"
,
(
-
Float
::
INFINITY
).
to_s
)
assert_equal
(
"-Infinity"
,
(
-
Float
::
INFINITY
).
to_s
)
assert_equal
(
"NaN"
,
Float
::
NAN
.
to_s
)
assert_equal
(
"NaN"
,
Float
::
NAN
.
to_s
)
assert_equal
(
"0"
,
0.0
.
to_s
)
assert_equal
(
"0
.0
"
,
0.0
.
to_s
)
assert_equal
(
"-0"
,
-
0.0
.
to_s
)
assert_equal
(
"-0
.0
"
,
-
0.0
.
to_s
)
assert_equal
(
"-3.25"
,
-
3.25
.
to_s
)
assert_equal
(
"-3.25"
,
-
3.25
.
to_s
)
assert_equal
(
"50"
,
50.0
.
to_s
)
assert_equal
(
"50
.0
"
,
50.0
.
to_s
)
assert_equal
(
"0.0125"
,
0.0125
.
to_s
)
assert_equal
(
"0.0125"
,
0.0125
.
to_s
)
assert_equal
(
"-0.0125"
,
-
0.0125
.
to_s
)
assert_equal
(
"-0.0125"
,
-
0.0125
.
to_s
)
assert_equal
(
"1.0e-10"
,
0.0000000001
.
to_s
)
assert_equal
(
"1.0e-10"
,
0.0000000001
.
to_s
)
...
@@ -224,8 +224,8 @@ assert('Float#to_s') do
...
@@ -224,8 +224,8 @@ assert('Float#to_s') do
assert_equal
(
"-1.0e+20"
,
-
1e20
.
to_s
)
assert_equal
(
"-1.0e+20"
,
-
1e20
.
to_s
)
assert_equal
(
"1.0e+16"
,
10000000000000000.0
.
to_s
)
assert_equal
(
"1.0e+16"
,
10000000000000000.0
.
to_s
)
assert_equal
(
"-1.0e+16"
,
-
10000000000000000.0
.
to_s
)
assert_equal
(
"-1.0e+16"
,
-
10000000000000000.0
.
to_s
)
assert_equal
(
"100000"
,
100000.0
.
to_s
)
assert_equal
(
"100000
.0
"
,
100000.0
.
to_s
)
assert_equal
(
"-100000"
,
-
100000.0
.
to_s
)
assert_equal
(
"-100000
.0
"
,
-
100000.0
.
to_s
)
if
uses_float
if
uses_float
assert_equal
(
"1.0e+08"
,
100000000.0
.
to_s
)
assert_equal
(
"1.0e+08"
,
100000000.0
.
to_s
)
assert_equal
(
"-1.0e+08"
,
-
100000000.0
.
to_s
)
assert_equal
(
"-1.0e+08"
,
-
100000000.0
.
to_s
)
...
@@ -234,8 +234,8 @@ assert('Float#to_s') do
...
@@ -234,8 +234,8 @@ assert('Float#to_s') do
else
else
assert_equal
(
"1.0e+15"
,
1000000000000000.0
.
to_s
)
assert_equal
(
"1.0e+15"
,
1000000000000000.0
.
to_s
)
assert_equal
(
"-1.0e+15"
,
-
1000000000000000.0
.
to_s
)
assert_equal
(
"-1.0e+15"
,
-
1000000000000000.0
.
to_s
)
assert_equal
(
"100000000000000"
,
100000000000000.0
.
to_s
)
assert_equal
(
"100000000000000
.0
"
,
100000000000000.0
.
to_s
)
assert_equal
(
"-100000000000000"
,
-
100000000000000.0
.
to_s
)
assert_equal
(
"-100000000000000
.0
"
,
-
100000000000000.0
.
to_s
)
end
end
end
end
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment