2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[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 //
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
13 //
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
21 // 
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 // 
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 //
33
34 using System;
35 using System.Globalization;
36
37 namespace System.Data.SqlTypes
38 {
39         public struct SqlSingle : INullable, IComparable
40         {
41                 #region Fields
42
43                 float value;
44
45                 private bool notNull;
46
47                 public static readonly SqlSingle MaxValue = new SqlSingle (3.40282346638528859E+38f);
48                 public static readonly SqlSingle MinValue = new SqlSingle (-3.40282346638528859E+38f);
49                 public static readonly SqlSingle Null;
50                 public static readonly SqlSingle Zero = new SqlSingle (0);
51
52                 #endregion
53
54                 #region Constructors
55
56                 public SqlSingle (double value) 
57                 {
58                         this.value = (float)value;
59                         notNull = true;
60                 }
61
62                 public SqlSingle (float value) 
63                 {
64                         this.value = value;
65                         notNull = true;
66                 }
67
68                 #endregion
69
70                 #region Properties
71
72                 public bool IsNull { 
73                         get { return !notNull; }
74                 }
75
76                 public float Value { 
77                         get { 
78                                 if (this.IsNull) 
79                                         throw new SqlNullValueException ();
80                                 else 
81                                         return value; 
82                         }
83                 }
84
85                 #endregion
86
87                 #region Methods
88
89                 public static SqlSingle Add (SqlSingle x, SqlSingle y)
90                 {
91                         return (x + y);
92                 }
93
94                 public int CompareTo (object value)
95                 {
96                         if (value == null)
97                                 return 1;
98                         else if (!(value is SqlSingle))
99                                 throw new ArgumentException (Locale.GetText ("Value is not a System.Data.SqlTypes.SqlSingle"));
100
101                         return CompareSqlSingle ((SqlSingle) value);
102                 }
103
104                 #if NET_2_0
105                 public int CompareTo (SqlSingle value)
106                 {
107                         return CompareSqlSingle (value);
108                 }
109                 #endif
110                 
111                 private int CompareSqlSingle (SqlSingle value)
112                 {
113                         if (value.IsNull)
114                                 return 1;
115                         else
116                                 return this.value.CompareTo (value.Value);
117                 }
118
119                 public static SqlSingle Divide (SqlSingle x, SqlSingle y)
120                 {
121                         return (x / y);
122                 }
123
124                 public override bool Equals (object value)
125                 {
126                         if (!(value is SqlSingle))
127                                 return false;
128                         else if (this.IsNull && ((SqlSingle)value).IsNull)
129                                 return true;
130                         else if (((SqlSingle)value).IsNull)
131                                 return false;
132                         else
133                                 return (bool) (this == (SqlSingle)value);
134                 }
135
136                 public static SqlBoolean Equals (SqlSingle x, SqlSingle y)
137                 {
138                         return (x == y);
139                 }
140
141                 public override int GetHashCode ()
142                 {
143                         long LongValue = (long) value;
144                         return (int)(LongValue ^ (LongValue >> 32));
145                 }
146
147                 public static SqlBoolean GreaterThan (SqlSingle x, SqlSingle y)
148                 {
149                         return (x > y);
150                 }
151
152                 public static SqlBoolean GreaterThanOrEqual (SqlSingle x, SqlSingle y)
153                 {
154                         return (x >= y);
155                 }
156
157                 public static SqlBoolean LessThan (SqlSingle x, SqlSingle y)
158                 {
159                         return (x < y);
160                 }
161
162                 public static SqlBoolean LessThanOrEqual (SqlSingle x, SqlSingle y)
163                 {
164                         return (x <= y);
165                 }
166
167                 public static SqlSingle Multiply (SqlSingle x, SqlSingle y)
168                 {
169                         return (x * y);
170                 }
171
172                 public static SqlBoolean NotEquals (SqlSingle x, SqlSingle y)
173                 {
174                         return (x != y);
175                 }
176
177                 public static SqlSingle Parse (string s)
178                 {
179                         return new SqlSingle (Single.Parse (s));
180                 }
181
182                 public static SqlSingle Subtract (SqlSingle x, SqlSingle y)
183                 {
184                         return (x - y);
185                 }
186
187                 public SqlBoolean ToSqlBoolean ()
188                 {
189                         return ((SqlBoolean)this);
190                 }
191                 
192                 public SqlByte ToSqlByte ()
193                 {
194                         return ((SqlByte)this);
195                 }
196
197                 public SqlDecimal ToSqlDecimal ()
198                 {
199                         return ((SqlDecimal)this);
200                 }
201
202                 public SqlDouble ToSqlDouble ()
203                 {
204                         return ((SqlDouble)this);
205                 }
206
207                 public SqlInt16 ToSqlInt16 ()
208                 {
209                         return ((SqlInt16)this);
210                 }
211
212                 public SqlInt32 ToSqlInt32 ()
213                 {
214                         return ((SqlInt32)this);
215                 }
216
217                 public SqlInt64 ToSqlInt64 ()
218                 {
219                         return ((SqlInt64)this);
220                 }
221
222                 public SqlMoney ToSqlMoney ()
223                 {
224                         return ((SqlMoney)this);
225                 }
226
227
228                 public SqlString ToSqlString ()
229                 {
230                         return ((SqlString)this);
231                 }
232
233                 public override string ToString ()
234                 {
235                         if (!notNull)
236                                 return "Null";
237                         return value.ToString ();
238                 }
239
240                 public static SqlSingle operator + (SqlSingle x, SqlSingle y)
241                 {
242                         float f = (float)(x.Value + y.Value);
243
244                         if (Single.IsInfinity (f))
245                                 throw new OverflowException ();
246
247                         return new SqlSingle (f);
248                 }
249
250                 public static SqlSingle operator / (SqlSingle x, SqlSingle y)
251                 {
252                         float f = (float)(x.Value / y.Value);
253
254                         if (Single.IsInfinity (f)) {
255                                 
256                                 if (y.Value == 0d) 
257                                         throw new DivideByZeroException ();
258                         }
259
260                         return new SqlSingle (x.Value / y.Value);
261                 }
262
263                 public static SqlBoolean operator == (SqlSingle x, SqlSingle y)
264                 {
265                         if (x.IsNull || y .IsNull) return SqlBoolean.Null;
266                         return new SqlBoolean (x.Value == y.Value);
267                 }
268
269                 public static SqlBoolean operator > (SqlSingle x, SqlSingle y)
270                 {
271                         if (x.IsNull || y .IsNull) return SqlBoolean.Null;
272                         return new SqlBoolean (x.Value > y.Value);
273                 }
274
275                 public static SqlBoolean operator >= (SqlSingle x, SqlSingle y)
276                 {
277                         if (x.IsNull || y .IsNull) return SqlBoolean.Null;
278                         return new SqlBoolean (x.Value >= y.Value);
279                 }
280
281                 public static SqlBoolean operator != (SqlSingle x, SqlSingle y)
282                 {
283                         if (x.IsNull || y .IsNull) return SqlBoolean.Null;
284                         return new SqlBoolean (!(x.Value == y.Value));
285                 }
286
287                 public static SqlBoolean operator < (SqlSingle x, SqlSingle y)
288                 {
289                         if (x.IsNull || y .IsNull) return SqlBoolean.Null;
290                         return new SqlBoolean (x.Value < y.Value);
291                 }
292
293                 public static SqlBoolean operator <= (SqlSingle x, SqlSingle y)
294                 {
295                         if (x.IsNull || y .IsNull) return SqlBoolean.Null;
296                         return new SqlBoolean (x.Value <= y.Value);
297                 }
298
299                 public static SqlSingle operator * (SqlSingle x, SqlSingle y)
300                 {
301                         float f = (float)(x.Value * y.Value);
302                         
303                         if (Single.IsInfinity (f))
304                                 throw new OverflowException ();
305
306                         return new SqlSingle (f);
307                 }
308
309                 public static SqlSingle operator - (SqlSingle x, SqlSingle y)
310                 {
311                         float f = (float)(x.Value - y.Value);
312
313                         if (Single.IsInfinity (f))
314                                 throw new OverflowException ();
315
316                         return new SqlSingle (f);
317                 }
318
319                 public static SqlSingle operator - (SqlSingle n)
320                 {
321                         return new SqlSingle (-(n.Value));
322                 }
323
324                 public static explicit operator SqlSingle (SqlBoolean x)
325                 {
326                         checked {
327                                 if (x.IsNull)
328                                         return Null;
329                                 
330                                 return new SqlSingle((float)x.ByteValue);
331                         }
332                 }
333
334                 public static explicit operator SqlSingle (SqlDouble x)
335                 {
336                         if (x.IsNull)
337                                 return Null;
338
339                         float f = (float)x.Value;
340
341                         if (Single.IsInfinity (f))
342                                 throw new OverflowException ();
343                                 
344                         return new SqlSingle(f);
345                 }
346
347                 public static explicit operator float (SqlSingle x)
348                 {
349                         return x.Value;
350                 }
351
352                 public static explicit operator SqlSingle (SqlString x)
353                 {
354                         checked {
355                                 if (x.IsNull)
356                                         return Null;
357                                 
358                                 return SqlSingle.Parse (x.Value);
359                         }
360                 }
361
362                 public static implicit operator SqlSingle (float x)
363                 {
364                         return new SqlSingle (x);
365                 }
366
367                 public static implicit operator SqlSingle (SqlByte x)
368                 {
369                         if (x.IsNull) 
370                                 return Null;
371                         else 
372                                 return new SqlSingle((float)x.Value);
373                 }
374
375                 public static implicit operator SqlSingle (SqlDecimal x)
376                 {
377                         if (x.IsNull) 
378                                 return Null;
379                         else
380                                 return new SqlSingle((float)x.Value);
381                 }
382
383                 public static implicit operator SqlSingle (SqlInt16 x)
384                 {
385                         if (x.IsNull) 
386                                 return Null;
387                         else
388                                 return new SqlSingle((float)x.Value);
389                 }
390
391                 public static implicit operator SqlSingle (SqlInt32 x)
392                 {
393                         if (x.IsNull) 
394                                 return Null;
395                         else
396                                 return new SqlSingle((float)x.Value);
397                 }
398
399                 public static implicit operator SqlSingle (SqlInt64 x)
400                 {
401                         if (x.IsNull) 
402                                 return Null;
403                         else
404                                 return new SqlSingle((float)x.Value);
405                 }
406
407                 public static implicit operator SqlSingle (SqlMoney x)
408                 {
409                         if (x.IsNull) 
410                                 return Null;
411                         else
412                                 return new SqlSingle((float)x.Value);
413                 }
414
415                 #endregion
416         }
417 }
418