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
0270d11c
Commit
0270d11c
authored
Aug 01, 2012
by
Yukihiro Matsumoto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
float do not have enough precision to round if MRB_USE_FLOAT is set
parent
1563cdfc
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
31 additions
and
30 deletions
+31
-30
src/numeric.c
src/numeric.c
+14
-13
test/t/float.rb
test/t/float.rb
+17
-17
No files found.
src/numeric.c
View file @
0270d11c
...
...
@@ -195,7 +195,11 @@ flo_to_s(mrb_state *mrb, mrb_value flt)
}
else
if
(
isnan
(
value
))
return
mrb_str_new
(
mrb
,
"NaN"
,
3
);
#ifdef MRB_USE_FLOAT
n
=
sprintf
(
buf
,
"%.7g"
,
value
);
#else
n
=
sprintf
(
buf
,
"%.14g"
,
value
);
#endif
assert
(
n
>=
0
);
return
mrb_str_new
(
mrb
,
buf
,
n
);
}
...
...
@@ -511,21 +515,13 @@ flo_ceil(mrb_state *mrb, mrb_value num)
*/
static
mrb_value
flo_round
(
mrb_state
*
mrb
,
/*int argc, mrb_value *argv,*/
mrb_value
num
)
flo_round
(
mrb_state
*
mrb
,
mrb_value
num
)
{
mrb_value
nd
;
mrb_float
number
,
f
;
double
number
,
f
;
int
ndigits
=
0
,
i
;
mrb_value
*
argv
;
int
argc
;
mrb_get_args
(
mrb
,
"*"
,
&
argv
,
&
argc
);
if
(
argc
==
1
)
{
nd
=
argv
[
0
];
ndigits
=
mrb_fixnum
(
nd
);
}
number
=
mrb_float
(
num
);
mrb_get_args
(
mrb
,
"|i"
,
&
ndigits
);
number
=
mrb_float
(
num
);
f
=
1
.
0
;
i
=
abs
(
ndigits
);
while
(
--
i
>=
0
)
...
...
@@ -535,7 +531,7 @@ flo_round(mrb_state *mrb, /*int argc, mrb_value *argv,*/ mrb_value num)
if
(
ndigits
<
0
)
number
=
0
;
}
else
{
mrb_float
d
;
double
d
;
if
(
ndigits
<
0
)
number
/=
f
;
else
number
*=
f
;
...
...
@@ -554,6 +550,11 @@ flo_round(mrb_state *mrb, /*int argc, mrb_value *argv,*/ mrb_value num)
else
number
/=
f
;
}
{
mrb_value
ff
=
mrb_float_value
(
number
);
printf
(
"%f.round(%d) = %f
\n
"
,
mrb_float
(
num
),
ndigits
,
mrb_float
(
ff
));
}
if
(
ndigits
>
0
)
return
mrb_float_value
(
number
);
return
mrb_fixnum_value
((
mrb_int
)
number
);
}
...
...
test/t/float.rb
View file @
0270d11c
...
...
@@ -26,11 +26,11 @@ assert('Float#-', '15.2.9.3.2') do
end
assert
(
'Float#*'
,
'15.2.9.3.3'
)
do
a
=
3.12
3456789
*
3.123456789
b
=
3.12
3456789
*
1
a
=
3.12
5
*
3.125
b
=
3.12
5
*
1
check_float
(
a
,
9.7
5598231275019
)
and
check_float
(
b
,
3.12
3456789
)
check_float
(
a
,
9.7
65625
)
and
check_float
(
b
,
3.12
5
)
end
assert
(
'Float#/'
,
'15.2.9.3.4'
)
do
...
...
@@ -42,19 +42,19 @@ assert('Float#/', '15.2.9.3.4') do
end
assert
(
'Float#%'
,
'15.2.9.3.5'
)
do
a
=
3.12
3456789
%
3.123456789
b
=
3.12
3456789
%
1
a
=
3.12
5
%
3.125
b
=
3.12
5
%
1
check_float
(
a
,
0.0
)
and
check_float
(
b
,
0.12
3456789
)
check_float
(
b
,
0.12
5
)
end
assert
(
'Float#<=>'
,
'15.2.9.3.6'
)
do
a
=
3.12
3456789
<=>
3.123456788
b
=
3.12
3456789
<=>
3.123456789
c
=
3.12
3456789
<=>
3.123456790
a2
=
3.12
3456789
<=>
3
c2
=
3.12
3456789
<=>
4
a
=
3.12
5
<=>
3.123
b
=
3.12
5
<=>
3.125
c
=
3.12
5
<=>
3.126
a2
=
3.12
5
<=>
3
c2
=
3.12
5
<=>
4
a
==
1
and
b
==
0
and
c
==
-
1
and
a2
==
1
and
c2
==
-
1
...
...
@@ -96,16 +96,16 @@ end
assert
(
'Float#round'
,
'15.2.9.3.12'
)
do
a
=
3.123456789
.
round
b
=
3.5
.
round
c
=
3.4999
99999
.
round
c
=
3.4999
.
round
d
=
(
-
3.123456789
).
round
e
=
(
-
3.5
).
round
f
=
12345.67
.
round
(
-
1
)
g
=
3.
1
23456789
.
round
(
0
)
h
=
3.
1
23456789
.
round
(
1
)
i
=
3.
123456789
.
round
(
4
)
g
=
3.
4
23456789
.
round
(
0
)
h
=
3.
4
23456789
.
round
(
1
)
i
=
3.
423456789
.
round
(
3
)
a
==
3
and
b
==
4
and
c
==
3
and
d
==
-
3
and
e
==
-
4
and
f
==
12350
and
g
==
3
and
h
==
3.
1
and
i
==
3.1235
f
==
12350
and
g
==
3
and
h
==
3.
4
and
i
==
3.423
end
assert
(
'Float#to_f'
,
'15.2.9.3.13'
)
do
...
...
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