Add license and copyright to all source files in System.Data
[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                         else if (((SqlSingle)value).IsNull)
101                                 return 1;
102                         else
103                                 return this.value.CompareTo (((SqlSingle)value).Value);
104                 }
105
106                 public static SqlSingle Divide (SqlSingle x, SqlSingle y)
107                 {
108                         return (x / y);
109                 }
110
111                 public override bool Equals (object value)
112                 {
113                         if (!(value is SqlSingle))
114                                 return false;
115                         else if (this.IsNull && ((SqlSingle)value).IsNull)
116                                 return true;
117                         else if (((SqlSingle)value).IsNull)
118                                 return false;
119                         else
120                                 return (bool) (this == (SqlSingle)value);
121                 }
122
123                 public static SqlBoolean Equals (SqlSingle x, SqlSingle y)
124                 {
125                         return (x == y);
126                 }
127
128                 public override int GetHashCode ()
129                 {
130                         long LongValue = (long) value;
131                         return (int)(LongValue ^ (LongValue >> 32));
132                 }
133
134                 public static SqlBoolean GreaterThan (SqlSingle x, SqlSingle y)
135                 {
136                         return (x > y);
137                 }
138
139                 public static SqlBoolean GreaterThanOrEqual (SqlSingle x, SqlSingle y)
140                 {
141                         return (x >= y);
142                 }
143
144                 public static SqlBoolean LessThan (SqlSingle x, SqlSingle y)
145                 {
146                         return (x < y);
147                 }
148
149                 public static SqlBoolean LessThanOrEqual (SqlSingle x, SqlSingle y)
150                 {
151                         return (x <= y);
152                 }
153
154                 public static SqlSingle Multiply (SqlSingle x, SqlSingle y)
155                 {
156                         return (x * y);
157                 }
158
159                 public static SqlBoolean NotEquals (SqlSingle x, SqlSingle y)
160                 {
161                         return (x != y);
162                 }
163
164                 public static SqlSingle Parse (string s)
165                 {
166                         return new SqlSingle (Single.Parse (s));
167                 }
168
169                 public static SqlSingle Subtract (SqlSingle x, SqlSingle y)
170                 {
171                         return (x - y);
172                 }
173
174                 public SqlBoolean ToSqlBoolean ()
175                 {
176                         return ((SqlBoolean)this);
177                 }
178                 
179                 public SqlByte ToSqlByte ()
180                 {
181                         return ((SqlByte)this);
182                 }
183
184                 public SqlDecimal ToSqlDecimal ()
185                 {
186                         return ((SqlDecimal)this);
187                 }
188
189                 public SqlDouble ToSqlDouble ()
190                 {
191                         return ((SqlDouble)this);
192                 }
193
194                 public SqlInt16 ToSqlInt16 ()
195                 {
196                         return ((SqlInt16)this);
197                 }
198
199                 public SqlInt32 ToSqlInt32 ()
200                 {
201                         return ((SqlInt32)this);
202                 }
203
204                 public SqlInt64 ToSqlInt64 ()
205                 {
206                         return ((SqlInt64)this);
207                 }
208
209                 public SqlMoney ToSqlMoney ()
210                 {
211                         return ((SqlMoney)this);
212                 }
213
214
215                 public SqlString ToSqlString ()
216                 {
217                         return ((SqlString)this);
218                 }
219
220                 public override string ToString ()
221                 {
222                         if (!notNull)
223                                 return "Null";
224                         return value.ToString ();
225                 }
226
227                 public static SqlSingle operator + (SqlSingle x, SqlSingle y)
228                 {
229                         float f = (float)(x.Value + y.Value);
230
231                         if (Single.IsInfinity (f))
232                                 throw new OverflowException ();
233
234                         return new SqlSingle (f);
235                 }
236
237                 public static SqlSingle operator / (SqlSingle x, SqlSingle y)
238                 {
239                         float f = (float)(x.Value / y.Value);
240
241                         if (Single.IsInfinity (f)) {
242                                 
243                                 if (y.Value == 0d) 
244                                         throw new DivideByZeroException ();
245                         }
246
247                         return new SqlSingle (x.Value / y.Value);
248                 }
249
250                 public static SqlBoolean operator == (SqlSingle x, SqlSingle y)
251                 {
252                         if (x.IsNull || y .IsNull) return SqlBoolean.Null;
253                         return new SqlBoolean (x.Value == y.Value);
254                 }
255
256                 public static SqlBoolean operator > (SqlSingle x, SqlSingle y)
257                 {
258                         if (x.IsNull || y .IsNull) return SqlBoolean.Null;
259                         return new SqlBoolean (x.Value > y.Value);
260                 }
261
262                 public static SqlBoolean operator >= (SqlSingle x, SqlSingle y)
263                 {
264                         if (x.IsNull || y .IsNull) return SqlBoolean.Null;
265                         return new SqlBoolean (x.Value >= y.Value);
266                 }
267
268                 public static SqlBoolean operator != (SqlSingle x, SqlSingle y)
269                 {
270                         if (x.IsNull || y .IsNull) return SqlBoolean.Null;
271                         return new SqlBoolean (!(x.Value == y.Value));
272                 }
273
274                 public static SqlBoolean operator < (SqlSingle x, SqlSingle y)
275                 {
276                         if (x.IsNull || y .IsNull) return SqlBoolean.Null;
277                         return new SqlBoolean (x.Value < y.Value);
278                 }
279
280                 public static SqlBoolean operator <= (SqlSingle x, SqlSingle y)
281                 {
282                         if (x.IsNull || y .IsNull) return SqlBoolean.Null;
283                         return new SqlBoolean (x.Value <= y.Value);
284                 }
285
286                 public static SqlSingle operator * (SqlSingle x, SqlSingle y)
287                 {
288                         float f = (float)(x.Value * y.Value);
289                         
290                         if (Single.IsInfinity (f))
291                                 throw new OverflowException ();
292
293                         return new SqlSingle (f);
294                 }
295
296                 public static SqlSingle operator - (SqlSingle x, SqlSingle y)
297                 {
298                         float f = (float)(x.Value - y.Value);
299
300                         if (Single.IsInfinity (f))
301                                 throw new OverflowException ();
302
303                         return new SqlSingle (f);
304                 }
305
306                 public static SqlSingle operator - (SqlSingle n)
307                 {
308                         return new SqlSingle (-(n.Value));
309                 }
310
311                 public static explicit operator SqlSingle (SqlBoolean x)
312                 {
313                         checked {
314                                 if (x.IsNull)
315                                         return Null;
316                                 
317                                 return new SqlSingle((float)x.ByteValue);
318                         }
319                 }
320
321                 public static explicit operator SqlSingle (SqlDouble x)
322                 {
323                         if (x.IsNull)
324                                 return Null;
325
326                         float f = (float)x.Value;
327
328                         if (Single.IsInfinity (f))
329                                 throw new OverflowException ();
330                                 
331                         return new SqlSingle(f);
332                 }
333
334                 public static explicit operator float (SqlSingle x)
335                 {
336                         return x.Value;
337                 }
338
339                 public static explicit operator SqlSingle (SqlString x)
340                 {
341                         checked {
342                                 if (x.IsNull)
343                                         return Null;
344                                 
345                                 return SqlSingle.Parse (x.Value);
346                         }
347                 }
348
349                 public static implicit operator SqlSingle (float x)
350                 {
351                         return new SqlSingle (x);
352                 }
353
354                 public static implicit operator SqlSingle (SqlByte x)
355                 {
356                         if (x.IsNull) 
357                                 return Null;
358                         else 
359                                 return new SqlSingle((float)x.Value);
360                 }
361
362                 public static implicit operator SqlSingle (SqlDecimal x)
363                 {
364                         if (x.IsNull) 
365                                 return Null;
366                         else
367                                 return new SqlSingle((float)x.Value);
368                 }
369
370                 public static implicit operator SqlSingle (SqlInt16 x)
371                 {
372                         if (x.IsNull) 
373                                 return Null;
374                         else
375                                 return new SqlSingle((float)x.Value);
376                 }
377
378                 public static implicit operator SqlSingle (SqlInt32 x)
379                 {
380                         if (x.IsNull) 
381                                 return Null;
382                         else
383                                 return new SqlSingle((float)x.Value);
384                 }
385
386                 public static implicit operator SqlSingle (SqlInt64 x)
387                 {
388                         if (x.IsNull) 
389                                 return Null;
390                         else
391                                 return new SqlSingle((float)x.Value);
392                 }
393
394                 public static implicit operator SqlSingle (SqlMoney x)
395                 {
396                         if (x.IsNull) 
397                                 return Null;
398                         else
399                                 return new SqlSingle((float)x.Value);
400                 }
401
402                 #endregion
403         }
404 }
405