fltfmt.c 5.0 KB

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