2003-05-28 Ville Palo <vi64pa@kolumbus.fi>
[mono.git] / mcs / class / System.Data / System.Data.SqlTypes / SqlSingle.cs
1 //
2 // System.Data.SqlTypes.SqlSingle
3 //
4 // Author:
5 //   Tim Coleman <tim@timcoleman.com>
6 //   Ville Palo <vi64pa@koti.soon.fi>
7 //
8 // (C) Copyright 2002 Tim Coleman
9 //
10
11 using System;
12 using System.Globalization;
13
14 namespace System.Data.SqlTypes
15 {
16         public struct SqlSingle : INullable, IComparable
17         {
18                 #region Fields
19
20                 float value;
21
22                 private bool notNull;
23
24                 public static readonly SqlSingle MaxValue = new SqlSingle (3.40282346638528859E+38f);
25                 public static readonly SqlSingle MinValue = new SqlSingle (-3.40282346638528859E+38f);
26                 public static readonly SqlSingle Null;
27                 public static readonly SqlSingle Zero = new SqlSingle (0);
28
29                 #endregion
30
31                 #region Constructors
32
33                 public SqlSingle (double value) 
34                 {
35                         this.value = (float)value;
36                         notNull = true;
37                 }
38
39                 public SqlSingle (float value) 
40                 {
41                         this.value = value;
42                         notNull = true;
43                 }
44
45                 #endregion
46
47                 #region Properties
48
49                 public bool IsNull { 
50                         get { return !notNull; }
51                 }
52
53                 public float Value { 
54                         get { 
55                                 if (this.IsNull) 
56                                         throw new SqlNullValueException ();
57                                 else 
58                                         return value; 
59                         }
60                 }
61
62                 #endregion
63
64                 #region Methods
65
66                 public static SqlSingle Add (SqlSingle x, SqlSingle y)
67                 {
68                         return (x + y);
69                 }
70
71                 public int CompareTo (object value)
72                 {
73                         if (value == null)
74                                 return 1;
75                         else if (!(value is SqlSingle))
76                                 throw new ArgumentException (Locale.GetText ("Value is not a System.Data.SqlTypes.SqlSingle"));
77                         else if (((SqlSingle)value).IsNull)
78                                 return 1;
79                         else
80                                 return this.value.CompareTo (((SqlSingle)value).Value);
81                 }
82
83                 public static SqlSingle Divide (SqlSingle x, SqlSingle y)
84                 {
85                         return (x / y);
86                 }
87
88                 public override bool Equals (object value)
89                 {
90                         if (!(value is SqlSingle))
91                                 return false;
92                         else if (this.IsNull && ((SqlSingle)value).IsNull)
93                                 return true;
94                         else if (((SqlSingle)value).IsNull)
95                                 return false;
96                         else
97                                 return (bool) (this == (SqlSingle)value);
98                 }
99
100                 public static SqlBoolean Equals (SqlSingle x, SqlSingle y)
101                 {
102                         return (x == y);
103                 }
104
105                 public override int GetHashCode ()
106                 {
107                         long LongValue = (long) value;
108                         return (int)(LongValue ^ (LongValue >> 32));
109                 }
110
111                 public static SqlBoolean GreaterThan (SqlSingle x, SqlSingle y)
112                 {
113                         return (x > y);
114                 }
115
116                 public static SqlBoolean GreaterThanOrEqual (SqlSingle x, SqlSingle y)
117                 {
118                         return (x >= y);
119                 }
120
121                 public static SqlBoolean LessThan (SqlSingle x, SqlSingle y)
122                 {
123                         return (x < y);
124                 }
125
126                 public static SqlBoolean LessThanOrEqual (SqlSingle x, SqlSingle y)
127                 {
128                         return (x <= y);
129                 }
130
131                 public static SqlSingle Multiply (SqlSingle x, SqlSingle y)
132                 {
133                         return (x * y);
134                 }
135
136                 public static SqlBoolean NotEquals (SqlSingle x, SqlSingle y)
137                 {
138                         return (x != y);
139                 }
140
141                 public static SqlSingle Parse (string s)
142                 {
143                         return new SqlSingle (Single.Parse (s));
144                 }
145
146                 public static SqlSingle Subtract (SqlSingle x, SqlSingle y)
147                 {
148                         return (x - y);
149                 }
150
151                 public SqlBoolean ToSqlBoolean ()
152                 {
153                         return ((SqlBoolean)this);
154                 }
155                 
156                 public SqlByte ToSqlByte ()
157                 {
158                         return ((SqlByte)this);
159                 }
160
161                 public SqlDecimal ToSqlDecimal ()
162                 {
163                         return ((SqlDecimal)this);
164                 }
165
166                 public SqlDouble ToSqlDouble ()
167                 {
168                         return ((SqlDouble)this);
169                 }
170
171                 public SqlInt16 ToSqlInt16 ()
172                 {
173                         return ((SqlInt16)this);
174                 }
175
176                 public SqlInt32 ToSqlInt32 ()
177                 {
178                         return ((SqlInt32)this);
179                 }
180
181                 public SqlInt64 ToSqlInt64 ()
182                 {
183                         return ((SqlInt64)this);
184                 }
185
186                 public SqlMoney ToSqlMoney ()
187                 {
188                         return ((SqlMoney)this);
189                 }
190
191
192                 public SqlString ToSqlString ()
193                 {
194                         return ((SqlString)this);
195                 }
196
197                 public override string ToString ()
198                 {
199                         return value.ToString ();
200                 }
201
202                 public static SqlSingle operator + (SqlSingle x, SqlSingle y)
203                 {
204                         float f = (float)(x.Value + y.Value);
205
206                         if (Single.IsInfinity (f))
207                                 throw new OverflowException ();
208
209                         return new SqlSingle (f);
210                 }
211
212                 public static SqlSingle operator / (SqlSingle x, SqlSingle y)
213                 {
214                         float f = (float)(x.Value / y.Value);
215
216                         if (Single.IsInfinity (f)) {
217                                 
218                                 if (y.Value == 0d) 
219                                         throw new DivideByZeroException ();
220                         }
221
222                         return new SqlSingle (x.Value / y.Value);
223                 }
224
225                 public static SqlBoolean operator == (SqlSingle x, SqlSingle y)
226                 {
227                         if (x.IsNull || y .IsNull) return SqlBoolean.Null;
228                         return new SqlBoolean (x.Value == y.Value);
229                 }
230
231                 public static SqlBoolean operator > (SqlSingle x, SqlSingle y)
232                 {
233                         if (x.IsNull || y .IsNull) return SqlBoolean.Null;
234                         return new SqlBoolean (x.Value > y.Value);
235                 }
236
237                 public static SqlBoolean operator >= (SqlSingle x, SqlSingle y)
238                 {
239                         if (x.IsNull || y .IsNull) return SqlBoolean.Null;
240                         return new SqlBoolean (x.Value >= y.Value);
241                 }
242
243                 public static SqlBoolean operator != (SqlSingle x, SqlSingle y)
244                 {
245                         if (x.IsNull || y .IsNull) return SqlBoolean.Null;
246                         return new SqlBoolean (!(x.Value == y.Value));
247                 }
248
249                 public static SqlBoolean operator < (SqlSingle x, SqlSingle y)
250                 {
251                         if (x.IsNull || y .IsNull) return SqlBoolean.Null;
252                         return new SqlBoolean (x.Value < y.Value);
253                 }
254
255                 public static SqlBoolean operator <= (SqlSingle x, SqlSingle y)
256                 {
257                         if (x.IsNull || y .IsNull) return SqlBoolean.Null;
258                         return new SqlBoolean (x.Value <= y.Value);
259                 }
260
261                 public static SqlSingle operator * (SqlSingle x, SqlSingle y)
262                 {
263                         float f = (float)(x.Value * y.Value);
264                         
265                         if (Single.IsInfinity (f))
266                                 throw new OverflowException ();
267
268                         return new SqlSingle (f);
269                 }
270
271                 public static SqlSingle operator - (SqlSingle x, SqlSingle y)
272                 {
273                         float f = (float)(x.Value - y.Value);
274
275                         if (Single.IsInfinity (f))
276                                 throw new OverflowException ();
277
278                         return new SqlSingle (f);
279                 }
280
281                 public static SqlSingle operator - (SqlSingle n)
282                 {
283                         return new SqlSingle (-(n.Value));
284                 }
285
286                 public static explicit operator SqlSingle (SqlBoolean x)
287                 {
288                         checked {
289                                 if (x.IsNull)
290                                         return Null;
291                                 
292                                 return new SqlSingle((float)x.ByteValue);
293                         }
294                 }
295
296                 public static explicit operator SqlSingle (SqlDouble x)
297                 {
298                         if (x.IsNull)
299                                 return Null;
300
301                         float f = (float)x.Value;
302
303                         if (Single.IsInfinity (f))
304                                 throw new OverflowException ();
305                                 
306                         return new SqlSingle(f);
307                 }
308
309                 public static explicit operator float (SqlSingle x)
310                 {
311                         return x.Value;
312                 }
313
314                 public static explicit operator SqlSingle (SqlString x)
315                 {
316                         checked {
317                                 if (x.IsNull)
318                                         return Null;
319                                 
320                                 return SqlSingle.Parse (x.Value);
321                         }
322                 }
323
324                 public static implicit operator SqlSingle (float x)
325                 {
326                         return new SqlSingle (x);
327                 }
328
329                 public static implicit operator SqlSingle (SqlByte x)
330                 {
331                         if (x.IsNull) 
332                                 return Null;
333                         else 
334                                 return new SqlSingle((float)x.Value);
335                 }
336
337                 public static implicit operator SqlSingle (SqlDecimal x)
338                 {
339                         if (x.IsNull) 
340                                 return Null;
341                         else
342                                 return new SqlSingle((float)x.Value);
343                 }
344
345                 public static implicit operator SqlSingle (SqlInt16 x)
346                 {
347                         if (x.IsNull) 
348                                 return Null;
349                         else
350                                 return new SqlSingle((float)x.Value);
351                 }
352
353                 public static implicit operator SqlSingle (SqlInt32 x)
354                 {
355                         if (x.IsNull) 
356                                 return Null;
357                         else
358                                 return new SqlSingle((float)x.Value);
359                 }
360
361                 public static implicit operator SqlSingle (SqlInt64 x)
362                 {
363                         if (x.IsNull) 
364                                 return Null;
365                         else
366                                 return new SqlSingle((float)x.Value);
367                 }
368
369                 public static implicit operator SqlSingle (SqlMoney x)
370                 {
371                         if (x.IsNull) 
372                                 return Null;
373                         else
374                                 return new SqlSingle((float)x.Value);
375                 }
376
377                 #endregion
378         }
379 }
380