summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorYukihiro "Matz" Matsumoto <[email protected]>2014-02-26 00:06:58 +0900
committerYukihiro "Matz" Matsumoto <[email protected]>2014-02-26 00:06:58 +0900
commitd747ea72c483446cb6d689cb888cf2488eb2a837 (patch)
tree847f49d9bd4f59c629480c7a58e91d63bc21223e
parent05d3b9de542f8b4335fb042a20f546d82be36022 (diff)
parent61577e8d6b69efe8da23070abab0dfc7a00f5ee3 (diff)
downloadmruby-d747ea72c483446cb6d689cb888cf2488eb2a837.tar.gz
mruby-d747ea72c483446cb6d689cb888cf2488eb2a837.zip
resolve conflict
-rw-r--r--include/mruby/numeric.h1
-rw-r--r--include/mruby/value.h1
-rw-r--r--src/numeric.c30
3 files changed, 18 insertions, 14 deletions
diff --git a/include/mruby/numeric.h b/include/mruby/numeric.h
index d866d36d1..d0305160c 100644
--- a/include/mruby/numeric.h
+++ b/include/mruby/numeric.h
@@ -16,7 +16,6 @@ extern "C" {
#define FIXABLE(f) (POSFIXABLE(f) && NEGFIXABLE(f))
mrb_value mrb_flo_to_fixnum(mrb_state *mrb, mrb_value val);
-mrb_value mrb_flo_to_str(mrb_state *mrb, mrb_value flo);
mrb_value mrb_fixnum_to_str(mrb_state *mrb, mrb_value x, int base);
diff --git a/include/mruby/value.h b/include/mruby/value.h
index 13acd039e..a0f0c2ef9 100644
--- a/include/mruby/value.h
+++ b/include/mruby/value.h
@@ -59,6 +59,7 @@ typedef short mrb_sym;
# include <float.h>
# define isnan _isnan
# define isinf(n) (!_finite(n) && !_isnan(n))
+# define signbit(n) (_copysign(1.0, (n)) < 0.0)
# define strtoll _strtoi64
# define strtof (float)strtod
# define PRId32 "I32d"
diff --git a/src/numeric.c b/src/numeric.c
index e081cf80a..5f23b2461 100644
--- a/src/numeric.c
+++ b/src/numeric.c
@@ -108,18 +108,12 @@ num_div(mrb_state *mrb, mrb_value x)
* representation.
*/
-mrb_value
-mrb_flo_to_str(mrb_state *mrb, mrb_value flo)
+static mrb_value
+mrb_flo_to_str(mrb_state *mrb, mrb_float flo)
{
- double n;
+ double n = (double)flo;
int max_digits = FLO_MAX_DIGITS;
- if (!mrb_float_p(flo)) {
- mrb_raise(mrb, E_TYPE_ERROR, "non float value");
- }
-
- n = (double)mrb_float(flo);
-
if (isnan(n)) {
return mrb_str_new_lit(mrb, "NaN");
}
@@ -140,12 +134,22 @@ mrb_flo_to_str(mrb_state *mrb, mrb_value flo)
char *c = &s[0];
int length = 0;
- if (n < 0) {
+ if (signbit(n)) {
n = -n;
*(c++) = '-';
}
- exp = (n > 1) ? floor(log10(n)) : -ceil(-log10(n));
+ if (n != 0.0) {
+ if (n > 1.0) {
+ exp = (int)floor(log10(n));
+ }
+ else {
+ exp = (int)-ceil(-log10(n));
+ }
+ }
+ else {
+ exp = 0;
+ }
/* preserve significands */
if (exp < 0) {
@@ -188,7 +192,7 @@ mrb_flo_to_str(mrb_state *mrb, mrb_value flo)
while (max_digits >= 0) {
double weight = pow(10.0, m);
double fdigit = n / weight;
-
+
if (fdigit < 0) fdigit = n = 0;
if (m < -1 && fdigit < FLO_EPSILON) {
if (e || exp > 0 || m <= -abs(exp)) {
@@ -254,7 +258,7 @@ mrb_flo_to_str(mrb_state *mrb, mrb_value flo)
static mrb_value
flo_to_s(mrb_state *mrb, mrb_value flt)
{
- return mrb_flo_to_str(mrb, flt);
+ return mrb_flo_to_str(mrb, mrb_float(flt));
}
/* 15.2.9.3.2 */