From fa070d1f3a5853105d678324f2902457c917eab1 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Thu, 21 Apr 2011 20:40:06 +0000 Subject: Gats float should (maybe) work... --- java/com/xagasoft/gats/GatsFloat.java | 119 ++++++++++++++++++++++++++++++++- java/com/xagasoft/gats/GatsObject.java | 2 +- 2 files changed, 119 insertions(+), 2 deletions(-) (limited to 'java') 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 @@ package com.xagasoft.gats; import java.io.InputStream; +import java.io.DataInputStream; import java.io.OutputStream; +import java.io.ByteArrayOutputStream; + +import java.lang.Math; public class GatsFloat extends GatsObject { + double dValue = 0.0; + + public GatsFloat() + { + } + + public GatsFloat( double dValue ) + { + this.dValue = dValue; + } + + public double getValue() + { + return dValue; + } + + public void setValue( double dValue ) + { + this.dValue = dValue; + } + + public String toString() + { + return "" + dValue; + } + public int getType() { return GatsObject.FLOAT; } - public void read( InputStream is, char cType ) + public void read( InputStream is, char cType ) throws java.io.IOException { + if( cType == 'F' ) + { + char cSubType = (char)is.read(); + switch( cSubType ) + { + case 'N': dValue = -Double.NaN; break; + case 'n': dValue = Double.NaN; break; + case 'I': dValue = Double.NEGATIVE_INFINITY; break; + case 'i': dValue = Double.POSITIVE_INFINITY; break; + case 'Z': dValue = -0.0; break; + case 'z': dValue = 0.0; break; + } + } + else if( cType == 'f' ) + { + int iStr = (int)GatsInteger.readPackedInt( is ); + boolean bNeg = false; + if( iStr < 0 ) + { + bNeg = true; + iStr = -iStr; + } + int aBuf[] = new int[iStr]; + DataInputStream dis = new DataInputStream( is ); + for( int j = 0; j < iStr; j++ ) + { + aBuf[j] = dis.readUnsignedByte(); + } + dValue = 0.0; + for( int j = iStr-1; j > 0; j-- ) + { + dValue = (dValue+(double)aBuf[j])*0x1p-8; + } + dValue += aBuf[0]; + long iScale = GatsInteger.readPackedInt( is ); + dValue *= Math.pow( 256.0, iScale ); + if( bNeg ) + dValue = -dValue; + } } public void write( OutputStream os ) throws java.io.IOException { + if( dValue == 0.0 ) + { + os.write( (int)'F' ); + os.write( (int)'z' ); + } + else if( Double.isInfinite( dValue ) ) + { + os.write( (int)'F' ); + if( dValue < 0 ) + os.write( (int)'I' ); + else + os.write( (int)'i' ); + } + else if( Double.isNaN( dValue ) ) + { + os.write( (int)'F' ); + os.write( (int)'n' ); + } + else + { + os.write( (int)'f' ); + double d = dValue; + boolean bNeg = false; + if( d < 0.0 ) + { + bNeg = true; + d = -d; + } + + ByteArrayOutputStream oTmp = new ByteArrayOutputStream(); + long iScale = (long)(Math.log( d ) / 0x1.62e42fefa39efp+2); + if( iScale < 0 ) iScale--; + d /= Math.pow( 256.0, iScale ); + oTmp.write( (int)d ); + d -= (int)d; + for( int j = 0; j < 150 && d != 0.0; j++ ) + { + d = d*256.0; + oTmp.write( (int)d ); + d -= (int)d; + } + if( bNeg ) + GatsInteger.writePackedInt( os, -oTmp.size() ); + else + GatsInteger.writePackedInt( os, oTmp.size() ); + os.write( oTmp.toByteArray() ); + GatsInteger.writePackedInt( os, iScale ); + } } }; 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 case 'f': case 'F': - // goRet = new GatsFloat(); + goRet = new GatsFloat(); break; case 'e': -- cgit v1.2.3