ipe-7.0.11: new version of prec patch
[shuber-gentoo-overlay.git] / media-gfx / ipe / files / ipe-outputprecision.patch
1 diff --git a/src/ipelib/ipebase.cpp b/src/ipelib/ipebase.cpp
2 index 63de035..19226ad 100644
3 --- a/src/ipelib/ipebase.cpp
4 +++ b/src/ipelib/ipebase.cpp
5 @@ -557,55 +557,50 @@ Stream &Stream::operator<<(int i)
6 //! Output double.
7 Stream &Stream::operator<<(double d)
8 {
9 - char buf[30];
10 - if (d < 0.0) {
11 - putChar('-');
12 - d = -d;
13 + // Maximum number of digits in total
14 + const unsigned maxdigits=14;
15 +
16 + const unsigned bufsize=511;
17 + char buf[bufsize+1];
18 +
19 + // The number of digits left of '.'
20 + int intpartsize = 0;
21 + if( fabs(d)>=1.0 )
22 + {
23 + snprintf(buf, bufsize, "%.0f", fabs(d));
24 + intpartsize = strlen(buf);
25 }
26 - if (d >= 1e9) {
27 - // PDF will not be able to read this, but we have to write something.
28 - // Such large numbers should only happen if something is wrong.
29 - std::sprintf(buf, "%g", d);
30 - putCString(buf);
31 - } else if (d < 1e-8) {
32 - putChar('0');
33 - } else {
34 - // Print six significant digits, but omit trailing zeros.
35 - // Probably I'll want to have adjustable precision later.
36 - int factor;
37 - if (d > 1000.0)
38 - factor = 100L;
39 - else if (d > 100.0)
40 - factor = 1000L;
41 - else if (d > 10.0)
42 - factor = 10000L;
43 - else if (d > 1.0)
44 - factor = 100000L;
45 - else if (d > 0.1)
46 - factor = 1000000L;
47 - else if (d > 0.01)
48 - factor = 10000000L;
49 - else
50 - factor = 100000000L;
51 - double dd = trunc(d);
52 - int intpart = int(dd + 0.5);
53 - // 10^9 < 2^31
54 - int v = int(factor * (d - dd) + 0.5);
55 - if (v >= factor) {
56 - ++intpart;
57 - v -= factor;
58 - }
59 - std::sprintf(buf, "%d", intpart);
60 - putCString(buf);
61 - int mask = factor / 10;
62 - if (v != 0) {
63 - putChar('.');
64 - while (v != 0) {
65 - putChar('0' + v / mask);
66 - v = (10 * v) % factor;
67 - }
68 +
69 + // The nubmer of digits right of '.'
70 + int respartsize = maxdigits-intpartsize;
71 + if( respartsize < 0 )
72 + respartsize = 0;
73 +
74 + // Building the format string and get the string conversion of d
75 + char fmt[256];
76 + snprintf(fmt, 255, "%%%d.%df", intpartsize, respartsize);
77 + snprintf(buf, bufsize, fmt, d);
78 +
79 +
80 + // Truncate redundant zeros and '.' from the right
81 + for(int i = strlen(buf)-1; i>=intpartsize; i--)
82 + {
83 + // Truncate all zeros from right-hand
84 + if( buf[i] == '0' )
85 + {
86 + buf[i] = '\0';
87 + continue;
88 }
89 +
90 + // Remove decimal dot without decimal digits
91 + if( buf[i] == '.' )
92 + buf[i] = '\0';
93 +
94 + break;
95 }
96 +
97 +
98 + putCString(buf);
99 return *this;
100 }
101