Created
August 25, 2009 15:14
-
-
Save methodmissing/174755 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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