aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2011-04-21 20:40:06 +0000
committerMike Buland <eichlan@xagasoft.com>2011-04-21 20:40:06 +0000
commitfa070d1f3a5853105d678324f2902457c917eab1 (patch)
tree0ae724617edf3008dbe553edaf0b8647df4932f8
parentd56af4807693e0e3b443f729b44d9aa8dcbb369d (diff)
downloadlibgats-fa070d1f3a5853105d678324f2902457c917eab1.tar.gz
libgats-fa070d1f3a5853105d678324f2902457c917eab1.tar.bz2
libgats-fa070d1f3a5853105d678324f2902457c917eab1.tar.xz
libgats-fa070d1f3a5853105d678324f2902457c917eab1.zip
Gats float should (maybe) work...
-rw-r--r--java/com/xagasoft/gats/GatsFloat.java119
-rw-r--r--java/com/xagasoft/gats/GatsObject.java2
2 files changed, 119 insertions, 2 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 @@
1package com.xagasoft.gats; 1package com.xagasoft.gats;
2 2
3import java.io.InputStream; 3import java.io.InputStream;
4import java.io.DataInputStream;
4import java.io.OutputStream; 5import java.io.OutputStream;
6import java.io.ByteArrayOutputStream;
7
8import java.lang.Math;
5 9
6public class GatsFloat extends GatsObject 10public 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
diff --git a/java/com/xagasoft/gats/GatsObject.java b/java/com/xagasoft/gats/GatsObject.java
index 266a08f..8c398e8 100644
--- a/java/com/xagasoft/gats/GatsObject.java
+++ b/java/com/xagasoft/gats/GatsObject.java
@@ -46,7 +46,7 @@ public abstract class GatsObject
46 46
47 case 'f': 47 case 'f':
48 case 'F': 48 case 'F':
49 // goRet = new GatsFloat(); 49 goRet = new GatsFloat();
50 break; 50 break;
51 51
52 case 'e': 52 case 'e':