fltfmt.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <ctype.h>
  4. #include "fmtdef.h"
  5. enum
  6. {
  7. FDIGIT = 30,
  8. FDEFLT = 6,
  9. NSIGNIF = 17,
  10. NEXP10 = 308,
  11. };
  12. static int
  13. xadd(char *a, int n, int v)
  14. {
  15. char *b;
  16. int c;
  17. if(n < 0 || n >= NSIGNIF)
  18. return 0;
  19. for(b = a+n; b >= a; b--) {
  20. c = *b + v;
  21. if(c <= '9') {
  22. *b = c;
  23. return 0;
  24. }
  25. *b = '0';
  26. v = 1;
  27. }
  28. *a = '1'; // overflow adding
  29. return 1;
  30. }
  31. static int
  32. xsub(char *a, int n, int v)
  33. {
  34. char *b;
  35. int c;
  36. for(b = a+n; b >= a; b--) {
  37. c = *b - v;
  38. if(c >= '0') {
  39. *b = c;
  40. return 0;
  41. }
  42. *b = '9';
  43. v = 1;
  44. }
  45. *a = '9'; // underflow subtracting
  46. return 1;
  47. }
  48. static void
  49. xdtoa(Fmt *fmt, char *s2, double f)
  50. {
  51. char s1[NSIGNIF+10];
  52. double g, h;
  53. int e, d, i, n;
  54. int c1, c2, c3, c4, ucase, sign, chr, prec;
  55. prec = FDEFLT;
  56. if(fmt->flags & FmtPrec)
  57. prec = fmt->prec;
  58. if(prec > FDIGIT)
  59. prec = FDIGIT;
  60. if(isNaN(f)) {
  61. strcpy(s2, "NaN");
  62. return;
  63. }
  64. if(isInf(f, 1)) {
  65. strcpy(s2, "+Inf");
  66. return;
  67. }
  68. if(isInf(f, -1)) {
  69. strcpy(s2, "-Inf");
  70. return;
  71. }
  72. sign = 0;
  73. if(f < 0) {
  74. f = -f;
  75. sign++;
  76. }
  77. ucase = 0;
  78. chr = fmt->r;
  79. if(isupper(chr)) {
  80. ucase = 1;
  81. chr = tolower(chr);
  82. }
  83. e = 0;
  84. g = f;
  85. if(g != 0) {
  86. frexp(f, &e);
  87. e = e * .301029995664;
  88. if(e >= -150 && e <= +150) {
  89. d = 0;
  90. h = f;
  91. } else {
  92. d = e/2;
  93. h = f * pow10(-d);
  94. }
  95. g = h * pow10(d-e);
  96. while(g < 1) {
  97. e--;
  98. g = h * pow10(d-e);
  99. }
  100. while(g >= 10) {
  101. e++;
  102. g = h * pow10(d-e);
  103. }
  104. }
  105. /*
  106. * convert NSIGNIF digits and convert
  107. * back to get accuracy.
  108. */
  109. for(i=0; i<NSIGNIF; i++) {
  110. d = g;
  111. s1[i] = d + '0';
  112. g = (g - d) * 10;
  113. }
  114. s1[i] = 0;
  115. /*
  116. * try decimal rounding to eliminate 9s
  117. */
  118. c2 = prec + 1;
  119. if(chr == 'f')
  120. c2 += e;
  121. if(c2 >= NSIGNIF-2) {
  122. strcpy(s2, s1);
  123. d = e;
  124. s1[NSIGNIF-2] = '0';
  125. s1[NSIGNIF-1] = '0';
  126. sprint(s1+NSIGNIF, "e%d", e-NSIGNIF+1);
  127. g = strtod(s1, nil);
  128. if(g == f)
  129. goto found;
  130. if(xadd(s1, NSIGNIF-3, 1)) {
  131. e++;
  132. sprint(s1+NSIGNIF, "e%d", e-NSIGNIF+1);
  133. }
  134. g = strtod(s1, nil);
  135. if(g == f)
  136. goto found;
  137. strcpy(s1, s2);
  138. e = d;
  139. }
  140. /*
  141. * convert back so s1 gets exact answer
  142. */
  143. for(;;) {
  144. sprint(s1+NSIGNIF, "e%d", e-NSIGNIF+1);
  145. g = strtod(s1, nil);
  146. if(f > g) {
  147. if(xadd(s1, NSIGNIF-1, 1))
  148. e--;
  149. continue;
  150. }
  151. if(f < g) {
  152. if(xsub(s1, NSIGNIF-1, 1))
  153. e++;
  154. continue;
  155. }
  156. break;
  157. }
  158. found:
  159. /*
  160. * sign
  161. */
  162. d = 0;
  163. i = 0;
  164. if(sign)
  165. s2[d++] = '-';
  166. else if(fmt->flags & FmtSign)
  167. s2[d++] = '+';
  168. else if(fmt->flags & FmtSpace)
  169. s2[d++] = ' ';
  170. /*
  171. * copy into final place
  172. * c1 digits of leading '0'
  173. * c2 digits from conversion
  174. * c3 digits of trailing '0'
  175. * c4 digits after '.'
  176. */
  177. c1 = 0;
  178. c2 = prec + 1;
  179. c3 = 0;
  180. c4 = prec;
  181. switch(chr) {
  182. default:
  183. if(xadd(s1, c2, 5))
  184. e++;
  185. break;
  186. case 'g':
  187. /*
  188. * decide on 'e' of 'f' style convers
  189. */
  190. if(xadd(s1, c2, 5))
  191. e++;
  192. if(e >= -5 && e <= prec) {
  193. c1 = -e - 1;
  194. c4 = prec - e;
  195. chr = 'h'; // flag for 'f' style
  196. }
  197. break;
  198. case 'f':
  199. if(xadd(s1, c2+e, 5))
  200. e++;
  201. c1 = -e;
  202. if(c1 > prec)
  203. c1 = c2;
  204. c2 += e;
  205. break;
  206. }
  207. /*
  208. * clean up c1 c2 and c3
  209. */
  210. if(c1 < 0)
  211. c1 = 0;
  212. if(c2 < 0)
  213. c2 = 0;
  214. if(c2 > NSIGNIF) {
  215. c3 = c2-NSIGNIF;
  216. c2 = NSIGNIF;
  217. }
  218. /*
  219. * copy digits
  220. */
  221. while(c1 > 0) {
  222. if(c1+c2+c3 == c4)
  223. s2[d++] = '.';
  224. s2[d++] = '0';
  225. c1--;
  226. }
  227. while(c2 > 0) {
  228. if(c2+c3 == c4)
  229. s2[d++] = '.';
  230. s2[d++] = s1[i++];
  231. c2--;
  232. }
  233. while(c3 > 0) {
  234. if(c3 == c4)
  235. s2[d++] = '.';
  236. s2[d++] = '0';
  237. c3--;
  238. }
  239. /*
  240. * strip trailing '0' on g conv
  241. */
  242. if(fmt->flags & FmtSharp) {
  243. if(0 == c4)
  244. s2[d++] = '.';
  245. } else
  246. if(chr == 'g' || chr == 'h') {
  247. for(n=d-1; n>=0; n--)
  248. if(s2[n] != '0')
  249. break;
  250. for(i=n; i>=0; i--)
  251. if(s2[i] == '.') {
  252. d = n;
  253. if(i != n)
  254. d++;
  255. break;
  256. }
  257. }
  258. if(chr == 'e' || chr == 'g') {
  259. if(ucase)
  260. s2[d++] = 'E';
  261. else
  262. s2[d++] = 'e';
  263. c1 = e;
  264. if(c1 < 0) {
  265. s2[d++] = '-';
  266. c1 = -c1;
  267. } else
  268. s2[d++] = '+';
  269. if(c1 >= 100) {
  270. s2[d++] = c1/100 + '0';
  271. c1 = c1%100;
  272. }
  273. s2[d++] = c1/10 + '0';
  274. s2[d++] = c1%10 + '0';
  275. }
  276. s2[d] = 0;
  277. }
  278. int
  279. _floatfmt(Fmt *fmt, double f)
  280. {
  281. char s[1+NEXP10+1+FDIGIT+1];
  282. /*
  283. * The max length of a %f string is
  284. * '[+-]'+"max exponent"+'.'+"max precision"+'\0'
  285. * which is 341 currently.
  286. */
  287. xdtoa(fmt, s, f);
  288. fmt->flags &= FmtWidth|FmtLeft;
  289. _fmtcpy(fmt, s, strlen(s), strlen(s));
  290. return 0;
  291. }
  292. int
  293. _efgfmt(Fmt *f)
  294. {
  295. double d;
  296. d = va_arg(f->args, double);
  297. return _floatfmt(f, d);
  298. }