2 // System.Data.SqlTypes.SqlSingle
5 // Tim Coleman <tim@timcoleman.com>
6 // Ville Palo <vi64pa@koti.soon.fi>
8 // (C) Copyright 2002 Tim Coleman
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:
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
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.
35 using System.Globalization;
37 namespace System.Data.SqlTypes
39 public struct SqlSingle : INullable, IComparable
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);
56 public SqlSingle (double value)
58 this.value = (float)value;
62 public SqlSingle (float value)
73 get { return !notNull; }
79 throw new SqlNullValueException ();
89 public static SqlSingle Add (SqlSingle x, SqlSingle y)
94 public int CompareTo (object value)
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)
103 return this.value.CompareTo (((SqlSingle)value).Value);
106 public static SqlSingle Divide (SqlSingle x, SqlSingle y)
111 public override bool Equals (object value)
113 if (!(value is SqlSingle))
115 else if (this.IsNull && ((SqlSingle)value).IsNull)
117 else if (((SqlSingle)value).IsNull)
120 return (bool) (this == (SqlSingle)value);
123 public static SqlBoolean Equals (SqlSingle x, SqlSingle y)
128 public override int GetHashCode ()
130 long LongValue = (long) value;
131 return (int)(LongValue ^ (LongValue >> 32));
134 public static SqlBoolean GreaterThan (SqlSingle x, SqlSingle y)
139 public static SqlBoolean GreaterThanOrEqual (SqlSingle x, SqlSingle y)
144 public static SqlBoolean LessThan (SqlSingle x, SqlSingle y)
149 public static SqlBoolean LessThanOrEqual (SqlSingle x, SqlSingle y)
154 public static SqlSingle Multiply (SqlSingle x, SqlSingle y)
159 public static SqlBoolean NotEquals (SqlSingle x, SqlSingle y)
164 public static SqlSingle Parse (string s)
166 return new SqlSingle (Single.Parse (s));
169 public static SqlSingle Subtract (SqlSingle x, SqlSingle y)
174 public SqlBoolean ToSqlBoolean ()
176 return ((SqlBoolean)this);
179 public SqlByte ToSqlByte ()
181 return ((SqlByte)this);
184 public SqlDecimal ToSqlDecimal ()
186 return ((SqlDecimal)this);
189 public SqlDouble ToSqlDouble ()
191 return ((SqlDouble)this);
194 public SqlInt16 ToSqlInt16 ()
196 return ((SqlInt16)this);
199 public SqlInt32 ToSqlInt32 ()
201 return ((SqlInt32)this);
204 public SqlInt64 ToSqlInt64 ()
206 return ((SqlInt64)this);
209 public SqlMoney ToSqlMoney ()
211 return ((SqlMoney)this);
215 public SqlString ToSqlString ()
217 return ((SqlString)this);
220 public override string ToString ()
224 return value.ToString ();
227 public static SqlSingle operator + (SqlSingle x, SqlSingle y)
229 float f = (float)(x.Value + y.Value);
231 if (Single.IsInfinity (f))
232 throw new OverflowException ();
234 return new SqlSingle (f);
237 public static SqlSingle operator / (SqlSingle x, SqlSingle y)
239 float f = (float)(x.Value / y.Value);
241 if (Single.IsInfinity (f)) {
244 throw new DivideByZeroException ();
247 return new SqlSingle (x.Value / y.Value);
250 public static SqlBoolean operator == (SqlSingle x, SqlSingle y)
252 if (x.IsNull || y .IsNull) return SqlBoolean.Null;
253 return new SqlBoolean (x.Value == y.Value);
256 public static SqlBoolean operator > (SqlSingle x, SqlSingle y)
258 if (x.IsNull || y .IsNull) return SqlBoolean.Null;
259 return new SqlBoolean (x.Value > y.Value);
262 public static SqlBoolean operator >= (SqlSingle x, SqlSingle y)
264 if (x.IsNull || y .IsNull) return SqlBoolean.Null;
265 return new SqlBoolean (x.Value >= y.Value);
268 public static SqlBoolean operator != (SqlSingle x, SqlSingle y)
270 if (x.IsNull || y .IsNull) return SqlBoolean.Null;
271 return new SqlBoolean (!(x.Value == y.Value));
274 public static SqlBoolean operator < (SqlSingle x, SqlSingle y)
276 if (x.IsNull || y .IsNull) return SqlBoolean.Null;
277 return new SqlBoolean (x.Value < y.Value);
280 public static SqlBoolean operator <= (SqlSingle x, SqlSingle y)
282 if (x.IsNull || y .IsNull) return SqlBoolean.Null;
283 return new SqlBoolean (x.Value <= y.Value);
286 public static SqlSingle operator * (SqlSingle x, SqlSingle y)
288 float f = (float)(x.Value * y.Value);
290 if (Single.IsInfinity (f))
291 throw new OverflowException ();
293 return new SqlSingle (f);
296 public static SqlSingle operator - (SqlSingle x, SqlSingle y)
298 float f = (float)(x.Value - y.Value);
300 if (Single.IsInfinity (f))
301 throw new OverflowException ();
303 return new SqlSingle (f);
306 public static SqlSingle operator - (SqlSingle n)
308 return new SqlSingle (-(n.Value));
311 public static explicit operator SqlSingle (SqlBoolean x)
317 return new SqlSingle((float)x.ByteValue);
321 public static explicit operator SqlSingle (SqlDouble x)
326 float f = (float)x.Value;
328 if (Single.IsInfinity (f))
329 throw new OverflowException ();
331 return new SqlSingle(f);
334 public static explicit operator float (SqlSingle x)
339 public static explicit operator SqlSingle (SqlString x)
345 return SqlSingle.Parse (x.Value);
349 public static implicit operator SqlSingle (float x)
351 return new SqlSingle (x);
354 public static implicit operator SqlSingle (SqlByte x)
359 return new SqlSingle((float)x.Value);
362 public static implicit operator SqlSingle (SqlDecimal x)
367 return new SqlSingle((float)x.Value);
370 public static implicit operator SqlSingle (SqlInt16 x)
375 return new SqlSingle((float)x.Value);
378 public static implicit operator SqlSingle (SqlInt32 x)
383 return new SqlSingle((float)x.Value);
386 public static implicit operator SqlSingle (SqlInt64 x)
391 return new SqlSingle((float)x.Value);
394 public static implicit operator SqlSingle (SqlMoney x)
399 return new SqlSingle((float)x.Value);