2 * Copyright (c) 2002-2004 Mainsoft Corporation.
\r
4 * Permission is hereby granted, free of charge, to any person obtaining a
\r
5 * copy of this software and associated documentation files (the "Software"),
\r
6 * to deal in the Software without restriction, including without limitation
\r
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
\r
8 * and/or sell copies of the Software, and to permit persons to whom the
\r
9 * Software is furnished to do so, subject to the following conditions:
\r
11 * The above copyright notice and this permission notice shall be included in
\r
12 * all copies or substantial portions of the Software.
\r
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
\r
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
\r
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
\r
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
\r
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
\r
20 * DEALINGS IN THE SOFTWARE.
\r
23 namespace System.Data.SqlTypes
\r
28 * <p>Description: </p>
\r
29 * <p>Copyright: Copyright (c) 2002</p>
\r
30 * <p>Company: MainSoft</p>
\r
31 * @author Pavel Sandler
\r
37 public struct SqlDouble : INullable, IComparable
\r
40 private Double _value;
\r
41 private bool _isNull;
\r
43 //private static double _maxVal = 1.79E+308;
\r
44 //private static double _minVal = -1.79E+308;
\r
46 public static readonly SqlDouble MaxValue = new SqlDouble(1.79E+308);
\r
47 public static readonly SqlDouble MinValue = new SqlDouble(-1.79E+308);
\r
48 public static readonly SqlDouble Null = new SqlDouble(true);
\r
49 public static readonly SqlDouble Zero = new SqlDouble(0.0);
\r
52 private SqlDouble(bool isNull)
\r
60 * @param value A double whose value will be used for the new SqlDouble.
\r
62 public SqlDouble(double value)
\r
70 * Indicates whether or not Value is null.
\r
71 * @return true if Value is null, otherwise false.
\r
82 * Gets the value of the SqlDouble instance.
\r
83 * @return the value of this instance
\r
90 throw new SqlNullValueException();
\r
96 * The addition operator computes the sum of the two SqlDouble operands.
\r
97 * @param x A SqlDouble structure.
\r
98 * @param y A SqlDouble structure.
\r
99 * @return The sum of the two SqlDouble operands.
\r
100 * If one of the parameters is null or null value - return SqlDouble.Null.
\r
102 public static SqlDouble Add(SqlDouble x, SqlDouble y)
\r
104 if (x.IsNull || y.IsNull)
\r
105 return SqlDouble.Null;
\r
107 double xVal = x._value;
\r
108 double yVal = y._value;
\r
110 return new SqlDouble(checked(xVal + yVal));
\r
114 * Compares this instance to the supplied object and returns an indication of their relative values.
\r
115 * @param obj The object to compare.
\r
116 * @return A signed number indicating the relative values of the instance and the object.
\r
117 * Less than zero This instance is less than object.
\r
118 * Zero This instance is the same as object.
\r
119 * Greater than zero This instance is greater than object -or-
\r
120 * object is a null reference.
\r
122 public int CompareTo(Object obj)
\r
127 if (obj is SqlDouble)
\r
129 SqlDouble d = (SqlDouble)obj;
\r
136 return this._value.CompareTo(d._value);
\r
139 throw new ArgumentException("parameter obj is not SqlDouble : " + obj.GetType().Name);
\r
144 * The division operator divides the first SqlDouble operand by the second.
\r
145 * @param x A SqlDouble instance.
\r
146 * @param y A SqlDouble instance.
\r
147 * @return A SqlDouble structure containing the results of the division operation.
\r
148 * If one of the parameters is null or null value - return SqlDouble.Null.
\r
150 public static SqlDouble Divide(SqlDouble x, SqlDouble y)
\r
153 if (x.IsNull || y.IsNull)
\r
154 return SqlDouble.Null;
\r
156 double xVal = x._value;
\r
157 double yVal = y._value;
\r
159 return new SqlDouble(checked(xVal / yVal));
\r
163 public override bool Equals(Object obj)
\r
169 if (obj is SqlDouble)
\r
171 SqlDouble d = (SqlDouble)obj;
\r
173 if (IsNull && d.IsNull)
\r
176 if (IsNull || d.IsNull)
\r
179 return _value == d._value;
\r
186 * Performs a logical comparison on two instances of SqlDouble to determine if they are equal.
\r
187 * @param x A SqlDouble instance.
\r
188 * @param y A SqlDouble instance.
\r
189 * @return true if the two values are equal, otherwise false.
\r
190 * If one of the parameters is null or null value return SqlBoolean.Null.
\r
192 public static SqlBoolean Equals(SqlDouble x, SqlDouble y)
\r
194 if (x.IsNull || y.IsNull)
\r
195 return SqlBoolean.Null;
\r
198 return SqlBoolean.True;
\r
200 return SqlBoolean.False;
\r
204 * Compares two instances of SqlDouble to determine if the first is greater than the second.
\r
205 * @param x A SqlDouble instance
\r
206 * @param y A SqlDouble instance
\r
207 * @return A SqlBoolean that is True if the first instance is greater than the second instance, otherwise False.
\r
208 * If either instance of SqlDouble is null, the Value of the SqlBoolean will be Null.
\r
210 public static SqlBoolean GreaterThan(SqlDouble x, SqlDouble y)
\r
212 if (x.IsNull || y.IsNull)
\r
213 return SqlBoolean.Null;
\r
215 if (x._value > y._value)
\r
216 return SqlBoolean.True;
\r
218 return SqlBoolean.False;
\r
222 * Compares two instances of SqlDouble to determine if the first is greater than or equal to the second.
\r
223 * @param x A SqlDouble instance
\r
224 * @param y A SqlDouble instance
\r
225 * @return A SqlBoolean that is True if the first instance is greaater than or equal to the second instance, otherwise False.
\r
226 * If either instance of SqlDouble is null, the Value of the SqlBoolean will be Null.
\r
228 public static SqlBoolean GreaterThanOrEqual(SqlDouble x, SqlDouble y)
\r
230 if (x.IsNull || y.IsNull)
\r
231 return SqlBoolean.Null;
\r
233 if (x._value >= y._value)
\r
234 return SqlBoolean.True;
\r
236 return SqlBoolean.False;
\r
240 * Compares two instances of SqlDouble to determine if the first is less than the second.
\r
241 * @param x A SqlDouble instance
\r
242 * @param y A SqlDouble instance
\r
243 * @return A SqlBoolean that is True if the first instance is less than the second instance, otherwise False.
\r
244 * If either instance of SqlDouble is null, the Value of the SqlBoolean will be Null.
\r
246 public static SqlBoolean LessThan(SqlDouble x, SqlDouble y)
\r
248 if (x.IsNull || y.IsNull)
\r
249 return SqlBoolean.Null;
\r
251 if (x._value < y._value)
\r
252 return SqlBoolean.True;
\r
254 return SqlBoolean.False;
\r
258 * Compares two instances of SqlDouble to determine if the first is less than or equal to the second.
\r
259 * @param x A SqlDouble instance
\r
260 * @param y A SqlDouble instance
\r
261 * @return A SqlBoolean that is True if the first instance is less than or equal to the second instance, otherwise False.
\r
262 * If either instance of SqlDouble is null, the Value of the SqlBoolean will be Null.
\r
264 public static SqlBoolean LessThanOrEqual(SqlDouble x, SqlDouble y)
\r
266 if (x.IsNull || y.IsNull)
\r
267 return SqlBoolean.Null;
\r
269 if (x._value <= y._value)
\r
270 return SqlBoolean.True;
\r
272 return SqlBoolean.False;
\r
276 * The multiplication operator computes the product of the two SqlDouble operands.
\r
277 * @param x A SqlDouble instance
\r
278 * @param y A SqlDouble instance
\r
279 * @return The product of the two SqlDouble operands.
\r
281 public static SqlDouble Multiply(SqlDouble x, SqlDouble y)
\r
283 if (x.IsNull || y.IsNull)
\r
284 return SqlDouble.Null;
\r
286 double xVal = x._value;
\r
287 double yVal = y._value;
\r
289 return new SqlDouble(checked(xVal * yVal));
\r
294 * Compares two instances of SqlDouble to determine if they are equal.
\r
295 * @param x A SqlDouble instance
\r
296 * @param y A SqlDouble instance
\r
297 * @return A SqlBoolean that is True if the two instances are not equal or False if the two instances are equal.
\r
298 * If either instance of SqlDouble is null, the Value of the SqlBoolean will be Null.
\r
300 public static SqlBoolean NotEquals(SqlDouble x, SqlDouble y)
\r
302 SqlBoolean res = Equals(x, y);
\r
307 return SqlBoolean.True;
\r
309 return SqlBoolean.False;
\r
313 * Converts the String representation of a number to its double-precision floating point number equivalent.
\r
314 * @param s The String to be parsed.
\r
315 * @return A SqlDouble containing the value represented by the String.
\r
317 public static SqlDouble Parse(String s)
\r
319 double d = double.Parse(s);
\r
320 return new SqlDouble(d);
\r
324 * The subtraction operator the second SqlDouble operand from the first.
\r
325 * @param x A SqlDouble instance
\r
326 * @param y A SqlDouble instance
\r
327 * @return The results of the subtraction operation.
\r
329 public static SqlDouble Subtract(SqlDouble x, SqlDouble y)
\r
331 if (x.IsNull || y.IsNull)
\r
332 return SqlDouble.Null;
\r
334 double xVal = x._value;
\r
335 double yVal = y._value;
\r
337 return new SqlDouble(checked(xVal - yVal));
\r
341 * Converts this SqlDouble structure to SqlBoolean.
\r
342 * @return A SqlBoolean structure whose Value will be True if the SqlDouble structure's Value is non-zero, False if the SqlDouble is zero
\r
343 * and Null if the SqlDouble structure is Null.
\r
345 public SqlBoolean ToSqlBoolean()
\r
348 return SqlBoolean.Null;
\r
351 return new SqlBoolean(0);
\r
353 return new SqlBoolean(1);
\r
357 * Converts this SqlDouble structure to SqlByte.
\r
358 * @return A SqlByte structure whose Value equals the Value of this SqlDouble structure.
\r
360 public SqlByte ToSqlByte()
\r
363 return SqlByte.Null;
\r
365 return new SqlByte(checked((byte)_value));
\r
369 * Converts this SqlDouble structure to SqlDecimal.
\r
370 * @return A SqlDecimal structure whose Value equals the Value of this SqlDouble structure.
\r
372 public SqlDecimal ToSqlDecimal()
\r
375 return SqlDecimal.Null;
\r
377 return new SqlDecimal(_value);
\r
381 * Converts this SqlDouble structure to SqlInt16.
\r
382 * @return A SqlInt16 structure whose Value equals the Value of this SqlDouble structure.
\r
384 public SqlInt16 ToSqlInt16()
\r
387 return SqlInt16.Null;
\r
389 return new SqlInt16(checked((short)_value));
\r
393 * Converts this SqlDouble structure to SqlInt32.
\r
394 * @return A SqlInt32 structure whose Value equals the Value of this SqlDouble structure.
\r
396 public SqlInt32 ToSqlInt32()
\r
399 return SqlInt32.Null;
\r
401 return new SqlInt32(checked((int)_value));
\r
405 * Converts this SqlDouble structure to SqlInt64.
\r
406 * @return A SqlInt64 structure whose Value equals the Value of this SqlDouble structure.
\r
408 public SqlInt64 ToSqlInt64()
\r
411 return SqlInt64.Null;
\r
413 return new SqlInt64(checked((long)_value));
\r
417 * Converts this SqlDouble instance to SqlDouble.
\r
418 * @return A SqlMoney instance whose Value equals the Value of this SqlDouble instance.
\r
420 public SqlMoney ToSqlMoney()
\r
423 return SqlMoney.Null;
\r
425 return new SqlMoney(_value);
\r
429 * Converts this SqlMoney instance to SqlSingle.
\r
430 * @return A SqlSingle instance whose Value equals the Value of this SqlMoney instance.
\r
432 public SqlSingle ToSqlSingle()
\r
435 return SqlSingle.Null;
\r
437 return new SqlSingle(checked((float)_value));
\r
442 * Converts this SqlDouble structure to SqlString.
\r
443 * @return A SqlString structure whose value is a string representing the date and time contained in this SqlDouble structure.
\r
445 public SqlString ToSqlString()
\r
447 return new SqlString(ToString());
\r
450 public override String ToString()
\r
456 return _value.ToString();
\r
459 public override int GetHashCode()
\r
461 return _value.GetHashCode();
\r
464 public static SqlDouble operator + (SqlDouble x, SqlDouble y)
\r
467 d = x.Value + y.Value;
\r
469 if (Double.IsInfinity (d))
\r
470 throw new OverflowException ();
\r
472 return new SqlDouble (d);
\r
475 public static SqlDouble operator / (SqlDouble x, SqlDouble y)
\r
477 double d = x.Value / y.Value;
\r
479 if (Double.IsInfinity (d))
\r
482 throw new DivideByZeroException ();
\r
485 return new SqlDouble (d);
\r
488 public static SqlBoolean operator == (SqlDouble x, SqlDouble y)
\r
490 if (x.IsNull || y.IsNull)
\r
491 return SqlBoolean.Null;
\r
493 return new SqlBoolean (x.Value == y.Value);
\r
496 public static SqlBoolean operator > (SqlDouble x, SqlDouble y)
\r
498 if (x.IsNull || y.IsNull)
\r
499 return SqlBoolean.Null;
\r
501 return new SqlBoolean (x.Value > y.Value);
\r
504 public static SqlBoolean operator >= (SqlDouble x, SqlDouble y)
\r
506 if (x.IsNull || y.IsNull)
\r
507 return SqlBoolean.Null;
\r
509 return new SqlBoolean (x.Value >= y.Value);
\r
512 public static SqlBoolean operator != (SqlDouble x, SqlDouble y)
\r
514 if (x.IsNull || y.IsNull)
\r
515 return SqlBoolean.Null;
\r
517 return new SqlBoolean (!(x.Value == y.Value));
\r
520 public static SqlBoolean operator < (SqlDouble x, SqlDouble y)
\r
522 if (x.IsNull || y.IsNull)
\r
523 return SqlBoolean.Null;
\r
525 return new SqlBoolean (x.Value < y.Value);
\r
528 public static SqlBoolean operator <= (SqlDouble x, SqlDouble y)
\r
530 if (x.IsNull || y.IsNull)
\r
531 return SqlBoolean.Null;
\r
533 return new SqlBoolean (x.Value <= y.Value);
\r
536 public static SqlDouble operator * (SqlDouble x, SqlDouble y)
\r
538 double d = x.Value * y.Value;
\r
540 if (Double.IsInfinity (d))
\r
541 throw new OverflowException ();
\r
543 return new SqlDouble (d);
\r
547 public static SqlDouble operator - (SqlDouble x, SqlDouble y)
\r
549 double d = x.Value - y.Value;
\r
551 if (Double.IsInfinity (d))
\r
552 throw new OverflowException ();
\r
554 return new SqlDouble (d);
\r
557 public static SqlDouble operator - (SqlDouble n)
\r
559 return new SqlDouble (-(n.Value));
\r
562 public static explicit operator SqlDouble (SqlBoolean x)
\r
567 return new SqlDouble ((double)x.ByteValue);
\r
570 public static explicit operator double (SqlDouble x)
\r
575 public static explicit operator SqlDouble (SqlString x)
\r
579 return SqlDouble.Parse (x.Value);
\r
583 public static implicit operator SqlDouble (double x)
\r
585 return new SqlDouble (x);
\r
588 public static implicit operator SqlDouble (SqlByte x)
\r
593 return new SqlDouble ((double)x.Value);
\r
596 public static implicit operator SqlDouble (SqlDecimal x)
\r
601 return new SqlDouble (x.ToDouble ());
\r
604 public static implicit operator SqlDouble (SqlInt16 x)
\r
609 return new SqlDouble ((double)x.Value);
\r
612 public static implicit operator SqlDouble (SqlInt32 x)
\r
617 return new SqlDouble ((double)x.Value);
\r
620 public static implicit operator SqlDouble (SqlInt64 x)
\r
625 return new SqlDouble ((double)x.Value);
\r
628 public static implicit operator SqlDouble (SqlMoney x)
\r
633 return new SqlDouble ((double)x.Value);
\r
636 public static implicit operator SqlDouble (SqlSingle x)
\r
641 return new SqlDouble ((double)x.Value);
\r