add_float_inspect.diff

Roger Pack, 03/26/2010 05:25 AM

Download (2.17 KB)

View differences:

numeric.c (working copy)
547 547

  
548 548
/*
549 549
 *  call-seq:
550
 *     flt.to_s  ->  string
550
 *     flt.inspect  ->  string
551 551
 *
552
 *  Returns a string containing a representation of self. As well as a
552
 *  Returns a string containing a full representation of self. As well as a
553 553
 *  fixed or exponential form of the number, the call may return
554 554
 *  ``<code>NaN</code>'', ``<code>Infinity</code>'', and
555 555
 *  ``<code>-Infinity</code>''.
556 556
 */
557 557

  
558 558
static VALUE
559
flo_to_s(VALUE flt)
559
flo_inspect(VALUE flt)
560 560
{
561 561
    enum {decimal_mant = DBL_MANT_DIG-DBL_DIG};
562 562
    enum {float_dig = DBL_DIG+1};
......
590 590
}
591 591

  
592 592
/*
593
 *  call-seq:
594
 *     flt.to_s    => string
595
 *
596
 *  Returns a string containing a human readable representation of self. As well as a
597
 *  fixed or exponential form of the number, the call may return
598
 *  ``<code>NaN</code>'', ``<code>Infinity</code>'', and
599
 *  ``<code>-Infinity</code>''.
600
 */
601

  
602
static VALUE
603
flo_to_s(VALUE flt)
604
{
605
    char buf[32];
606
    double value = RFLOAT_VALUE(flt);
607
    char *p, *e;
608

  
609
    if (isinf(value))
610
	return rb_usascii_str_new2(value < 0 ? "-Infinity" : "Infinity");
611
    else if(isnan(value))
612
	return rb_usascii_str_new2("NaN");
613

  
614
    snprintf(buf, sizeof(buf), "%#.15g", value); /* ensure to print decimal point */
615
    if (!(e = strchr(buf, 'e'))) {
616
	e = buf + strlen(buf);
617
    }
618
    if (!ISDIGIT(e[-1])) { /* reformat if ended with decimal point (ex 111111111111111.) */
619
	snprintf(buf, sizeof(buf), "%#.14e", value);
620
	if (!(e = strchr(buf, 'e'))) {
621
	    e = buf + strlen(buf);
622
	}
623
    }
624
    p = e;
625
    while (p[-1]=='0' && ISDIGIT(p[-2]))
626
	p--;
627
    memmove(p, e, strlen(e)+1);
628
    return rb_usascii_str_new2(buf);
629
}
630

  
631

  
632
/*
593 633
 * MISSING: documentation
594 634
 */
595 635

  
......
3339 3379
    rb_define_const(rb_cFloat, "NAN", DBL2NUM(NAN));
3340 3380

  
3341 3381
    rb_define_method(rb_cFloat, "to_s", flo_to_s, 0);
3382
    rb_define_method(rb_cFloat, "inspect", flo_inspect, 0);
3342 3383
    rb_define_method(rb_cFloat, "coerce", flo_coerce, 1);
3343 3384
    rb_define_method(rb_cFloat, "-@", flo_uminus, 0);
3344 3385
    rb_define_method(rb_cFloat, "+", flo_plus, 1);