Skip to content

Instantly share code, notes, and snippets.

@methodmissing
Created August 25, 2009 15:14
Show Gist options
  • Select an option

  • Save methodmissing/174755 to your computer and use it in GitHub Desktop.

Select an option

Save methodmissing/174755 to your computer and use it in GitHub Desktop.
static VALUE
flo_to_s(flt)
VALUE flt;
{
char buf[32];
double value = RFLOAT(flt)->value;
char *p, *e;
if (isinf(value))
return rb_str_new2(value < 0 ? "-Infinity" : "Infinity");
else if(isnan(value))
return rb_str_new2("NaN");
sprintf(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.) */
sprintf(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_str_new2(buf);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment