mac: Let IPhotoDataProvider::GetAlbumNames() return albums in a deterministic order.
[chromium-blink-merge.git] / base / third_party / dmg_fp / g_fmt.cc
blobbfa358d154c7d5cb2bead52329fc22af38687280
1 /****************************************************************
3 * The author of this software is David M. Gay.
5 * Copyright (c) 1991, 1996 by Lucent Technologies.
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose without fee is hereby granted, provided that this entire notice
9 * is included in all copies of any software which is or includes a copy
10 * or modification of this software and in all copies of the supporting
11 * documentation for such software.
13 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
14 * WARRANTY. IN PARTICULAR, NEITHER THE AUTHOR NOR LUCENT MAKES ANY
15 * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
16 * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
18 ***************************************************************/
20 /* g_fmt(buf,x) stores the closest decimal approximation to x in buf;
21 * it suffices to declare buf
22 * char buf[32];
25 #include "dmg_fp.h"
27 namespace dmg_fp {
29 char *
30 g_fmt(register char *b, double x)
32 register int i, k;
33 register char *s;
34 int decpt, j, sign;
35 char *b0, *s0, *se;
37 b0 = b;
38 #ifdef IGNORE_ZERO_SIGN
39 if (!x) {
40 *b++ = '0';
41 *b = 0;
42 goto done;
44 #endif
45 s = s0 = dtoa(x, 0, 0, &decpt, &sign, &se);
46 if (sign)
47 *b++ = '-';
48 if (decpt == 9999) /* Infinity or Nan */ {
49 for(*b = *s++; *b++; *b = *s++) {}
50 goto done0;
52 if (decpt <= -4 || decpt > se - s + 5) {
53 *b++ = *s++;
54 if (*s) {
55 *b++ = '.';
56 for(*b = *s++; *b; *b = *s++)
57 b++;
59 *b++ = 'e';
60 /* sprintf(b, "%+.2d", decpt - 1); */
61 if (--decpt < 0) {
62 *b++ = '-';
63 decpt = -decpt;
65 else
66 *b++ = '+';
67 for(j = 2, k = 10; 10*k <= decpt; j++, k *= 10) {}
68 for(;;) {
69 i = decpt / k;
70 *b++ = (char)i + '0';
71 if (--j <= 0)
72 break;
73 decpt -= i*k;
74 decpt *= 10;
76 *b = 0;
78 else if (decpt <= 0) {
79 *b++ = '.';
80 for(; decpt < 0; decpt++)
81 *b++ = '0';
82 for(*b = *s++; *b++; *b = *s++) {}
84 else {
85 for(*b = *s++; *b; *b = *s++) {
86 b++;
87 if (--decpt == 0 && *s)
88 *b++ = '.';
90 for(; decpt > 0; decpt--)
91 *b++ = '0';
92 *b = 0;
94 done0:
95 freedtoa(s0);
96 #ifdef IGNORE_ZERO_SIGN
97 done:
98 #endif
99 return b0;
102 } // namespace dmg_fp