1 // System.Data.SqlTypes.SqlDouble
\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 SqlDouble : INullable, IComparable
\r
48 private Double _value;
\r
49 private bool _isNull;
\r
51 //private static double _maxVal = 1.79E+308;
\r
52 //private static double _minVal = -1.79E+308;
\r
54 public static readonly SqlDouble MaxValue = new SqlDouble(1.79E+308);
\r
55 public static readonly SqlDouble MinValue = new SqlDouble(-1.79E+308);
\r
56 public static readonly SqlDouble Null = new SqlDouble(true);
\r
57 public static readonly SqlDouble Zero = new SqlDouble(0.0);
\r
60 private SqlDouble(bool isNull)
\r
68 * @param value A double whose value will be used for the new SqlDouble.
\r
70 public SqlDouble(double 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 SqlDouble 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 SqlDouble operands.
\r
105 * @param x A SqlDouble structure.
\r
106 * @param y A SqlDouble structure.
\r
107 * @return The sum of the two SqlDouble operands.
\r
108 * If one of the parameters is null or null value - return SqlDouble.Null.
\r
110 public static SqlDouble Add(SqlDouble x, SqlDouble y)
\r
112 if (x.IsNull || y.IsNull)
\r
113 return SqlDouble.Null;
\r
115 double xVal = x._value;
\r
116 double yVal = y._value;
\r
118 return new SqlDouble(checked(xVal + yVal));
\r
122 * Compares this instance to the supplied object and returns an indication of their relative values.
\r
123 * @param obj The object to compare.
\r
124 * @return A signed number indicating the relative values of the instance and the object.
\r
125 * Less than zero This instance is less than object.
\r
126 * Zero This instance is the same as object.
\r
127 * Greater than zero This instance is greater than object -or-
\r
128 * object is a null reference.
\r
130 public int CompareTo(Object obj)
\r
135 if (obj is SqlDouble)
\r
137 SqlDouble d = (SqlDouble)obj;
\r
144 return this._value.CompareTo(d._value);
\r
147 throw new ArgumentException("parameter obj is not SqlDouble : " + obj.GetType().Name);
\r
152 * The division operator divides the first SqlDouble operand by the second.
\r
153 * @param x A SqlDouble instance.
\r
154 * @param y A SqlDouble instance.
\r
155 * @return A SqlDouble structure containing the results of the division operation.
\r
156 * If one of the parameters is null or null value - return SqlDouble.Null.
\r
158 public static SqlDouble Divide(SqlDouble x, SqlDouble y)
\r
161 if (x.IsNull || y.IsNull)
\r
162 return SqlDouble.Null;
\r
164 double xVal = x._value;
\r
165 double yVal = y._value;
\r
167 return new SqlDouble(checked(xVal / yVal));
\r
171 public override bool Equals(Object obj)
\r
177 if (obj is SqlDouble)
\r
179 SqlDouble d = (SqlDouble)obj;
\r
181 if (IsNull && d.IsNull)
\r
184 if (IsNull || d.IsNull)
\r
187 return _value == d._value;
\r
194 * Performs a logical comparison on two instances of SqlDouble to determine if they are equal.
\r
195 * @param x A SqlDouble instance.
\r
196 * @param y A SqlDouble instance.
\r
197 * @return true if the two values are equal, otherwise false.
\r
198 * If one of the parameters is null or null value return SqlBoolean.Null.
\r
200 public static SqlBoolean Equals(SqlDouble x, SqlDouble y)
\r
202 if (x.IsNull || y.IsNull)
\r
203 return SqlBoolean.Null;
\r
206 return SqlBoolean.True;
\r
208 return SqlBoolean.False;
\r
212 * Compares two instances of SqlDouble to determine if the first is greater than the second.
\r
213 * @param x A SqlDouble instance
\r
214 * @param y A SqlDouble instance
\r
215 * @return A SqlBoolean that is True if the first instance is greater than the second instance, otherwise False.
\r
216 * If either instance of SqlDouble is null, the Value of the SqlBoolean will be Null.
\r
218 public static SqlBoolean GreaterThan(SqlDouble x, SqlDouble y)
\r
220 if (x.IsNull || y.IsNull)
\r
221 return SqlBoolean.Null;
\r
223 if (x._value > y._value)
\r
224 return SqlBoolean.True;
\r
226 return SqlBoolean.False;
\r
230 * Compares two instances of SqlDouble to determine if the first is greater than or equal to the second.
\r
231 * @param x A SqlDouble instance
\r
232 * @param y A SqlDouble instance
\r
233 * @return A SqlBoolean that is True if the first instance is greaater than or equal to the second instance, otherwise False.
\r
234 * If either instance of SqlDouble is null, the Value of the SqlBoolean will be Null.
\r
236 public static SqlBoolean GreaterThanOrEqual(SqlDouble x, SqlDouble y)
\r
238 if (x.IsNull || y.IsNull)
\r
239 return SqlBoolean.Null;
\r
241 if (x._value >= y._value)
\r
242 return SqlBoolean.True;
\r
244 return SqlBoolean.False;
\r
248 * Compares two instances of SqlDouble to determine if the first is less than the second.
\r
249 * @param x A SqlDouble instance
\r
250 * @param y A SqlDouble instance
\r
251 * @return A SqlBoolean that is True if the first instance is less than the second instance, otherwise False.
\r
252 * If either instance of SqlDouble is null, the Value of the SqlBoolean will be Null.
\r
254 public static SqlBoolean LessThan(SqlDouble x, SqlDouble y)
\r
256 if (x.IsNull || y.IsNull)
\r
257 return SqlBoolean.Null;
\r
259 if (x._value < y._value)
\r
260 return SqlBoolean.True;
\r
262 return SqlBoolean.False;
\r
266 * Compares two instances of SqlDouble to determine if the first is less than or equal to the second.
\r
267 * @param x A SqlDouble instance
\r
268 * @param y A SqlDouble instance
\r
269 * @return A SqlBoolean that is True if the first instance is less than or equal to the second instance, otherwise False.
\r
270 * If either instance of SqlDouble is null, the Value of the SqlBoolean will be Null.
\r
272 public static SqlBoolean LessThanOrEqual(SqlDouble x, SqlDouble y)
\r
274 if (x.IsNull || y.IsNull)
\r
275 return SqlBoolean.Null;
\r
277 if (x._value <= y._value)
\r
278 return SqlBoolean.True;
\r
280 return SqlBoolean.False;
\r
284 * The multiplication operator computes the product of the two SqlDouble operands.
\r
285 * @param x A SqlDouble instance
\r
286 * @param y A SqlDouble instance
\r
287 * @return The product of the two SqlDouble operands.
\r
289 public static SqlDouble Multiply(SqlDouble x, SqlDouble y)
\r
291 if (x.IsNull || y.IsNull)
\r
292 return SqlDouble.Null;
\r
294 double xVal = x._value;
\r
295 double yVal = y._value;
\r
297 return new SqlDouble(checked(xVal * yVal));
\r
302 * Compares two instances of SqlDouble to determine if they are equal.
\r
303 * @param x A SqlDouble instance
\r
304 * @param y A SqlDouble instance
\r
305 * @return A SqlBoolean that is True if the two instances are not equal or False if the two instances are equal.
\r
306 * If either instance of SqlDouble is null, the Value of the SqlBoolean will be Null.
\r
308 public static SqlBoolean NotEquals(SqlDouble x, SqlDouble y)
\r
310 SqlBoolean res = Equals(x, y);
\r
315 return SqlBoolean.True;
\r
317 return SqlBoolean.False;
\r
321 * Converts the String representation of a number to its double-precision floating point number equivalent.
\r
322 * @param s The String to be parsed.
\r
323 * @return A SqlDouble containing the value represented by the String.
\r
325 public static SqlDouble Parse(String s)
\r
327 double d = double.Parse(s);
\r
328 return new SqlDouble(d);
\r
332 * The subtraction operator the second SqlDouble operand from the first.
\r
333 * @param x A SqlDouble instance
\r
334 * @param y A SqlDouble instance
\r
335 * @return The results of the subtraction operation.
\r
337 public static SqlDouble Subtract(SqlDouble x, SqlDouble y)
\r
339 if (x.IsNull || y.IsNull)
\r
340 return SqlDouble.Null;
\r
342 double xVal = x._value;
\r
343 double yVal = y._value;
\r
345 return new SqlDouble(checked(xVal - yVal));
\r
349 * Converts this SqlDouble structure to SqlBoolean.
\r
350 * @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
351 * and Null if the SqlDouble structure is Null.
\r
353 public SqlBoolean ToSqlBoolean()
\r
356 return SqlBoolean.Null;
\r
359 return new SqlBoolean(0);
\r
361 return new SqlBoolean(1);
\r
365 * Converts this SqlDouble structure to SqlByte.
\r
366 * @return A SqlByte structure whose Value equals the Value of this SqlDouble structure.
\r
368 public SqlByte ToSqlByte()
\r
371 return SqlByte.Null;
\r
373 return new SqlByte(checked((byte)_value));
\r
377 * Converts this SqlDouble structure to SqlDecimal.
\r
378 * @return A SqlDecimal structure whose Value equals the Value of this SqlDouble structure.
\r
380 public SqlDecimal ToSqlDecimal()
\r
383 return SqlDecimal.Null;
\r
385 return new SqlDecimal(_value);
\r
389 * Converts this SqlDouble structure to SqlInt16.
\r
390 * @return A SqlInt16 structure whose Value equals the Value of this SqlDouble structure.
\r
392 public SqlInt16 ToSqlInt16()
\r
395 return SqlInt16.Null;
\r
397 return new SqlInt16(checked((short)_value));
\r
401 * Converts this SqlDouble structure to SqlInt32.
\r
402 * @return A SqlInt32 structure whose Value equals the Value of this SqlDouble structure.
\r
404 public SqlInt32 ToSqlInt32()
\r
407 return SqlInt32.Null;
\r
409 return new SqlInt32(checked((int)_value));
\r
413 * Converts this SqlDouble structure to SqlInt64.
\r
414 * @return A SqlInt64 structure whose Value equals the Value of this SqlDouble structure.
\r
416 public SqlInt64 ToSqlInt64()
\r
419 return SqlInt64.Null;
\r
421 return new SqlInt64(checked((long)_value));
\r
425 * Converts this SqlDouble instance to SqlDouble.
\r
426 * @return A SqlMoney instance whose Value equals the Value of this SqlDouble instance.
\r
428 public SqlMoney ToSqlMoney()
\r
431 return SqlMoney.Null;
\r
433 return new SqlMoney(_value);
\r
437 * Converts this SqlMoney instance to SqlSingle.
\r
438 * @return A SqlSingle instance whose Value equals the Value of this SqlMoney instance.
\r
440 public SqlSingle ToSqlSingle()
\r
443 return SqlSingle.Null;
\r
445 return new SqlSingle(checked((float)_value));
\r
450 * Converts this SqlDouble structure to SqlString.
\r
451 * @return A SqlString structure whose value is a string representing the date and time contained in this SqlDouble structure.
\r
453 public SqlString ToSqlString()
\r
455 return new SqlString(ToString());
\r
458 public override String ToString()
\r
464 return _value.ToString();
\r
467 public override int GetHashCode()
\r
469 return _value.GetHashCode();
\r
472 public static SqlDouble operator + (SqlDouble x, SqlDouble y)
\r
475 d = x.Value + y.Value;
\r
477 if (Double.IsInfinity (d))
\r
478 throw new OverflowException ();
\r
480 return new SqlDouble (d);
\r
483 public static SqlDouble operator / (SqlDouble x, SqlDouble y)
\r
485 double d = x.Value / y.Value;
\r
487 if (Double.IsInfinity (d))
\r
490 throw new DivideByZeroException ();
\r
493 return new SqlDouble (d);
\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 SqlBoolean operator <= (SqlDouble x, SqlDouble y)
\r
538 if (x.IsNull || y.IsNull)
\r
539 return SqlBoolean.Null;
\r
541 return new SqlBoolean (x.Value <= y.Value);
\r
544 public static SqlDouble operator * (SqlDouble x, SqlDouble y)
\r
546 double d = x.Value * y.Value;
\r
548 if (Double.IsInfinity (d))
\r
549 throw new OverflowException ();
\r
551 return new SqlDouble (d);
\r
555 public static SqlDouble operator - (SqlDouble x, SqlDouble y)
\r
557 double d = x.Value - y.Value;
\r
559 if (Double.IsInfinity (d))
\r
560 throw new OverflowException ();
\r
562 return new SqlDouble (d);
\r
565 public static SqlDouble operator - (SqlDouble n)
\r
567 return new SqlDouble (-(n.Value));
\r
570 public static explicit operator SqlDouble (SqlBoolean x)
\r
575 return new SqlDouble ((double)x.ByteValue);
\r
578 public static explicit operator double (SqlDouble x)
\r
583 public static explicit operator SqlDouble (SqlString x)
\r
587 return SqlDouble.Parse (x.Value);
\r
591 public static implicit operator SqlDouble (double x)
\r
593 return new SqlDouble (x);
\r
596 public static implicit operator SqlDouble (SqlByte x)
\r
601 return new SqlDouble ((double)x.Value);
\r
604 public static implicit operator SqlDouble (SqlDecimal x)
\r
609 return new SqlDouble (x.ToDouble ());
\r
612 public static implicit operator SqlDouble (SqlInt16 x)
\r
617 return new SqlDouble ((double)x.Value);
\r
620 public static implicit operator SqlDouble (SqlInt32 x)
\r
625 return new SqlDouble ((double)x.Value);
\r
628 public static implicit operator SqlDouble (SqlInt64 x)
\r
633 return new SqlDouble ((double)x.Value);
\r
636 public static implicit operator SqlDouble (SqlMoney x)
\r
641 return new SqlDouble ((double)x.Value);
\r
644 public static implicit operator SqlDouble (SqlSingle x)
\r
649 return new SqlDouble ((double)x.Value);
\r