Feature #2152 ยป add_float_inspect.diff
| numeric.c (working copy) | ||
|---|---|---|
|
/*
|
||
|
* call-seq:
|
||
|
* flt.to_s -> string
|
||
|
* flt.inspect -> string
|
||
|
*
|
||
|
* Returns a string containing a representation of self. As well as a
|
||
|
* Returns a string containing a full 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>''.
|
||
|
*/
|
||
|
static VALUE
|
||
|
flo_to_s(VALUE flt)
|
||
|
flo_inspect(VALUE flt)
|
||
|
{
|
||
|
enum {decimal_mant = DBL_MANT_DIG-DBL_DIG};
|
||
|
enum {float_dig = DBL_DIG+1};
|
||
| ... | ... | |
|
}
|
||
|
/*
|
||
|
* call-seq:
|
||
|
* flt.to_s => string
|
||
|
*
|
||
|
* Returns a string containing a human readable 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>''.
|
||
|
*/
|
||
|
static VALUE
|
||
|
flo_to_s(VALUE flt)
|
||
|
{
|
||
|
char buf[32];
|
||
|
double value = RFLOAT_VALUE(flt);
|
||
|
char *p, *e;
|
||
|
if (isinf(value))
|
||
|
return rb_usascii_str_new2(value < 0 ? "-Infinity" : "Infinity");
|
||
|
else if(isnan(value))
|
||
|
return rb_usascii_str_new2("NaN");
|
||
|
snprintf(buf, sizeof(buf), "%#.15g", value); /* ensure to print decimal point */
|
||
|
if (!(e = strchr(buf, 'e'))) {
|
||
|
e = buf + strlen(buf);
|
||
|
}
|
||
|
if (!ISDIGIT(e[-1])) { /* reformat if ended with decimal point (ex 111111111111111.) */
|
||
|
snprintf(buf, sizeof(buf), "%#.14e", value);
|
||
|
if (!(e = strchr(buf, 'e'))) {
|
||
|
e = buf + strlen(buf);
|
||
|
}
|
||
|
}
|
||
|
p = e;
|
||
|
while (p[-1]=='0' && ISDIGIT(p[-2]))
|
||
|
p--;
|
||
|
memmove(p, e, strlen(e)+1);
|
||
|
return rb_usascii_str_new2(buf);
|
||
|
}
|
||
|
/*
|
||
|
* MISSING: documentation
|
||
|
*/
|
||
| ... | ... | |
|
rb_define_const(rb_cFloat, "NAN", DBL2NUM(NAN));
|
||
|
rb_define_method(rb_cFloat, "to_s", flo_to_s, 0);
|
||
|
rb_define_method(rb_cFloat, "inspect", flo_inspect, 0);
|
||
|
rb_define_method(rb_cFloat, "coerce", flo_coerce, 1);
|
||
|
rb_define_method(rb_cFloat, "-@", flo_uminus, 0);
|
||
|
rb_define_method(rb_cFloat, "+", flo_plus, 1);
|
||