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
9be437c6
Commit
9be437c6
authored
Mar 17, 2013
by
Masaki Muranaka
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add a new function mrb_flo_to_str(). Use it instead of sprintf() as possible.
parent
fd69e271
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
98 additions
and
17 deletions
+98
-17
src/numeric.c
src/numeric.c
+98
-17
No files found.
src/numeric.c
View file @
9be437c6
...
...
@@ -163,6 +163,99 @@ num_abs(mrb_state *mrb, mrb_value num)
* representation.
*/
mrb_value
mrb_flo_to_str
(
mrb_state
*
mrb
,
mrb_float
n
,
int
max_digit
)
{
mrb_value
result
;
if
(
max_digit
>
40
)
{
mrb_raise
(
mrb
,
E_RANGE_ERROR
,
"Too large max_digit."
);
}
if
(
isnan
(
n
))
{
result
=
mrb_str_new
(
mrb
,
"NaN"
,
3
);
}
else
if
(
isinf
(
n
))
{
if
(
n
<
0
)
{
result
=
mrb_str_new
(
mrb
,
"-inf"
,
4
);
}
else
{
result
=
mrb_str_new
(
mrb
,
"inf"
,
3
);
}
}
else
{
int
digit
;
int
m
;
int
exp
;
int
e
=
0
;
char
s
[
48
];
char
*
c
=
&
s
[
0
];
if
(
n
<
0
)
{
n
=
-
n
;
*
(
c
++
)
=
'-'
;
}
exp
=
log10
(
n
);
if
((
exp
<
0
?
-
exp
:
exp
)
>
max_digit
)
{
/* exponent representation */
e
=
1
;
m
=
exp
;
if
(
m
<
0
)
{
m
-=
1
;
}
n
=
n
/
pow
(
10
.
0
,
m
);
m
=
0
;
}
else
{
/* un-exponent (normal) representation */
m
=
exp
;
if
(
m
<
0
)
{
m
=
0
;
}
}
/* puts digits */
while
(
max_digit
>=
0
)
{
mrb_float
weight
=
pow
(
10
.
0
,
m
);
digit
=
floor
(
n
/
weight
+
FLT_EPSILON
);
*
(
c
++
)
=
'0'
+
digit
;
n
-=
(
digit
*
weight
);
max_digit
--
;
if
(
m
--
==
0
)
{
*
(
c
++
)
=
'.'
;
}
else
if
(
m
<
-
1
&&
n
<
FLT_EPSILON
)
{
break
;
}
}
if
(
e
)
{
*
(
c
++
)
=
'e'
;
if
(
exp
>
0
)
{
*
(
c
++
)
=
'+'
;
}
else
{
*
(
c
++
)
=
'-'
;
exp
=
-
exp
;
}
if
(
exp
>=
100
)
{
mrb_raise
(
mrb
,
E_RANGE_ERROR
,
"Too large expornent."
);
}
*
(
c
++
)
=
'0'
+
exp
/
10
;
*
(
c
++
)
=
'0'
+
exp
%
10
;
}
*
c
=
'\0'
;
result
=
mrb_str_new
(
mrb
,
&
s
[
0
],
c
-
&
s
[
0
]);
}
return
result
;
}
/* 15.2.9.3.16(x) */
/*
* call-seq:
...
...
@@ -177,27 +270,15 @@ num_abs(mrb_state *mrb, mrb_value num)
static
mrb_value
flo_to_s
(
mrb_state
*
mrb
,
mrb_value
flt
)
{
char
buf
[
32
];
int
n
;
mrb_float
value
=
mrb_float
(
flt
);
if
(
isinf
(
value
))
{
static
const
char
s
[
2
][
5
]
=
{
"-inf"
,
"inf"
};
static
const
int
n
[]
=
{
4
,
3
};
int
idx
;
idx
=
(
value
<
0
)
?
0
:
1
;
return
mrb_str_new
(
mrb
,
s
[
idx
],
n
[
idx
]);
}
else
if
(
isnan
(
value
))
return
mrb_str_new
(
mrb
,
"NaN"
,
3
);
int
max_digit
;
#ifdef MRB_USE_FLOAT
n
=
sprintf
(
buf
,
"%.7g"
,
value
)
;
max_digit
=
7
;
#else
n
=
sprintf
(
buf
,
"%.14g"
,
value
)
;
max_digit
=
14
;
#endif
assert
(
n
>=
0
);
return
mrb_
str_new
(
mrb
,
buf
,
n
);
return
mrb_
flo_to_str
(
mrb
,
mrb_float
(
flt
),
max_digit
);
}
/* 15.2.9.3.2 */
...
...
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