diff options
author | Mike Buland <eichlan@xagasoft.com> | 2011-03-21 22:03:55 +0000 |
---|---|---|
committer | Mike Buland <eichlan@xagasoft.com> | 2011-03-21 22:03:55 +0000 |
commit | 9d7ee5a5b9b6ca2093043b7c584df02913739b02 (patch) | |
tree | dec9d86baa65cc4cabdc509d590b5689a1ddd9d1 /src | |
parent | 7d605dda5a653c4c40be6de10853d6945457324a (diff) | |
download | libbu++-9d7ee5a5b9b6ca2093043b7c584df02913739b02.tar.gz libbu++-9d7ee5a5b9b6ca2093043b7c584df02913739b02.tar.bz2 libbu++-9d7ee5a5b9b6ca2093043b7c584df02913739b02.tar.xz libbu++-9d7ee5a5b9b6ca2093043b7c584df02913739b02.zip |
floats can be output through the formatter again, except sometimes they're
really ugly.
Diffstat (limited to '')
-rw-r--r-- | src/formatter.cpp | 45 | ||||
-rw-r--r-- | src/formatter.h | 54 |
2 files changed, 97 insertions, 2 deletions
diff --git a/src/formatter.cpp b/src/formatter.cpp index dcaa3a5..17fb311 100644 --- a/src/formatter.cpp +++ b/src/formatter.cpp | |||
@@ -10,6 +10,51 @@ | |||
10 | #include "bu/stream.h" | 10 | #include "bu/stream.h" |
11 | #include <string.h> | 11 | #include <string.h> |
12 | 12 | ||
13 | template<> float Bu::tlog( float x ) | ||
14 | { | ||
15 | return logf( x ); | ||
16 | } | ||
17 | |||
18 | template<> double Bu::tlog( double x ) | ||
19 | { | ||
20 | return log( x ); | ||
21 | } | ||
22 | |||
23 | template<> long double Bu::tlog( long double x ) | ||
24 | { | ||
25 | return logl( x ); | ||
26 | } | ||
27 | |||
28 | template<> float Bu::tfloor( float x ) | ||
29 | { | ||
30 | return floorf( x ); | ||
31 | } | ||
32 | |||
33 | template<> double Bu::tfloor( double x ) | ||
34 | { | ||
35 | return floor( x ); | ||
36 | } | ||
37 | |||
38 | template<> long double Bu::tfloor( long double x ) | ||
39 | { | ||
40 | return floorl( x ); | ||
41 | } | ||
42 | |||
43 | template<> float Bu::tpow( float x, float y ) | ||
44 | { | ||
45 | return powf( x, y ); | ||
46 | } | ||
47 | |||
48 | template<> double Bu::tpow( double x, double y ) | ||
49 | { | ||
50 | return pow( x, y ); | ||
51 | } | ||
52 | |||
53 | template<> long double Bu::tpow( long double x, long double y ) | ||
54 | { | ||
55 | return powl( x, y ); | ||
56 | } | ||
57 | |||
13 | Bu::Formatter::Formatter( Stream &rStream ) : | 58 | Bu::Formatter::Formatter( Stream &rStream ) : |
14 | rStream( rStream ), | 59 | rStream( rStream ), |
15 | bTempFmt( false ), | 60 | bTempFmt( false ), |
diff --git a/src/formatter.h b/src/formatter.h index f87a9be..a7689e1 100644 --- a/src/formatter.h +++ b/src/formatter.h | |||
@@ -11,10 +11,27 @@ | |||
11 | #include "bu/string.h" | 11 | #include "bu/string.h" |
12 | #include "bu/fmt.h" | 12 | #include "bu/fmt.h" |
13 | 13 | ||
14 | #include <math.h> | ||
15 | |||
14 | namespace Bu | 16 | namespace Bu |
15 | { | 17 | { |
16 | class Stream; | 18 | class Stream; |
17 | 19 | ||
20 | template<typename t> t tlog( t x ); | ||
21 | template<> float tlog( float x ); | ||
22 | template<> double tlog( double x ); | ||
23 | template<> long double tlog( long double x ); | ||
24 | |||
25 | template<typename t> t tfloor( t x ); | ||
26 | template<> float tfloor( float x ); | ||
27 | template<> double tfloor( double x ); | ||
28 | template<> long double tfloor( long double x ); | ||
29 | |||
30 | template<typename t> t tpow( t x, t y ); | ||
31 | template<> float tpow( float x, float y ); | ||
32 | template<> double tpow( double x, double y ); | ||
33 | template<> long double tpow( long double x, long double y ); | ||
34 | |||
18 | class Formatter | 35 | class Formatter |
19 | { | 36 | { |
20 | public: | 37 | public: |
@@ -118,8 +135,41 @@ namespace Bu | |||
118 | void ffmt( type f ) | 135 | void ffmt( type f ) |
119 | { | 136 | { |
120 | Bu::String fTmp; | 137 | Bu::String fTmp; |
121 | // fTmp.format("%f", f ); | 138 | bool bNeg = false; |
122 | // writeAligned("**make floats work**"); | 139 | char cBase = fLast.bCaps?'A':'a'; |
140 | if( fLast.uRadix < 2 || fLast.uRadix > 36 ) | ||
141 | { | ||
142 | usedFormat(); | ||
143 | return; | ||
144 | } | ||
145 | |||
146 | if( signbit(f) ) | ||
147 | { | ||
148 | bNeg = true; | ||
149 | f = -f; | ||
150 | } | ||
151 | int iScale = tfloor(tlog( f ) / tlog( (type)fLast.uRadix )); | ||
152 | f /= tpow( (type)fLast.uRadix, (type)iScale ); | ||
153 | |||
154 | if( iScale < 0 ) | ||
155 | { | ||
156 | fTmp += "0."; | ||
157 | for( int j = 1; j < -iScale; j++ ) | ||
158 | fTmp += '0'; | ||
159 | } | ||
160 | int c = f; | ||
161 | fTmp += (char)((c<10)?('0'+c):(cBase+c-10)); | ||
162 | f -= (int)f; | ||
163 | for( int j = 0; j < 150 && f; j++ ) | ||
164 | { | ||
165 | if( iScale - j == 0 ) | ||
166 | fTmp += '.'; | ||
167 | f = f*fLast.uRadix; | ||
168 | int c = f; | ||
169 | fTmp += (char)((c<10)?('0'+c):(cBase+c-10)); | ||
170 | f -= (int)f; | ||
171 | } | ||
172 | |||
123 | writeAligned( fTmp ); | 173 | writeAligned( fTmp ); |
124 | usedFormat(); | 174 | usedFormat(); |
125 | } | 175 | } |