summaryrefslogtreecommitdiff
path: root/src/formatter.h
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2011-03-21 22:03:55 +0000
committerMike Buland <eichlan@xagasoft.com>2011-03-21 22:03:55 +0000
commit9d7ee5a5b9b6ca2093043b7c584df02913739b02 (patch)
treedec9d86baa65cc4cabdc509d590b5689a1ddd9d1 /src/formatter.h
parent7d605dda5a653c4c40be6de10853d6945457324a (diff)
downloadlibbu++-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 'src/formatter.h')
-rw-r--r--src/formatter.h54
1 files changed, 52 insertions, 2 deletions
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
14namespace Bu 16namespace 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 }