1 //------------------------------------------------------------------------------
2 // <copyright file="SqlDouble.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
5 // <owner current="true" primary="true">junfang</owner>
6 // <owner current="true" primary="false">Microsoft</owner>
7 // <owner current="true" primary="false">Microsoft</owner>
8 //------------------------------------------------------------------------------
10 //**************************************************************************
11 // @File: SqlDouble.cs
15 // Purpose: Implementation of SqlDouble which is equivalent to
16 // data type "float" in SQL Server
22 // 09/17/99 JunFang Created and implemented as first drop.
25 //**************************************************************************
28 using System.Data.Common;
29 using System.Runtime.InteropServices;
30 using System.Globalization;
32 using System.Xml.Schema;
33 using System.Xml.Serialization;
35 namespace System.Data.SqlTypes {
39 /// Represents a floating-point number within the range of
41 /// +308 through 1.79E +308 to be stored in or retrieved from
46 [StructLayout(LayoutKind.Sequential)]
47 [XmlSchemaProvider("GetXsdType")]
48 public struct SqlDouble : INullable, IComparable, IXmlSerializable {
50 private bool m_fNotNull; // false if null
51 private double m_value;
55 private SqlDouble(bool fNull) {
61 /// <para>[To be supplied.]</para>
63 public SqlDouble(double value) {
64 if (Double.IsInfinity(value) || Double.IsNaN(value))
65 throw new OverflowException(SQLResource.ArithOverflowMessage);
74 /// <para>[To be supplied.]</para>
77 get { return !m_fNotNull;}
82 /// <para>[To be supplied.]</para>
89 throw new SqlNullValueException();
93 // Implicit conversion from double to SqlDouble
95 /// <para>[To be supplied.]</para>
97 public static implicit operator SqlDouble(double x) {
98 return new SqlDouble(x);
101 // Explicit conversion from SqlDouble to double. Throw exception if x is Null.
103 /// <para>[To be supplied.]</para>
105 public static explicit operator double(SqlDouble x) {
110 /// <para>[To be supplied.]</para>
112 public override String ToString() {
113 return IsNull ? SQLResource.NullString : m_value.ToString((IFormatProvider)null);
117 /// <para>[To be supplied.]</para>
119 public static SqlDouble Parse(String s) {
120 if (s == SQLResource.NullString)
121 return SqlDouble.Null;
123 return new SqlDouble(Double.Parse(s, CultureInfo.InvariantCulture));
129 /// <para>[To be supplied.]</para>
131 public static SqlDouble operator -(SqlDouble x) {
132 return x.IsNull ? Null : new SqlDouble(-x.m_value);
138 // Arithmetic operators
140 /// <para>[To be supplied.]</para>
142 public static SqlDouble operator +(SqlDouble x, SqlDouble y) {
143 if (x.IsNull || y.IsNull)
146 double value = x.m_value + y.m_value;
148 if (Double.IsInfinity(value))
149 throw new OverflowException(SQLResource.ArithOverflowMessage);
151 return new SqlDouble(value);
155 /// <para>[To be supplied.]</para>
157 public static SqlDouble operator -(SqlDouble x, SqlDouble y) {
158 if (x.IsNull || y.IsNull)
161 double value = x.m_value - y.m_value;
163 if (Double.IsInfinity(value))
164 throw new OverflowException(SQLResource.ArithOverflowMessage);
166 return new SqlDouble(value);
170 /// <para>[To be supplied.]</para>
172 public static SqlDouble operator *(SqlDouble x, SqlDouble y) {
173 if (x.IsNull || y.IsNull)
176 double value = x.m_value * y.m_value;
178 if (Double.IsInfinity(value))
179 throw new OverflowException(SQLResource.ArithOverflowMessage);
181 return new SqlDouble(value);
185 /// <para>[To be supplied.]</para>
187 public static SqlDouble operator /(SqlDouble x, SqlDouble y) {
188 if (x.IsNull || y.IsNull)
191 if (y.m_value == (double)0.0)
192 throw new DivideByZeroException(SQLResource.DivideByZeroMessage);
194 double value = x.m_value / y.m_value;
196 if (Double.IsInfinity(value))
197 throw new OverflowException(SQLResource.ArithOverflowMessage);
199 return new SqlDouble(value);
204 // Implicit conversions
206 // Implicit conversion from SqlBoolean to SqlDouble
208 /// <para>[To be supplied.]</para>
210 public static explicit operator SqlDouble(SqlBoolean x) {
211 return x.IsNull ? Null : new SqlDouble((double)(x.ByteValue));
214 // Implicit conversion from SqlByte to SqlDouble
216 /// <para>[To be supplied.]</para>
218 public static implicit operator SqlDouble(SqlByte x) {
219 return x.IsNull ? Null : new SqlDouble((double)(x.Value));
222 // Implicit conversion from SqlInt16 to SqlDouble
224 /// <para>[To be supplied.]</para>
226 public static implicit operator SqlDouble(SqlInt16 x) {
227 return x.IsNull ? Null : new SqlDouble((double)(x.Value));
230 // Implicit conversion from SqlInt32 to SqlDouble
232 /// <para>[To be supplied.]</para>
234 public static implicit operator SqlDouble(SqlInt32 x) {
235 return x.IsNull ? Null : new SqlDouble((double)(x.Value));
238 // Implicit conversion from SqlInt64 to SqlDouble
240 /// <para>[To be supplied.]</para>
242 public static implicit operator SqlDouble(SqlInt64 x) {
243 return x.IsNull ? Null : new SqlDouble((double)(x.Value));
246 // Implicit conversion from SqlSingle to SqlDouble
248 /// <para>[To be supplied.]</para>
250 public static implicit operator SqlDouble(SqlSingle x) {
251 return x.IsNull ? Null : new SqlDouble((double)(x.Value));
254 // Implicit conversion from SqlMoney to SqlDouble
256 /// <para>[To be supplied.]</para>
258 public static implicit operator SqlDouble(SqlMoney x) {
259 return x.IsNull ? Null : new SqlDouble(x.ToDouble());
262 // Implicit conversion from SqlDecimal to SqlDouble
264 /// <para>[To be supplied.]</para>
266 public static implicit operator SqlDouble(SqlDecimal x) {
267 return x.IsNull ? Null : new SqlDouble(x.ToDouble());
271 // Explicit conversions
275 // Explicit conversion from SqlString to SqlDouble
276 // Throws FormatException or OverflowException if necessary.
278 /// <para>[To be supplied.]</para>
280 public static explicit operator SqlDouble(SqlString x) {
282 return SqlDouble.Null;
284 return Parse(x.Value);
287 // Overloading comparison operators
289 /// <para>[To be supplied.]</para>
291 public static SqlBoolean operator==(SqlDouble x, SqlDouble y) {
292 return(x.IsNull || y.IsNull) ? SqlBoolean.Null : new SqlBoolean(x.m_value == y.m_value);
296 /// <para>[To be supplied.]</para>
298 public static SqlBoolean operator!=(SqlDouble x, SqlDouble y) {
303 /// <para>[To be supplied.]</para>
305 public static SqlBoolean operator<(SqlDouble x, SqlDouble y) {
306 return(x.IsNull || y.IsNull) ? SqlBoolean.Null : new SqlBoolean(x.m_value < y.m_value);
310 /// <para>[To be supplied.]</para>
312 public static SqlBoolean operator>(SqlDouble x, SqlDouble y) {
313 return(x.IsNull || y.IsNull) ? SqlBoolean.Null : new SqlBoolean(x.m_value > y.m_value);
317 /// <para>[To be supplied.]</para>
319 public static SqlBoolean operator<=(SqlDouble x, SqlDouble y) {
320 return(x.IsNull || y.IsNull) ? SqlBoolean.Null : new SqlBoolean(x.m_value <= y.m_value);
324 /// <para>[To be supplied.]</para>
326 public static SqlBoolean operator>=(SqlDouble x, SqlDouble y) {
327 return(x.IsNull || y.IsNull) ? SqlBoolean.Null : new SqlBoolean(x.m_value >= y.m_value);
330 //--------------------------------------------------
331 // Alternative methods for overloaded operators
332 //--------------------------------------------------
334 // Alternative method for operator +
335 public static SqlDouble Add(SqlDouble x, SqlDouble y) {
338 // Alternative method for operator -
339 public static SqlDouble Subtract(SqlDouble x, SqlDouble y) {
343 // Alternative method for operator *
344 public static SqlDouble Multiply(SqlDouble x, SqlDouble y) {
348 // Alternative method for operator /
349 public static SqlDouble Divide(SqlDouble x, SqlDouble y) {
353 // Alternative method for operator ==
354 public static SqlBoolean Equals(SqlDouble x, SqlDouble y) {
358 // Alternative method for operator !=
359 public static SqlBoolean NotEquals(SqlDouble x, SqlDouble y) {
363 // Alternative method for operator <
364 public static SqlBoolean LessThan(SqlDouble x, SqlDouble y) {
368 // Alternative method for operator >
369 public static SqlBoolean GreaterThan(SqlDouble x, SqlDouble y) {
373 // Alternative method for operator <=
374 public static SqlBoolean LessThanOrEqual(SqlDouble x, SqlDouble y) {
378 // Alternative method for operator >=
379 public static SqlBoolean GreaterThanOrEqual(SqlDouble x, SqlDouble y) {
383 // Alternative method for conversions.
385 public SqlBoolean ToSqlBoolean() {
386 return (SqlBoolean)this;
389 public SqlByte ToSqlByte() {
390 return (SqlByte)this;
393 public SqlInt16 ToSqlInt16() {
394 return (SqlInt16)this;
397 public SqlInt32 ToSqlInt32() {
398 return (SqlInt32)this;
401 public SqlInt64 ToSqlInt64() {
402 return (SqlInt64)this;
405 public SqlMoney ToSqlMoney() {
406 return (SqlMoney)this;
409 public SqlDecimal ToSqlDecimal() {
410 return (SqlDecimal)this;
413 public SqlSingle ToSqlSingle() {
414 return (SqlSingle)this;
417 public SqlString ToSqlString() {
418 return (SqlString)this;
424 // Compares this object to another object, returning an integer that
425 // indicates the relationship.
426 // Returns a value less than zero if this < object, zero if this = object,
427 // or a value greater than zero if this > object.
428 // null is considered to be less than any instance.
429 // If object is not of same type, this method throws an ArgumentException.
431 /// <para>[To be supplied.]</para>
433 public int CompareTo(Object value) {
434 if (value is SqlDouble) {
435 SqlDouble i = (SqlDouble)value;
439 throw ADP.WrongType(value.GetType(), typeof(SqlDouble));
442 public int CompareTo(SqlDouble value) {
443 // If both Null, consider them equal.
444 // Otherwise, Null is less than anything.
446 return value.IsNull ? 0 : -1;
447 else if (value.IsNull)
450 if (this < value) return -1;
451 if (this > value) return 1;
455 // Compares this instance with a specified object
457 /// <para>[To be supplied.]</para>
459 public override bool Equals(Object value) {
460 if (!(value is SqlDouble)) {
464 SqlDouble i = (SqlDouble)value;
466 if (i.IsNull || IsNull)
467 return (i.IsNull && IsNull);
469 return (this == i).Value;
472 // For hashing purpose
474 /// <para>[To be supplied.]</para>
476 public override int GetHashCode() {
477 return IsNull ? 0 : Value.GetHashCode();
481 /// <para>[To be supplied.]</para>
483 XmlSchema IXmlSerializable.GetSchema() { return null; }
486 /// <para>[To be supplied.]</para>
488 void IXmlSerializable.ReadXml(XmlReader reader) {
489 string isNull = reader.GetAttribute("nil", XmlSchema.InstanceNamespace);
490 if (isNull != null && XmlConvert.ToBoolean(isNull)) {
491 // VSTFDevDiv# 479603 - SqlTypes read null value infinitely and never read the next value. Fix - Read the next value.
492 reader.ReadElementString();
496 m_value = XmlConvert.ToDouble(reader.ReadElementString());
502 /// <para>[To be supplied.]</para>
504 void IXmlSerializable.WriteXml(XmlWriter writer) {
506 writer.WriteAttributeString("xsi", "nil", XmlSchema.InstanceNamespace, "true");
509 writer.WriteString(XmlConvert.ToString(m_value));
514 /// <para>[To be supplied.]</para>
516 public static XmlQualifiedName GetXsdType(XmlSchemaSet schemaSet) {
517 return new XmlQualifiedName("double", XmlSchema.Namespace);
521 /// <para>[To be supplied.]</para>
523 public static readonly SqlDouble Null = new SqlDouble(true);
525 /// <para>[To be supplied.]</para>
527 public static readonly SqlDouble Zero = new SqlDouble(0.0);
529 /// <para>[To be supplied.]</para>
531 public static readonly SqlDouble MinValue = new SqlDouble(Double.MinValue);
533 /// <para>[To be supplied.]</para>
535 public static readonly SqlDouble MaxValue = new SqlDouble(Double.MaxValue);
539 } // namespace System.Data.SqlTypes