aboutsummaryrefslogtreecommitdiff
path: root/src/tools/float.cpp
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2011-03-10 17:53:20 +0000
committerMike Buland <eichlan@xagasoft.com>2011-03-10 17:53:20 +0000
commit616ec97dec2f057a1072c29ea78df08f66fbbdd4 (patch)
tree3ce43dc91820e664db77394a6f4d1990cde42d79 /src/tools/float.cpp
parent8f53239f4ceccfd747283c8f0d7968fa40731faf (diff)
downloadlibbu++-616ec97dec2f057a1072c29ea78df08f66fbbdd4.tar.gz
libbu++-616ec97dec2f057a1072c29ea78df08f66fbbdd4.tar.bz2
libbu++-616ec97dec2f057a1072c29ea78df08f66fbbdd4.tar.xz
libbu++-616ec97dec2f057a1072c29ea78df08f66fbbdd4.zip
Removed the float test, it was silly to have it in libbu++.
Diffstat (limited to '')
-rw-r--r--src/tools/float.cpp130
1 files changed, 0 insertions, 130 deletions
diff --git a/src/tools/float.cpp b/src/tools/float.cpp
deleted file mode 100644
index 2c241f3..0000000
--- a/src/tools/float.cpp
+++ /dev/null
@@ -1,130 +0,0 @@
1#include <bu/sio.h>
2#include <bu/string.h>
3#include <stdlib.h>
4#include <math.h>
5
6using namespace Bu;
7
8Bu::String ftos( double dNum )
9{
10 if( dNum == 1.0 )
11 return "0x1p+0";
12 if( dNum == 0.0 )
13 return signbit(dNum)?"-0x0p+0":"0x0p+0";
14 if( dNum == -1.0 )
15 return "-0x1p+0";
16 Bu::String s("0x1.");
17 if( signbit(dNum) )
18 {
19 s.prepend("-");
20 dNum = -dNum;
21 }
22/*
23 for( int j = 0; j < 150 && dNum; j++ )
24 {
25 dNum = dNum*2;
26 sio << dNum << " -- " << (int)dNum << sio.nl;
27 if( (int)dNum == 0 )
28 s += '0';
29 else
30 {
31 s += '1';
32 dNum -= 1.0;
33 }
34 }
35 return s;
36 */
37 int iPower = 0;
38 if( dNum < 1.0 )
39 {
40 while( dNum < 1.0 )
41 {
42 dNum *= 2.0;
43 iPower--;
44 }
45 }
46 else if( dNum >= 2.0 )
47 {
48 while( dNum >= 2.0 )
49 {
50 dNum *= 0.5;
51 iPower++;
52 }
53 }
54 dNum -= (int)dNum;
55 for( int j = 0; j < 150 && dNum; j++ )
56 {
57 dNum = dNum*16.0;
58 s += "0123456789abcdef"[(int)dNum];
59 dNum -= (int)dNum;
60 }
61 s.formatAppend("p%+d", iPower );
62 return s;
63}
64
65double stof( const Bu::String &s )
66{
67 if( s == "0x1p+0" )
68 return 1.0;
69 if( s == "-0x1p+0" )
70 return -1.0;
71 if( s == "0x0p+0" )
72 return 0.0;
73 if( s == "-0x0p+0" )
74 return 0.0;
75 double dNum = 0.0;
76 int iPPos = s.getSize()-1;
77 for(; s[iPPos] != 'p'; iPPos--) { }
78 for( int i = iPPos-1; s[i] != '.'; i-- )
79 {
80 if( s[i]>='0' && s[i]<='9' )
81 {
82 dNum += s[i]-'0';
83 }
84 else if( s[i]>='a' && s[i]<='f' )
85 {
86 dNum += s[i]-'a'+10.0;
87 }
88 dNum *= 0.0625;
89 }
90
91 dNum += 1.0;
92
93 int iTop = strtol( s.getStr()+iPPos+1, NULL, 10 );
94 if( iTop < 0 )
95 {
96 for( int j = 0; j > iTop; j-- )
97 {
98 dNum *= 0.5;
99 }
100 }
101 else
102 {
103 for( int j = 0; j < iTop; j++ )
104 {
105 dNum *= 2.0;
106 }
107 }
108
109 if( s[0] == '-' )
110 return -dNum;
111 return dNum;
112}
113
114int main( int argc, char *argv[] )
115{
116 for( int j = 1; j < argc; j++ )
117 {
118 double dNum = strtod( argv[j], NULL );
119
120 String s = ftos( dNum );
121 sio << "in: " << dNum << sio.nl;
122 sio << "mine: " << s << sio.nl;
123 printf("printf: %a\n", dNum );
124 sio << "out: " << stof( s ) << sio.nl;
125 sio << "match: " << (stof( s ) == dNum) << sio.nl << sio.nl;
126 }
127
128 return 0;
129}
130