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"));
101 return CompareSqlSingle ((SqlSingle) value);
105 public int CompareTo (SqlSingle value)
107 return CompareSqlSingle (value);
111 private int CompareSqlSingle (SqlSingle value)
116 return this.value.CompareTo (value.Value);
119 public static SqlSingle Divide (SqlSingle x, SqlSingle y)
124 public override bool Equals (object value)
126 if (!(value is SqlSingle))
128 else if (this.IsNull && ((SqlSingle)value).IsNull)
130 else if (((SqlSingle)value).IsNull)
133 return (bool) (this == (SqlSingle)value);
136 public static SqlBoolean Equals (SqlSingle x, SqlSingle 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)
152 public static SqlBoolean GreaterThanOrEqual (SqlSingle x, SqlSingle y)
157 public static SqlBoolean LessThan (SqlSingle x, SqlSingle y)
162 public static SqlBoolean LessThanOrEqual (SqlSingle x, SqlSingle y)
167 public static SqlSingle Multiply (SqlSingle x, SqlSingle y)
172 public static SqlBoolean NotEquals (SqlSingle x, SqlSingle y)
177 public static SqlSingle Parse (string s)
179 return new SqlSingle (Single.Parse (s));
182 public static SqlSingle Subtract (SqlSingle x, SqlSingle 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 ()
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)) {
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)
330 return new SqlSingle((float)x.ByteValue);
334 public static explicit operator SqlSingle (SqlDouble x)
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)
352 public static explicit operator SqlSingle (SqlString x)
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)
372 return new SqlSingle((float)x.Value);
375 public static implicit operator SqlSingle (SqlDecimal x)
380 return new SqlSingle((float)x.Value);
383 public static implicit operator SqlSingle (SqlInt16 x)
388 return new SqlSingle((float)x.Value);
391 public static implicit operator SqlSingle (SqlInt32 x)
396 return new SqlSingle((float)x.Value);
399 public static implicit operator SqlSingle (SqlInt64 x)
404 return new SqlSingle((float)x.Value);
407 public static implicit operator SqlSingle (SqlMoney x)
412 return new SqlSingle((float)x.Value);