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