1 // System.Data.SqlTypes.SqlSingle
\r
4 // Konstantin Triger <kostat@mainsoft.com>
5 // Boris Kirzner <borisk@mainsoft.com>
7 // (C) 2005 Mainsoft Corporation (http://www.mainsoft.com)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 namespace System.Data.SqlTypes
\r
36 * <p>Description: </p>
\r
37 * <p>Copyright: Copyright (c) 2002</p>
\r
38 * <p>Company: MainSoft</p>
\r
39 * @author Pavel Sandler
\r
45 public struct SqlSingle : INullable, IComparable
\r
48 private float _value;
\r
50 private static float _minVal = -3.40E+38F;
\r
51 private static float _maxVal = 3.40E+38F;
\r
53 public static readonly SqlSingle MaxValue = new SqlSingle(3.40E+38F);
\r
54 public static readonly SqlSingle MinValue = new SqlSingle(-3.40E+38F);
\r
55 public static readonly SqlSingle Null = new SqlSingle(true);
\r
56 public static readonly SqlSingle Zero = new SqlSingle(0);
\r
58 private bool _isNull;
\r
60 private SqlSingle(bool isNull)
\r
68 * @param value A float whose value will be used for the new SqlSingle.
\r
70 public SqlSingle(float value)
\r
78 * Indicates whether or not Value is null.
\r
79 * @return true if Value is null, otherwise false.
\r
90 * Gets the value of the SqlSingle instance.
\r
91 * @return the value of this instance
\r
98 throw new SqlNullValueException();
\r
104 * The addition operator computes the sum of the two SqlSingle operands.
\r
105 * @param x A SqlSingle structure.
\r
106 * @param y A SqlSingle structure.
\r
107 * @return The sum of the two SqlSingle operands.
\r
108 * If one of the parameters is null or null value - return SqlSingle.Null.
\r
110 public static SqlSingle Add(SqlSingle x, SqlSingle y)
\r
112 if (x.IsNull || y.IsNull)
\r
113 return SqlSingle.Null;
\r
115 float xVal = x._value;
\r
116 float yVal = y._value;
\r
118 if (xVal < 0 && yVal < 0 && (_minVal - xVal > yVal))
\r
119 throw new System.OverflowException("Overflow - " + x + " + " + y + " < " + _minVal);
\r
120 if (xVal > 0 && yVal > 0 && (_maxVal - xVal < yVal))
\r
121 throw new System.OverflowException("Overflow - " + x + " + " + y + " > " + _maxVal);
\r
124 return new SqlSingle(xVal + yVal);
\r
128 * Compares this instance to the supplied object and returns an indication of their relative values.
\r
129 * @param obj The object to compare.
\r
130 * @return A signed number indicating the relative values of the instance and the object.
\r
131 * Less than zero This instance is less than object.
\r
132 * Zero This instance is the same as object.
\r
133 * Greater than zero This instance is greater than object -or-
\r
134 * object is a null reference.
\r
136 public int CompareTo(Object obj)
\r
141 if (obj is SqlSingle)
\r
143 SqlSingle d = (SqlSingle)obj;
\r
150 return this._value.CompareTo(d._value);
\r
153 throw new ArgumentException("parameter obj is not SqlSingle : " + obj.GetType().Name);
\r
158 * The division operator divides the first SqlSingle operand by the second.
\r
159 * @param x A SqlSingle instance.
\r
160 * @param y A SqlSingle instance.
\r
161 * @return A SqlSingle structure containing the results of the division operation.
\r
162 * If one of the parameters is null or null value - return SqlSingle.Null.
\r
164 public static SqlSingle Divide(SqlSingle x, SqlSingle y)
\r
166 if (x.IsNull || y.IsNull)
\r
167 return SqlSingle.Null;
\r
169 float xVal = x._value;
\r
170 float yVal = y._value;
\r
172 if (java.lang.Math.abs(yVal) < 1)
\r
174 if (xVal < 0 && yVal < 0 && (_maxVal * yVal > xVal))
\r
175 throw new System.OverflowException("Overflow - " + x + " / " + y + " > " + _maxVal);
\r
176 if (xVal < 0 && yVal > 0 && (_minVal * yVal > xVal))
\r
177 throw new System.OverflowException("Overflow - " + x + " / " + y + " < " + _minVal);
\r
178 if (xVal > 0 && yVal < 0 && (_minVal * yVal < xVal))
\r
179 throw new System.OverflowException("Overflow - " + x + " / " + y + " < " + _minVal);
\r
180 if (xVal > 0 && yVal > 0 && (_maxVal * yVal < xVal))
\r
181 throw new System.OverflowException("Overflow - " + x + " / " + y + " > " + _maxVal);
\r
184 return new SqlSingle(xVal / yVal);
\r
187 public override bool Equals(Object obj)
\r
189 if (Object.ReferenceEquals(obj, this))
\r
195 if (obj is SqlSingle)
\r
197 SqlSingle d = (SqlSingle)obj;
\r
199 if (IsNull && d.IsNull)
\r
202 if (IsNull || d.IsNull)
\r
205 return _value.Equals(d._value);
\r
215 * Performs a logical comparison on two instances of SqlSingle to determine if they are equal.
\r
216 * @param x A SqlSingle instance.
\r
217 * @param y A SqlSingle instance.
\r
218 * @return true if the two values are equal, otherwise false.
\r
219 * If one of the parameters is null or null value return SqlBoolean.Null.
\r
221 public static SqlBoolean Equals(SqlSingle x, SqlSingle y)
\r
223 if (x.IsNull || y.IsNull)
\r
224 return SqlBoolean.Null;
\r
227 return SqlBoolean.True;
\r
229 return SqlBoolean.False;
\r
233 * Compares two instances of SqlSingle to determine if the first is greater than the second.
\r
234 * @param x A SqlSingle instance
\r
235 * @param y A SqlSingle instance
\r
236 * @return A SqlBoolean that is True if the first instance is greater than the second instance, otherwise False.
\r
237 * If either instance of SqlSingle is null, the Value of the SqlBoolean will be Null.
\r
239 public static SqlBoolean GreaterThan(SqlSingle x, SqlSingle y)
\r
241 if (x.IsNull || y.IsNull)
\r
242 return SqlBoolean.Null;
\r
244 if (x._value > y._value)
\r
245 return SqlBoolean.True;
\r
247 return SqlBoolean.False;
\r
251 * Compares two instances of SqlSingle to determine if the first is greater than or equal to the second.
\r
252 * @param x A SqlSingle instance
\r
253 * @param y A SqlSingle instance
\r
254 * @return A SqlBoolean that is True if the first instance is greaater than or equal to the second instance, otherwise False.
\r
255 * If either instance of SqlSingle is null, the Value of the SqlBoolean will be Null.
\r
257 public static SqlBoolean GreaterThanOrEqual(SqlSingle x, SqlSingle y)
\r
259 if (x.IsNull || y.IsNull)
\r
260 return SqlBoolean.Null;
\r
262 if (x._value >= y._value)
\r
263 return SqlBoolean.True;
\r
265 return SqlBoolean.False;
\r
269 * Compares two instances of SqlSingle to determine if the first is less than the second.
\r
270 * @param x A SqlSingle instance
\r
271 * @param y A SqlSingle instance
\r
272 * @return A SqlBoolean that is True if the first instance is less than the second instance, otherwise False.
\r
273 * If either instance of SqlSingle is null, the Value of the SqlBoolean will be Null.
\r
275 public static SqlBoolean LessThan(SqlSingle x, SqlSingle y)
\r
277 if (x.IsNull || y.IsNull)
\r
278 return SqlBoolean.Null;
\r
280 if (x._value < y._value)
\r
281 return SqlBoolean.True;
\r
283 return SqlBoolean.False;
\r
287 * Compares two instances of SqlSingle to determine if the first is less than or equal to the second.
\r
288 * @param x A SqlSingle instance
\r
289 * @param y A SqlSingle instance
\r
290 * @return A SqlBoolean that is True if the first instance is less than or equal to the second instance, otherwise False.
\r
291 * If either instance of SqlSingle is null, the Value of the SqlBoolean will be Null.
\r
293 public static SqlBoolean LessThanOrEqual(SqlSingle x, SqlSingle y)
\r
295 if (x.IsNull || y.IsNull)
\r
296 return SqlBoolean.Null;
\r
298 if (x._value <= y._value)
\r
299 return SqlBoolean.True;
\r
301 return SqlBoolean.False;
\r
305 * The multiplication operator computes the product of the two SqlSingle operands.
\r
306 * @param x A SqlSingle instance
\r
307 * @param y A SqlSingle instance
\r
308 * @return The product of the two SqlSingle operands.
\r
310 public static SqlSingle Multiply(SqlSingle x, SqlSingle y)
\r
312 if (x.IsNull || y.IsNull)
\r
313 return SqlSingle.Null;
\r
315 float xVal = x._value;
\r
316 float yVal = y._value;
\r
318 if (java.lang.Math.abs(xVal) > 1 && java.lang.Math.abs(yVal) > 1)
\r
320 if (xVal < 0 && yVal < 0 && (_maxVal / xVal > yVal))
\r
321 throw new System.OverflowException("Overflow - " + x + " * " + y + " > " + _maxVal);
\r
322 if (xVal < 0 && yVal > 0 && (_minVal / xVal < yVal))
\r
323 throw new System.OverflowException("Overflow - " + x + " * " + y + " < " + _minVal);
\r
324 if (xVal > 0 && yVal < 0 && (_minVal / xVal > yVal))
\r
325 throw new System.OverflowException("Overflow - " + x + " * " + y + " < " + _minVal);
\r
326 if (xVal > 0 && yVal > 0 && (_maxVal / xVal < yVal))
\r
327 throw new System.OverflowException("Overflow - " + x + " * " + y + " > " + _maxVal);
\r
330 return new SqlSingle(xVal * yVal);
\r
335 * Compares two instances of SqlSingle to determine if they are equal.
\r
336 * @param x A SqlSingle instance
\r
337 * @param y A SqlSingle instance
\r
338 * @return A SqlBoolean that is True if the two instances are not equal or False if the two instances are equal.
\r
339 * If either instance of SqlSingle is null, the Value of the SqlBoolean will be Null.
\r
341 public static SqlBoolean NotEquals(SqlSingle x, SqlSingle y)
\r
343 SqlBoolean res = Equals(x, y);
\r
348 return SqlBoolean.True;
\r
350 return SqlBoolean.False;
\r
354 * Converts the String representation of a number to its float-precision floating point number equivalent.
\r
355 * @param s The String to be parsed.
\r
356 * @return A SqlSingle containing the value represented by the String.
\r
358 public static SqlSingle Parse(String s)
\r
360 float d = Single.Parse(s);
\r
361 return new SqlSingle(d);
\r
365 * The subtraction operator the second SqlSingle operand from the first.
\r
366 * @param x A SqlSingle instance
\r
367 * @param y A SqlSingle instance
\r
368 * @return The results of the subtraction operation.
\r
370 public static SqlSingle Subtract(SqlSingle x, SqlSingle y)
\r
372 if (x.IsNull || y.IsNull)
\r
373 return SqlSingle.Null;
\r
375 float xVal = x._value;
\r
376 float yVal = y._value;
\r
378 if (xVal < 0 && yVal > 0 && (java.lang.Math.abs(_minVal - xVal) < yVal))
\r
379 throw new System.OverflowException("Overflow - " + x + " - " + y + " < " + _minVal);
\r
380 if (xVal > 0 && yVal < 0 && (_maxVal - xVal < java.lang.Math.abs(yVal)))
\r
381 throw new System.OverflowException("Overflow - " + x + " - " + y + " > " + _maxVal);
\r
384 return new SqlSingle(x._value - y._value);
\r
389 * Converts this SqlSingle structure to SqlBoolean.
\r
390 * @return A SqlBoolean structure whose Value will be True if the SqlSingle structure's Value is non-zero, False if the SqlSingle is zero
\r
391 * and Null if the SqlSingle structure is Null.
\r
393 public SqlBoolean ToSqlBoolean()
\r
396 return new SqlBoolean(0);
\r
398 return new SqlBoolean(1);
\r
402 * Converts this SqlSingle structure to SqlByte.
\r
403 * @return A SqlByte structure whose Value equals the Value of this SqlSingle structure.
\r
405 public SqlByte ToSqlByte()
\r
407 int val = (int)_value;
\r
409 if (val < 0 || val > 255)
\r
410 throw new OverflowException("Can not conver this instance to SqlByte - overflowing : " + val);
\r
412 return new SqlByte((byte)val);
\r
416 * Converts this SqlSingle structure to SqlDouble.
\r
417 * @return A SqlDouble structure whose Value equals the Value of this SqlSingle structure.
\r
419 public SqlDouble ToSqlDouble()
\r
421 return new SqlDouble(_value);
\r
425 * Converts this SqlSingle structure to SqlDecimal.
\r
426 * @return A SqlDecimal structure whose Value equals the Value of this SqlSingle structure.
\r
428 public SqlDecimal ToSqlDecimal()
\r
430 return new SqlDecimal(_value);
\r
434 * Converts this SqlSingle structure to SqlInt16.
\r
435 * @return A SqlInt16 structure whose Value equals the Value of this SqlSingle structure.
\r
437 public SqlInt16 ToSqlInt16()
\r
439 return new SqlInt16((short)_value);
\r
443 * Converts this SqlSingle structure to SqlInt32.
\r
444 * @return A SqlInt32 structure whose Value equals the Value of this SqlSingle structure.
\r
446 public SqlInt32 ToSqlInt32()
\r
448 return new SqlInt32((int)_value);
\r
452 * Converts this SqlSingle structure to SqlInt64.
\r
453 * @return A SqlInt64 structure whose Value equals the Value of this SqlSingle structure.
\r
455 public SqlInt64 ToSqlInt64()
\r
457 return new SqlInt64((long)_value);
\r
461 * Converts this SqlSingle instance to SqlSingle.
\r
462 * @return A SqlMoney instance whose Value equals the Value of this SqlSingle instance.
\r
464 public SqlMoney ToSqlMoney()
\r
466 return new SqlMoney(_value);
\r
470 * Converts this SqlSingle structure to SqlString.
\r
471 * @return A SqlString structure whose value is a string representing the date and time contained in this SqlSingle structure.
\r
473 public SqlString ToSqlString()
\r
475 return new SqlString(ToString());
\r
479 public override String ToString()
\r
484 return _value.ToString();
\r
487 public override int GetHashCode()
\r
489 return _value.GetHashCode();
\r
492 public static SqlSingle operator + (SqlSingle x, SqlSingle y)
\r
494 float f = (float)(x.Value + y.Value);
\r
496 if (Single.IsInfinity (f))
\r
497 throw new OverflowException ();
\r
499 return new SqlSingle (f);
\r
502 public static SqlSingle operator / (SqlSingle x, SqlSingle y)
\r
504 float f = (float)(x.Value / y.Value);
\r
506 if (Single.IsInfinity (f))
\r
509 if (y.Value == 0d)
\r
510 throw new DivideByZeroException ();
\r
513 return new SqlSingle (x.Value / y.Value);
\r
516 public static SqlBoolean operator == (SqlSingle x, SqlSingle y)
\r
518 if (x.IsNull || y .IsNull)
\r
519 return SqlBoolean.Null;
\r
520 return new SqlBoolean (x.Value == y.Value);
\r
523 public static SqlBoolean operator > (SqlSingle x, SqlSingle y)
\r
525 if (x.IsNull || y .IsNull)
\r
526 return SqlBoolean.Null;
\r
527 return new SqlBoolean (x.Value > y.Value);
\r
530 public static SqlBoolean operator >= (SqlSingle x, SqlSingle y)
\r
532 if (x.IsNull || y .IsNull)
\r
533 return SqlBoolean.Null;
\r
534 return new SqlBoolean (x.Value >= y.Value);
\r
537 public static SqlBoolean operator != (SqlSingle x, SqlSingle y)
\r
539 if (x.IsNull || y .IsNull)
\r
540 return SqlBoolean.Null;
\r
541 return new SqlBoolean (!(x.Value == y.Value));
\r
544 public static SqlBoolean operator < (SqlSingle x, SqlSingle y)
\r
546 if (x.IsNull || y .IsNull)
\r
547 return SqlBoolean.Null;
\r
548 return new SqlBoolean (x.Value < y.Value);
\r
551 public static SqlBoolean operator <= (SqlSingle x, SqlSingle y)
\r
553 if (x.IsNull || y .IsNull)
\r
554 return SqlBoolean.Null;
\r
555 return new SqlBoolean (x.Value <= y.Value);
\r
558 public static SqlSingle operator * (SqlSingle x, SqlSingle y)
\r
560 float f = (float)(x.Value * y.Value);
\r
562 if (Single.IsInfinity (f))
\r
563 throw new OverflowException ();
\r
565 return new SqlSingle (f);
\r
568 public static SqlSingle operator - (SqlSingle x, SqlSingle y)
\r
570 float f = (float)(x.Value - y.Value);
\r
572 if (Single.IsInfinity (f))
\r
573 throw new OverflowException ();
\r
575 return new SqlSingle (f);
\r
578 public static SqlSingle operator - (SqlSingle n)
\r
580 return new SqlSingle (-(n.Value));
\r
583 public static explicit operator SqlSingle (SqlBoolean x)
\r
590 return new SqlSingle((float)x.ByteValue);
\r
594 public static explicit operator SqlSingle (SqlDouble x)
\r
599 float f = (float)x.Value;
\r
601 if (Single.IsInfinity (f))
\r
602 throw new OverflowException ();
\r
604 return new SqlSingle(f);
\r
607 public static explicit operator float (SqlSingle x)
\r
612 public static explicit operator SqlSingle (SqlString x)
\r
619 return SqlSingle.Parse (x.Value);
\r
623 public static implicit operator SqlSingle (float x)
\r
625 return new SqlSingle (x);
\r
628 public static implicit operator SqlSingle (SqlByte x)
\r
633 return new SqlSingle((float)x.Value);
\r
636 public static implicit operator SqlSingle (SqlDecimal x)
\r
641 return new SqlSingle((float)x.Value);
\r
644 public static implicit operator SqlSingle (SqlInt16 x)
\r
649 return new SqlSingle((float)x.Value);
\r
652 public static implicit operator SqlSingle (SqlInt32 x)
\r
657 return new SqlSingle((float)x.Value);
\r
660 public static implicit operator SqlSingle (SqlInt64 x)
\r
665 return new SqlSingle((float)x.Value);
\r
668 public static implicit operator SqlSingle (SqlMoney x)
\r
673 return new SqlSingle((float)x.Value);
\r