Snippets

Takehiko NOZAKI gdtoa usage (printf %a conversion like)

Updated by Takehiko NOZAKI

File acvt.c Modified

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

File acvt.c Modified

  • Ignore whitespace
  • Hide word diff
 	}
 	/* assume DBL_MIN_10_EXP(-307) DBL_MAX_10_EXP(308) */
 	if (exp < 10) {
-		expstr[2] = to_char(exp % 10);
+		expstr[2] = to_char(exp);
 		len = 3;
 	} else 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 {
Updated by Takehiko NOZAKI

File acvt.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] = 'p';
 	if (exp >= 0) {
-		sign = '+';
+		expstr[1] = '+';
 	} else {
-		sign = '-';
+		expstr[1] = '-';
 		exp = -exp;
 	}
-	expstr = &buf[sizeof(buf)];
-	while (exp > 9) {
-		*--expstr = to_char(exp % 10);
-		exp /= 10;
+	/* assume DBL_MIN_10_EXP(-307) DBL_MAX_10_EXP(308) */
+	if (exp < 10) {
+		expstr[2] = to_char(exp % 10);
+		len = 3;
+	} else if (exp < 100) {
+		expstr[2] = to_char((exp % 100) / 10);
+		expstr[3] = to_char(exp % 10);
+		len = 4;
+	} else {
+		expstr[2] = to_char(exp / 100);
+		expstr[3] = to_char((exp % 100) / 10);
+		expstr[4] = to_char(exp % 10);
+		len = 5;
 	}
-	*--expstr = to_char(exp);
-	*--expstr = sign;
-	*--expstr = 'p';
-	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
-	p = 2;
-	while (exp > 9) {
-		++p;
-		exp /= 9;
-	}
-	++p;
-#else
 	/* assume DBL_MIN_10_EXP(-307) DBL_MAX_10_EXP(308) */
 	if (exp < 10)
 		p = 3;
 		p = 4;
 	else
 		p = 5;
-#endif
 	return x + s + i + f + d + p;
 }
 
Updated by Takehiko NOZAKI

File acvt.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
 	p = 2;
 	while (exp > 9) {
Updated by Takehiko NOZAKI

File acvt.c Modified

  • Ignore whitespace
  • Hide word diff
 	if (f < prec)
 		f = prec;
 	d = (f > 0 || flags & SHARP) ? 1 : 0;
+#if 0
 	p = 2;
 	while (exp > 9) {
 		++p;
 		exp /= 9;
 	}
 	++p;
+#else
+	/* assume DBL_MIN_10_EXP(-307) DBL_MAX_10_EXP(308) */
+	if (exp < 10)
+		p = 3;
+	else if (exp < 100)
+		p = 4;
+	else
+		p = 5;
+#endif
 	return x + s + i + f + d + p;
 }
 
  1. 1
  2. 2
HTTPS SSH

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