diff options
author | Mike Buland <eichlan@xagasoft.com> | 2011-04-21 20:40:06 +0000 |
---|---|---|
committer | Mike Buland <eichlan@xagasoft.com> | 2011-04-21 20:40:06 +0000 |
commit | fa070d1f3a5853105d678324f2902457c917eab1 (patch) | |
tree | 0ae724617edf3008dbe553edaf0b8647df4932f8 /java/com/xagasoft/gats/GatsFloat.java | |
parent | d56af4807693e0e3b443f729b44d9aa8dcbb369d (diff) | |
download | libgats-fa070d1f3a5853105d678324f2902457c917eab1.tar.gz libgats-fa070d1f3a5853105d678324f2902457c917eab1.tar.bz2 libgats-fa070d1f3a5853105d678324f2902457c917eab1.tar.xz libgats-fa070d1f3a5853105d678324f2902457c917eab1.zip |
Gats float should (maybe) work...
Diffstat (limited to '')
-rw-r--r-- | java/com/xagasoft/gats/GatsFloat.java | 119 |
1 files changed, 118 insertions, 1 deletions
diff --git a/java/com/xagasoft/gats/GatsFloat.java b/java/com/xagasoft/gats/GatsFloat.java index 62e763c..d70f910 100644 --- a/java/com/xagasoft/gats/GatsFloat.java +++ b/java/com/xagasoft/gats/GatsFloat.java | |||
@@ -1,21 +1,138 @@ | |||
1 | package com.xagasoft.gats; | 1 | package com.xagasoft.gats; |
2 | 2 | ||
3 | import java.io.InputStream; | 3 | import java.io.InputStream; |
4 | import java.io.DataInputStream; | ||
4 | import java.io.OutputStream; | 5 | import java.io.OutputStream; |
6 | import java.io.ByteArrayOutputStream; | ||
7 | |||
8 | import java.lang.Math; | ||
5 | 9 | ||
6 | public class GatsFloat extends GatsObject | 10 | public class GatsFloat extends GatsObject |
7 | { | 11 | { |
12 | double dValue = 0.0; | ||
13 | |||
14 | public GatsFloat() | ||
15 | { | ||
16 | } | ||
17 | |||
18 | public GatsFloat( double dValue ) | ||
19 | { | ||
20 | this.dValue = dValue; | ||
21 | } | ||
22 | |||
23 | public double getValue() | ||
24 | { | ||
25 | return dValue; | ||
26 | } | ||
27 | |||
28 | public void setValue( double dValue ) | ||
29 | { | ||
30 | this.dValue = dValue; | ||
31 | } | ||
32 | |||
33 | public String toString() | ||
34 | { | ||
35 | return "" + dValue; | ||
36 | } | ||
37 | |||
8 | public int getType() | 38 | public int getType() |
9 | { | 39 | { |
10 | return GatsObject.FLOAT; | 40 | return GatsObject.FLOAT; |
11 | } | 41 | } |
12 | 42 | ||
13 | public void read( InputStream is, char cType ) | 43 | public void read( InputStream is, char cType ) throws java.io.IOException |
14 | { | 44 | { |
45 | if( cType == 'F' ) | ||
46 | { | ||
47 | char cSubType = (char)is.read(); | ||
48 | switch( cSubType ) | ||
49 | { | ||
50 | case 'N': dValue = -Double.NaN; break; | ||
51 | case 'n': dValue = Double.NaN; break; | ||
52 | case 'I': dValue = Double.NEGATIVE_INFINITY; break; | ||
53 | case 'i': dValue = Double.POSITIVE_INFINITY; break; | ||
54 | case 'Z': dValue = -0.0; break; | ||
55 | case 'z': dValue = 0.0; break; | ||
56 | } | ||
57 | } | ||
58 | else if( cType == 'f' ) | ||
59 | { | ||
60 | int iStr = (int)GatsInteger.readPackedInt( is ); | ||
61 | boolean bNeg = false; | ||
62 | if( iStr < 0 ) | ||
63 | { | ||
64 | bNeg = true; | ||
65 | iStr = -iStr; | ||
66 | } | ||
67 | int aBuf[] = new int[iStr]; | ||
68 | DataInputStream dis = new DataInputStream( is ); | ||
69 | for( int j = 0; j < iStr; j++ ) | ||
70 | { | ||
71 | aBuf[j] = dis.readUnsignedByte(); | ||
72 | } | ||
73 | dValue = 0.0; | ||
74 | for( int j = iStr-1; j > 0; j-- ) | ||
75 | { | ||
76 | dValue = (dValue+(double)aBuf[j])*0x1p-8; | ||
77 | } | ||
78 | dValue += aBuf[0]; | ||
79 | long iScale = GatsInteger.readPackedInt( is ); | ||
80 | dValue *= Math.pow( 256.0, iScale ); | ||
81 | if( bNeg ) | ||
82 | dValue = -dValue; | ||
83 | } | ||
15 | } | 84 | } |
16 | 85 | ||
17 | public void write( OutputStream os ) throws java.io.IOException | 86 | public void write( OutputStream os ) throws java.io.IOException |
18 | { | 87 | { |
88 | if( dValue == 0.0 ) | ||
89 | { | ||
90 | os.write( (int)'F' ); | ||
91 | os.write( (int)'z' ); | ||
92 | } | ||
93 | else if( Double.isInfinite( dValue ) ) | ||
94 | { | ||
95 | os.write( (int)'F' ); | ||
96 | if( dValue < 0 ) | ||
97 | os.write( (int)'I' ); | ||
98 | else | ||
99 | os.write( (int)'i' ); | ||
100 | } | ||
101 | else if( Double.isNaN( dValue ) ) | ||
102 | { | ||
103 | os.write( (int)'F' ); | ||
104 | os.write( (int)'n' ); | ||
105 | } | ||
106 | else | ||
107 | { | ||
108 | os.write( (int)'f' ); | ||
109 | double d = dValue; | ||
110 | boolean bNeg = false; | ||
111 | if( d < 0.0 ) | ||
112 | { | ||
113 | bNeg = true; | ||
114 | d = -d; | ||
115 | } | ||
116 | |||
117 | ByteArrayOutputStream oTmp = new ByteArrayOutputStream(); | ||
118 | long iScale = (long)(Math.log( d ) / 0x1.62e42fefa39efp+2); | ||
119 | if( iScale < 0 ) iScale--; | ||
120 | d /= Math.pow( 256.0, iScale ); | ||
121 | oTmp.write( (int)d ); | ||
122 | d -= (int)d; | ||
123 | for( int j = 0; j < 150 && d != 0.0; j++ ) | ||
124 | { | ||
125 | d = d*256.0; | ||
126 | oTmp.write( (int)d ); | ||
127 | d -= (int)d; | ||
128 | } | ||
129 | if( bNeg ) | ||
130 | GatsInteger.writePackedInt( os, -oTmp.size() ); | ||
131 | else | ||
132 | GatsInteger.writePackedInt( os, oTmp.size() ); | ||
133 | os.write( oTmp.toByteArray() ); | ||
134 | GatsInteger.writePackedInt( os, iScale ); | ||
135 | } | ||
19 | } | 136 | } |
20 | }; | 137 | }; |
21 | 138 | ||