Snippets

Takehiko NOZAKI gdtoa usage (printf %g conversion like)

Updated by Takehiko NOZAKI

File gcvt.c Modified

  • Ignore whitespace
  • Hide word diff
  * SUCH DAMAGE.
  */
 
+#define _GNU_SOURCE /* for asprintf(3) */
 #include <assert.h>
 #include <stdio.h>
 #include <stdlib.h>
Updated by Takehiko NOZAKI

File gcvt.c Modified

  • Ignore whitespace
  • Hide word diff
 	}
 	/* assume DBL_MIN_EXP(-1021) DBL_MAX_EXP(1024) */
 	if (exp < 100) {
-		expstr[2] = to_char((exp % 100) / 10);
+		expstr[2] = to_char(exp / 10);
 		expstr[3] = to_char(exp % 10);
 		len = 4;
 	} else if (exp < 1000) {
-		expstr[2] = to_char((exp % 1000) / 100);
+		expstr[2] = to_char(exp / 100);
 		expstr[3] = to_char((exp % 100) / 10);
 		expstr[4] = to_char(exp % 10);
 		len = 5;
Updated by Takehiko NOZAKI

File gcvt.c Modified

  • Ignore whitespace
  • Hide word diff
 static inline int
 cvt_exp(int exp, FILE *fp)
 {
-	char buf[MAXEXPSIZ + 2], *expstr;
+	char expstr[MAXEXPSIZ + 2];
 	int sign;
 	size_t len;
 
+	expstr[0] = 'e';
 	if (exp >= 0) {
-		sign = '+';
+		expstr[1] = '+';
 	} else {
-		sign = '-';
+		expstr[1] = '-';
 		exp = -exp;
 	}
-	expstr = &buf[sizeof(buf)];
-	if (exp > 9) {
-		do {
-			*--expstr = to_char(exp % 10);
-			exp /= 10;
-		} while (exp > 9);
-		*--expstr = to_char(exp);
+	/* assume DBL_MIN_EXP(-1021) DBL_MAX_EXP(1024) */
+	if (exp < 100) {
+		expstr[2] = to_char((exp % 100) / 10);
+		expstr[3] = to_char(exp % 10);
+		len = 4;
+	} else if (exp < 1000) {
+		expstr[2] = to_char((exp % 1000) / 100);
+		expstr[3] = to_char((exp % 100) / 10);
+		expstr[4] = to_char(exp % 10);
+		len = 5;
 	} else {
-		*--expstr = to_char(exp);
-		*--expstr = to_char(0);
+		expstr[2] = to_char(exp / 1000);
+		expstr[3] = to_char((exp % 1000) / 100);
+		expstr[4] = to_char((exp % 100) / 10);
+		expstr[5] = to_char(exp % 10);
+		len = 6;
 	}
-	*--expstr = sign;
-	*--expstr = 'e';
-	len = &buf[sizeof(buf)] - expstr;
 	if (fwrite(expstr, 1, len, fp) != len)
 		return 1;
 	return 0;
 	d = (f > 0 || flags & SHARP) ? 1 : 0;
 	if (exp < 0)
 		exp = -exp;
-#if 0
-	e = 2;
-	if (exp > 9) {
-		do {
-			++e;
-			exp /= 9;
-		} while (exp > 9);
-		++e;
-	} else {
-		e += 2;
-	}
-#else
 	/* assume DBL_MIN_EXP(-1021) DBL_MAX_EXP(1024) */
 	if (exp < 100)
 		e = 4;
 		e = 5;
 	else
 		e = 6;
-#endif
 	return s + i + f + d + e;
 }
 
Updated by Takehiko NOZAKI

File gcvt.c Modified

  • Ignore whitespace
  • Hide word diff
 	if (f < prec)
 		f = prec;
 	d = (f > 0 || flags & SHARP) ? 1 : 0;
+	if (exp < 0)
+		exp = -exp;
 #if 0
 	e = 2;
 	if (exp > 9) {
Updated by Takehiko NOZAKI

File gcvt.c Modified

  • Ignore whitespace
  • Hide word diff
 	if (f < prec)
 		f = prec;
 	d = (f > 0 || flags & SHARP) ? 1 : 0;
+#if 0
 	e = 2;
 	if (exp > 9) {
 		do {
 	} else {
 		e += 2;
 	}
+#else
+	/* assume DBL_MIN_EXP(-1021) DBL_MAX_EXP(1024) */
+	if (exp < 100)
+		e = 4;
+	else if (exp < 1000)
+		e = 5;
+	else
+		e = 6;
+#endif
 	return s + i + f + d + e;
 }
 
  1. 1
  2. 2
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.