diff options
Diffstat (limited to 'c++-qt/src/float.cpp')
-rw-r--r-- | c++-qt/src/float.cpp | 122 |
1 files changed, 122 insertions, 0 deletions
diff --git a/c++-qt/src/float.cpp b/c++-qt/src/float.cpp new file mode 100644 index 0000000..a82edec --- /dev/null +++ b/c++-qt/src/float.cpp | |||
@@ -0,0 +1,122 @@ | |||
1 | #include "gats-qt/float.h" | ||
2 | #include "gats-qt/integer.h" | ||
3 | |||
4 | #include <math.h> | ||
5 | |||
6 | Gats::Float::Float() : | ||
7 | fVal( 0.0 ) | ||
8 | { | ||
9 | } | ||
10 | |||
11 | Gats::Float::Float( double f ) : | ||
12 | fVal( f ) | ||
13 | { | ||
14 | } | ||
15 | |||
16 | Gats::Float::~Float() | ||
17 | { | ||
18 | } | ||
19 | |||
20 | void Gats::Float::write( QIODevice &rOut ) const | ||
21 | { | ||
22 | if( fVal == 0.0 ) | ||
23 | { | ||
24 | if( signbit( fVal ) ) | ||
25 | rOut.write("FZ", 2 ); | ||
26 | else | ||
27 | rOut.write("Fz", 2 ); | ||
28 | } | ||
29 | else if( !isfinite( fVal ) ) | ||
30 | { | ||
31 | if( isnan( fVal ) ) | ||
32 | { | ||
33 | if( signbit( fVal ) ) | ||
34 | rOut.write("FN", 2 ); | ||
35 | else | ||
36 | rOut.write("Fn", 2 ); | ||
37 | } | ||
38 | else | ||
39 | { | ||
40 | if( signbit( fVal ) ) | ||
41 | rOut.write("FI", 2 ); | ||
42 | else | ||
43 | rOut.write("Fi", 2 ); | ||
44 | } | ||
45 | } | ||
46 | else | ||
47 | { | ||
48 | rOut.write("f", 1 ); | ||
49 | double d = fVal; | ||
50 | bool bNeg = false; | ||
51 | int64_t iScale=0; | ||
52 | if( signbit( d ) ) | ||
53 | { | ||
54 | bNeg = true; | ||
55 | d = -d; | ||
56 | } | ||
57 | |||
58 | iScale = log( d ) / 0x1.62e42fefa39efp+2; // log( 256.0 ) | ||
59 | if( iScale < 0 ) iScale--; | ||
60 | d /= pow( 256.0, iScale ); | ||
61 | |||
62 | QByteArray s; | ||
63 | s += (uint8_t)(d); | ||
64 | d -= (int)d; | ||
65 | for( int j = 0; j < 150 && d; j++ ) | ||
66 | { | ||
67 | d = d*256.0; | ||
68 | s += (uint8_t)d; | ||
69 | d -= (int)d; | ||
70 | } | ||
71 | Gats::Integer::writePackedInt( rOut, bNeg?-s.size():s.size() ); | ||
72 | rOut.write( s.constData(), s.size() ); | ||
73 | Gats::Integer::writePackedInt( rOut, iScale ); | ||
74 | } | ||
75 | } | ||
76 | |||
77 | void Gats::Float::read( QIODevice &rIn, char cType ) | ||
78 | { | ||
79 | if( cType == 'F' ) | ||
80 | { | ||
81 | char buf; | ||
82 | rIn.read( &buf, 1 ); | ||
83 | switch( buf ) | ||
84 | { | ||
85 | case 'N': fVal = -NAN; break; | ||
86 | case 'n': fVal = NAN; break; | ||
87 | case 'I': fVal = -INFINITY; break; | ||
88 | case 'i': fVal = INFINITY; break; | ||
89 | case 'Z': fVal = -0.0; break; | ||
90 | case 'z': fVal = 0.0; break; | ||
91 | } | ||
92 | } | ||
93 | else if( cType == 'f' ) | ||
94 | { | ||
95 | int64_t iStr; | ||
96 | Gats::Integer::readPackedInt( rIn, iStr ); | ||
97 | bool bNeg = false; | ||
98 | if( iStr < 0 ) | ||
99 | { | ||
100 | bNeg = true; | ||
101 | iStr = -iStr; | ||
102 | } | ||
103 | QByteArray s( iStr, '\0' ); | ||
104 | rIn.read( s.data(), iStr ); | ||
105 | fVal = 0.0; | ||
106 | for( int j = iStr-1; j > 0; j-- ) | ||
107 | { | ||
108 | fVal = (fVal+(uint8_t)s[j])*0x1p-8; | ||
109 | } | ||
110 | fVal += (uint8_t)s[0]; | ||
111 | int64_t iScale; | ||
112 | Gats::Integer::readPackedInt( rIn, iScale ); | ||
113 | fVal *= pow( 256.0, iScale ); | ||
114 | if( bNeg ) fVal = -fVal; | ||
115 | } | ||
116 | } | ||
117 | /* | ||
118 | Bu::Formatter &operator<<( Bu::Formatter &f, const Gats::Float &flt ) | ||
119 | { | ||
120 | return f << "(float) " << flt.getValue(); | ||
121 | }*/ | ||
122 | |||