aboutsummaryrefslogtreecommitdiff
path: root/cs-dotnet/src/gatsfloat.cs
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2012-11-15 17:41:14 +0000
committerMike Buland <eichlan@xagasoft.com>2012-11-15 17:41:14 +0000
commitf1e413e914b9f03607194757848bab1ed4f401a3 (patch)
tree0cc0dbba61295b47bf343d439a4234123c4369a3 /cs-dotnet/src/gatsfloat.cs
parent63628550708a616c5c58bc5c707db1e7fd9cd7c2 (diff)
downloadlibgats-f1e413e914b9f03607194757848bab1ed4f401a3.tar.gz
libgats-f1e413e914b9f03607194757848bab1ed4f401a3.tar.bz2
libgats-f1e413e914b9f03607194757848bab1ed4f401a3.tar.xz
libgats-f1e413e914b9f03607194757848bab1ed4f401a3.zip
Accidentally included a dll, oops. Also, gats null, and gats float work
correctly now and are tested against the python implementation.
Diffstat (limited to '')
-rw-r--r--cs-dotnet/src/gatsfloat.cs124
1 files changed, 124 insertions, 0 deletions
diff --git a/cs-dotnet/src/gatsfloat.cs b/cs-dotnet/src/gatsfloat.cs
new file mode 100644
index 0000000..7161975
--- /dev/null
+++ b/cs-dotnet/src/gatsfloat.cs
@@ -0,0 +1,124 @@
1using System.IO;
2using System;
3
4namespace Com.Xagasoft.Gats
5{
6 public class GatsFloat : GatsObject
7 {
8 private static readonly double Log256 = Math.Log( 256.0 );
9 public double Value { get; set; }
10
11 public GatsFloat()
12 {
13 Value = 0.0;
14 }
15
16 public GatsFloat( double val )
17 {
18 Value = val;
19 }
20
21 public override string ToString()
22 {
23 return Value.ToString();
24 }
25
26 public override void Read( Stream s, char type )
27 {
28 if( type == 'F' )
29 {
30 int subType = s.ReadByte();
31 if( subType == -1 )
32 throw new GatsException( GatsException.Type.PrematureEnd );
33 switch( (char)subType )
34 {
35 case 'N': Value = -Double.NaN; break;
36 case 'n': Value = Double.NaN; break;
37 case 'I': Value = Double.NegativeInfinity; break;
38 case 'i': Value = Double.PositiveInfinity; break;
39 case 'Z': Value = -0.0; break;
40 case 'z': Value = 0.0; break;
41 }
42 }
43 else if( type == 'f' )
44 {
45 int len = (int)GatsInteger.ReadPackedInt( s );
46 bool neg = false;
47 if( len < 0 )
48 {
49 neg = true;
50 len = -len;
51 }
52 int[] buf = new int[len];
53 for( int j = 0; j < len; j++ )
54 {
55 buf[j] = s.ReadByte();
56 }
57 Value = 0.0;
58 for( int j = len-1; j > 0; j-- )
59 {
60 Value = (Value+(double)buf[j]) * (1.0/256.0);
61 }
62 Value += buf[0];
63 long scale = GatsInteger.ReadPackedInt( s );
64 Value *= Math.Pow( 256.0, scale );
65 if( neg )
66 Value = -Value;
67 }
68 }
69
70 public override void Write( Stream s )
71 {
72 if( Value == 0.0 )
73 {
74 s.WriteByte( (int)'F' );
75 s.WriteByte( (int)'z' );
76 }
77 else if( Double.IsInfinity( Value ) )
78 {
79 s.WriteByte( (int)'F' );
80 if( Double.IsNegativeInfinity( Value ) )
81 s.WriteByte( (int)'I' );
82 else
83 s.WriteByte( (int)'i' );
84 }
85 else if( Double.IsNaN( Value ) )
86 {
87 s.WriteByte( (int)'F' );
88 s.WriteByte( (int)'n' );
89 }
90 else
91 {
92 s.WriteByte( (int)'f' );
93 double d = Value;
94 bool neg = false;
95 if( d < 0.0 )
96 {
97 neg = true;
98 d = -d;
99 }
100
101 MemoryStream ms = new MemoryStream();
102 long scale = (long)(Math.Log( d ) / Log256);
103 if( scale < 0 ) scale--;
104 d /= Math.Pow( 256.0, scale );
105 ms.WriteByte( (byte)d );
106 d -= (int)d;
107 for( int j = 0; j < 150 && d != 0.0; j++ )
108 {
109 d = d*256.0;
110 ms.WriteByte( (byte)d );
111 d -= (int)d;
112 }
113 byte[] msbuf = ms.ToArray();
114 if( neg )
115 GatsInteger.WritePackedInt( s, -msbuf.Length );
116 else
117 GatsInteger.WritePackedInt( s, msbuf.Length );
118 s.Write( msbuf, 0, msbuf.Length );
119 GatsInteger.WritePackedInt( s, scale );
120 }
121 }
122 }
123}
124