fltfmt.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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
  11. * ANY REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
  12. * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
  13. */
  14. #include <u.h>
  15. #include <libc.h>
  16. #include <float.h>
  17. #include <ctype.h>
  18. #include "fmtdef.h"
  19. enum
  20. {
  21. FDIGIT = 30,
  22. FDEFLT = 6,
  23. NSIGNIF = 17
  24. };
  25. /*
  26. * first few powers of 10, enough for about 1/2 of the
  27. * total space for doubles.
  28. */
  29. static double pows10[] =
  30. {
  31. 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
  32. 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
  33. 1e20, 1e21, 1e22, 1e23, 1e24, 1e25, 1e26, 1e27, 1e28, 1e29,
  34. 1e30, 1e31, 1e32, 1e33, 1e34, 1e35, 1e36, 1e37, 1e38, 1e39,
  35. 1e40, 1e41, 1e42, 1e43, 1e44, 1e45, 1e46, 1e47, 1e48, 1e49,
  36. 1e50, 1e51, 1e52, 1e53, 1e54, 1e55, 1e56, 1e57, 1e58, 1e59,
  37. 1e60, 1e61, 1e62, 1e63, 1e64, 1e65, 1e66, 1e67, 1e68, 1e69,
  38. 1e70, 1e71, 1e72, 1e73, 1e74, 1e75, 1e76, 1e77, 1e78, 1e79,
  39. 1e80, 1e81, 1e82, 1e83, 1e84, 1e85, 1e86, 1e87, 1e88, 1e89,
  40. 1e90, 1e91, 1e92, 1e93, 1e94, 1e95, 1e96, 1e97, 1e98, 1e99,
  41. 1e100, 1e101, 1e102, 1e103, 1e104, 1e105, 1e106, 1e107, 1e108, 1e109,
  42. 1e110, 1e111, 1e112, 1e113, 1e114, 1e115, 1e116, 1e117, 1e118, 1e119,
  43. 1e120, 1e121, 1e122, 1e123, 1e124, 1e125, 1e126, 1e127, 1e128, 1e129,
  44. 1e130, 1e131, 1e132, 1e133, 1e134, 1e135, 1e136, 1e137, 1e138, 1e139,
  45. 1e140, 1e141, 1e142, 1e143, 1e144, 1e145, 1e146, 1e147, 1e148, 1e149,
  46. 1e150, 1e151, 1e152, 1e153, 1e154, 1e155, 1e156, 1e157, 1e158, 1e159,
  47. };
  48. #undef pow10
  49. #define pow10(x) fmtpow10(x)
  50. static double
  51. pow10(int n)
  52. {
  53. double d;
  54. int neg;
  55. neg = 0;
  56. if(n < 0){
  57. if(n < DBL_MIN_10_EXP){
  58. return 0.;
  59. }
  60. neg = 1;
  61. n = -n;
  62. }else if(n > DBL_MAX_10_EXP){
  63. return HUGE_VAL;
  64. }
  65. if(n < (int)(sizeof(pows10)/sizeof(pows10[0])))
  66. d = pows10[n];
  67. else{
  68. d = pows10[sizeof(pows10)/sizeof(pows10[0]) - 1];
  69. for(;;){
  70. n -= sizeof(pows10)/sizeof(pows10[0]) - 1;
  71. if(n < (int)(sizeof(pows10)/sizeof(pows10[0]))){
  72. d *= pows10[n];
  73. break;
  74. }
  75. d *= pows10[sizeof(pows10)/sizeof(pows10[0]) - 1];
  76. }
  77. }
  78. if(neg){
  79. return 1./d;
  80. }
  81. return d;
  82. }
  83. static int
  84. xadd(char *a, int n, int v)
  85. {
  86. char *b;
  87. int c;
  88. if(n < 0 || n >= NSIGNIF)
  89. return 0;
  90. for(b = a+n; b >= a; b--) {
  91. c = *b + v;
  92. if(c <= '9') {
  93. *b = c;
  94. return 0;
  95. }
  96. *b = '0';
  97. v = 1;
  98. }
  99. *a = '1'; /* overflow adding */
  100. return 1;
  101. }
  102. static int
  103. xsub(char *a, int n, int v)
  104. {
  105. char *b;
  106. int c;
  107. for(b = a+n; b >= a; b--) {
  108. c = *b - v;
  109. if(c >= '0') {
  110. *b = c;
  111. return 0;
  112. }
  113. *b = '9';
  114. v = 1;
  115. }
  116. *a = '9'; /* underflow subtracting */
  117. return 1;
  118. }
  119. static void
  120. xdtoa(Fmt *fmt, char *s2, double f)
  121. {
  122. char s1[NSIGNIF+10];
  123. double g, h;
  124. int e, d, i, n;
  125. int c1, c2, c3, c4, ucase, sign, chr, prec;
  126. prec = FDEFLT;
  127. if(fmt->flags & FmtPrec)
  128. prec = fmt->prec;
  129. if(prec > FDIGIT)
  130. prec = FDIGIT;
  131. if(__isNaN(f)) {
  132. strcpy(s2, "NaN");
  133. return;
  134. }
  135. if(__isInf(f, 1)) {
  136. strcpy(s2, "+Inf");
  137. return;
  138. }
  139. if(__isInf(f, -1)) {
  140. strcpy(s2, "-Inf");
  141. return;
  142. }
  143. sign = 0;
  144. if(f < 0) {
  145. f = -f;
  146. sign++;
  147. }
  148. ucase = 0;
  149. chr = fmt->r;
  150. if(isupper(chr)) {
  151. ucase = 1;
  152. chr = tolower(chr);
  153. }
  154. e = 0;
  155. g = f;
  156. if(g != 0) {
  157. frexp(f, &e);
  158. e = e * .301029995664;
  159. if(e >= -150 && e <= +150) {
  160. d = 0;
  161. h = f;
  162. } else {
  163. d = e/2;
  164. h = f * pow10(-d);
  165. }
  166. g = h * pow10(d-e);
  167. while(g < 1) {
  168. e--;
  169. g = h * pow10(d-e);
  170. }
  171. while(g >= 10) {
  172. e++;
  173. g = h * pow10(d-e);
  174. }
  175. }
  176. /*
  177. * convert NSIGNIF digits and convert
  178. * back to get accuracy.
  179. */
  180. for(i=0; i<NSIGNIF; i++) {
  181. d = g;
  182. s1[i] = d + '0';
  183. g = (g - d) * 10;
  184. }
  185. s1[i] = 0;
  186. /*
  187. * try decimal rounding to eliminate 9s
  188. */
  189. c2 = prec + 1;
  190. if(chr == 'f')
  191. c2 += e;
  192. if(c2 >= NSIGNIF-2) {
  193. strcpy(s2, s1);
  194. d = e;
  195. s1[NSIGNIF-2] = '0';
  196. s1[NSIGNIF-1] = '0';
  197. sprint(s1+NSIGNIF, "e%d", e-NSIGNIF+1);
  198. g = strtod(s1, nil);
  199. if(g == f)
  200. goto found;
  201. if(xadd(s1, NSIGNIF-3, 1)) {
  202. e++;
  203. sprint(s1+NSIGNIF, "e%d", e-NSIGNIF+1);
  204. }
  205. g = strtod(s1, nil);
  206. if(g == f)
  207. goto found;
  208. strcpy(s1, s2);
  209. e = d;
  210. }
  211. /*
  212. * convert back so s1 gets exact answer
  213. */
  214. for(;;) {
  215. sprint(s1+NSIGNIF, "e%d", e-NSIGNIF+1);
  216. g = strtod(s1, nil);
  217. if(f > g) {
  218. if(xadd(s1, NSIGNIF-1, 1))
  219. e--;
  220. continue;
  221. }
  222. if(f < g) {
  223. if(xsub(s1, NSIGNIF-1, 1))
  224. e++;
  225. continue;
  226. }
  227. break;
  228. }
  229. found:
  230. /*
  231. * sign
  232. */
  233. d = 0;
  234. i = 0;
  235. if(sign)
  236. s2[d++] = '-';
  237. else if(fmt->flags & FmtSign)
  238. s2[d++] = '+';
  239. else if(fmt->flags & FmtSpace)
  240. s2[d++] = ' ';
  241. /*
  242. * copy into final place
  243. * c1 digits of leading '0'
  244. * c2 digits from conversion
  245. * c3 digits of trailing '0'
  246. * c4 digits after '.'
  247. */
  248. c1 = 0;
  249. c2 = prec + 1;
  250. c3 = 0;
  251. c4 = prec;
  252. switch(chr) {
  253. default:
  254. if(xadd(s1, c2, 5))
  255. e++;
  256. break;
  257. case 'g':
  258. /*
  259. * decide on 'e' of 'f' style convers
  260. */
  261. if(xadd(s1, c2, 5))
  262. e++;
  263. if(e >= -5 && e <= prec) {
  264. c1 = -e - 1;
  265. c4 = prec - e;
  266. chr = 'h'; // flag for 'f' style
  267. }
  268. break;
  269. case 'f':
  270. if(xadd(s1, c2+e, 5))
  271. e++;
  272. c1 = -e;
  273. if(c1 > prec)
  274. c1 = c2;
  275. c2 += e;
  276. break;
  277. }
  278. /*
  279. * clean up c1 c2 and c3
  280. */
  281. if(c1 < 0)
  282. c1 = 0;
  283. if(c2 < 0)
  284. c2 = 0;
  285. if(c2 > NSIGNIF) {
  286. c3 = c2-NSIGNIF;
  287. c2 = NSIGNIF;
  288. }
  289. /*
  290. * copy digits
  291. */
  292. while(c1 > 0) {
  293. if(c1+c2+c3 == c4)
  294. s2[d++] = '.';
  295. s2[d++] = '0';
  296. c1--;
  297. }
  298. while(c2 > 0) {
  299. if(c2+c3 == c4)
  300. s2[d++] = '.';
  301. s2[d++] = s1[i++];
  302. c2--;
  303. }
  304. while(c3 > 0) {
  305. if(c3 == c4)
  306. s2[d++] = '.';
  307. s2[d++] = '0';
  308. c3--;
  309. }
  310. /*
  311. * strip trailing '0' on g conv
  312. */
  313. if(fmt->flags & FmtSharp) {
  314. if(0 == c4)
  315. s2[d++] = '.';
  316. } else
  317. if(chr == 'g' || chr == 'h') {
  318. for(n=d-1; n>=0; n--)
  319. if(s2[n] != '0')
  320. break;
  321. for(i=n; i>=0; i--)
  322. if(s2[i] == '.') {
  323. d = n;
  324. if(i != n)
  325. d++;
  326. break;
  327. }
  328. }
  329. if(chr == 'e' || chr == 'g') {
  330. if(ucase)
  331. s2[d++] = 'E';
  332. else
  333. s2[d++] = 'e';
  334. c1 = e;
  335. if(c1 < 0) {
  336. s2[d++] = '-';
  337. c1 = -c1;
  338. } else
  339. s2[d++] = '+';
  340. if(c1 >= 100) {
  341. s2[d++] = c1/100 + '0';
  342. c1 = c1%100;
  343. }
  344. s2[d++] = c1/10 + '0';
  345. s2[d++] = c1%10 + '0';
  346. }
  347. s2[d] = 0;
  348. }
  349. static int
  350. floatfmt(Fmt *fmt, double f)
  351. {
  352. char s[341]; /* precision+exponent+sign+'.'+null */
  353. xdtoa(fmt, s, f);
  354. fmt->flags &= FmtWidth|FmtLeft;
  355. __fmtcpy(fmt, s, strlen(s), strlen(s));
  356. return 0;
  357. }
  358. int
  359. __efgfmt(Fmt *f)
  360. {
  361. double d;
  362. d = va_arg(f->args, double);
  363. return floatfmt(f, d);
  364. }