**** Merged from MCS ****
[mono-project.git] / mcs / class / System.Data / System.Data.SqlTypes / SqlSingle.cs
blob5b1b2dac04b03e4e5ad464224a33497ca7d6ae5d
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 //
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
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.
34 using System;
35 using System.Globalization;
37 namespace System.Data.SqlTypes
39 public struct SqlSingle : INullable, IComparable
41 #region Fields
43 float value;
45 private bool notNull;
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);
52 #endregion
54 #region Constructors
56 public SqlSingle (double value)
58 this.value = (float)value;
59 notNull = true;
62 public SqlSingle (float value)
64 this.value = value;
65 notNull = true;
68 #endregion
70 #region Properties
72 public bool IsNull {
73 get { return !notNull; }
76 public float Value {
77 get {
78 if (this.IsNull)
79 throw new SqlNullValueException ();
80 else
81 return value;
85 #endregion
87 #region Methods
89 public static SqlSingle Add (SqlSingle x, SqlSingle y)
91 return (x + y);
94 public int CompareTo (object value)
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"));
101 return CompareSqlSingle ((SqlSingle) value);
104 #if NET_2_0
105 public int CompareTo (SqlSingle value)
107 return CompareSqlSingle (value);
109 #endif
111 private int CompareSqlSingle (SqlSingle value)
113 if (value.IsNull)
114 return 1;
115 else
116 return this.value.CompareTo (value.Value);
119 public static SqlSingle Divide (SqlSingle x, SqlSingle y)
121 return (x / y);
124 public override bool Equals (object value)
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);
136 public static SqlBoolean Equals (SqlSingle x, SqlSingle y)
138 return (x == y);
141 public override int GetHashCode ()
143 long LongValue = (long) value;
144 return (int)(LongValue ^ (LongValue >> 32));
147 public static SqlBoolean GreaterThan (SqlSingle x, SqlSingle y)
149 return (x > y);
152 public static SqlBoolean GreaterThanOrEqual (SqlSingle x, SqlSingle y)
154 return (x >= y);
157 public static SqlBoolean LessThan (SqlSingle x, SqlSingle y)
159 return (x < y);
162 public static SqlBoolean LessThanOrEqual (SqlSingle x, SqlSingle y)
164 return (x <= y);
167 public static SqlSingle Multiply (SqlSingle x, SqlSingle y)
169 return (x * y);
172 public static SqlBoolean NotEquals (SqlSingle x, SqlSingle y)
174 return (x != y);
177 public static SqlSingle Parse (string s)
179 return new SqlSingle (Single.Parse (s));
182 public static SqlSingle Subtract (SqlSingle x, SqlSingle y)
184 return (x - y);
187 public SqlBoolean ToSqlBoolean ()
189 return ((SqlBoolean)this);
192 public SqlByte ToSqlByte ()
194 return ((SqlByte)this);
197 public SqlDecimal ToSqlDecimal ()
199 return ((SqlDecimal)this);
202 public SqlDouble ToSqlDouble ()
204 return ((SqlDouble)this);
207 public SqlInt16 ToSqlInt16 ()
209 return ((SqlInt16)this);
212 public SqlInt32 ToSqlInt32 ()
214 return ((SqlInt32)this);
217 public SqlInt64 ToSqlInt64 ()
219 return ((SqlInt64)this);
222 public SqlMoney ToSqlMoney ()
224 return ((SqlMoney)this);
228 public SqlString ToSqlString ()
230 return ((SqlString)this);
233 public override string ToString ()
235 if (!notNull)
236 return "Null";
237 return value.ToString ();
240 public static SqlSingle operator + (SqlSingle x, SqlSingle y)
242 float f = (float)(x.Value + y.Value);
244 if (Single.IsInfinity (f))
245 throw new OverflowException ();
247 return new SqlSingle (f);
250 public static SqlSingle operator / (SqlSingle x, SqlSingle y)
252 float f = (float)(x.Value / y.Value);
254 if (Single.IsInfinity (f)) {
256 if (y.Value == 0d)
257 throw new DivideByZeroException ();
260 return new SqlSingle (x.Value / y.Value);
263 public static SqlBoolean operator == (SqlSingle x, SqlSingle y)
265 if (x.IsNull || y .IsNull) return SqlBoolean.Null;
266 return new SqlBoolean (x.Value == y.Value);
269 public static SqlBoolean operator > (SqlSingle x, SqlSingle y)
271 if (x.IsNull || y .IsNull) return SqlBoolean.Null;
272 return new SqlBoolean (x.Value > y.Value);
275 public static SqlBoolean operator >= (SqlSingle x, SqlSingle y)
277 if (x.IsNull || y .IsNull) return SqlBoolean.Null;
278 return new SqlBoolean (x.Value >= y.Value);
281 public static SqlBoolean operator != (SqlSingle x, SqlSingle y)
283 if (x.IsNull || y .IsNull) return SqlBoolean.Null;
284 return new SqlBoolean (!(x.Value == y.Value));
287 public static SqlBoolean operator < (SqlSingle x, SqlSingle y)
289 if (x.IsNull || y .IsNull) return SqlBoolean.Null;
290 return new SqlBoolean (x.Value < y.Value);
293 public static SqlBoolean operator <= (SqlSingle x, SqlSingle y)
295 if (x.IsNull || y .IsNull) return SqlBoolean.Null;
296 return new SqlBoolean (x.Value <= y.Value);
299 public static SqlSingle operator * (SqlSingle x, SqlSingle y)
301 float f = (float)(x.Value * y.Value);
303 if (Single.IsInfinity (f))
304 throw new OverflowException ();
306 return new SqlSingle (f);
309 public static SqlSingle operator - (SqlSingle x, SqlSingle y)
311 float f = (float)(x.Value - y.Value);
313 if (Single.IsInfinity (f))
314 throw new OverflowException ();
316 return new SqlSingle (f);
319 public static SqlSingle operator - (SqlSingle n)
321 return new SqlSingle (-(n.Value));
324 public static explicit operator SqlSingle (SqlBoolean x)
326 checked {
327 if (x.IsNull)
328 return Null;
330 return new SqlSingle((float)x.ByteValue);
334 public static explicit operator SqlSingle (SqlDouble x)
336 if (x.IsNull)
337 return Null;
339 float f = (float)x.Value;
341 if (Single.IsInfinity (f))
342 throw new OverflowException ();
344 return new SqlSingle(f);
347 public static explicit operator float (SqlSingle x)
349 return x.Value;
352 public static explicit operator SqlSingle (SqlString x)
354 checked {
355 if (x.IsNull)
356 return Null;
358 return SqlSingle.Parse (x.Value);
362 public static implicit operator SqlSingle (float x)
364 return new SqlSingle (x);
367 public static implicit operator SqlSingle (SqlByte x)
369 if (x.IsNull)
370 return Null;
371 else
372 return new SqlSingle((float)x.Value);
375 public static implicit operator SqlSingle (SqlDecimal x)
377 if (x.IsNull)
378 return Null;
379 else
380 return new SqlSingle((float)x.Value);
383 public static implicit operator SqlSingle (SqlInt16 x)
385 if (x.IsNull)
386 return Null;
387 else
388 return new SqlSingle((float)x.Value);
391 public static implicit operator SqlSingle (SqlInt32 x)
393 if (x.IsNull)
394 return Null;
395 else
396 return new SqlSingle((float)x.Value);
399 public static implicit operator SqlSingle (SqlInt64 x)
401 if (x.IsNull)
402 return Null;
403 else
404 return new SqlSingle((float)x.Value);
407 public static implicit operator SqlSingle (SqlMoney x)
409 if (x.IsNull)
410 return Null;
411 else
412 return new SqlSingle((float)x.Value);
415 #endregion