mscorlib(4.0.0.0) API with additions
Double.cs
5 using System.Security;
6 
7 namespace System
8 {
10  [Serializable]
11  [ComVisible(true)]
12  [__DynamicallyInvokable]
13  public struct Double : IComparable, IFormattable, IConvertible, IComparable<double>, IEquatable<double>
14  {
15  internal double m_value;
16 
18  [__DynamicallyInvokable]
19  public const double MinValue = -1.7976931348623157E+308;
20 
22  [__DynamicallyInvokable]
23  public const double MaxValue = 1.7976931348623157E+308;
24 
26  [__DynamicallyInvokable]
27  public const double Epsilon = 4.94065645841247E-324;
28 
30  [__DynamicallyInvokable]
31  public const double NegativeInfinity = -1.0 / 0.0;
32 
34  [__DynamicallyInvokable]
35  public const double PositiveInfinity = 1.0 / 0.0;
36 
38  [__DynamicallyInvokable]
39  public const double NaN = 0.0 / 0.0;
40 
41  internal static double NegativeZero = BitConverter.Int64BitsToDouble(long.MinValue);
42 
47  [SecuritySafeCritical]
48  [NonVersionable]
49  [__DynamicallyInvokable]
50  public unsafe static bool IsInfinity(double d)
51  {
52  return (*(long*)(&d) & long.MaxValue) == 9218868437227405312L;
53  }
54 
59  [NonVersionable]
60  [__DynamicallyInvokable]
61  public static bool IsPositiveInfinity(double d)
62  {
63  if (d == double.PositiveInfinity)
64  {
65  return true;
66  }
67  return false;
68  }
69 
74  [NonVersionable]
75  [__DynamicallyInvokable]
76  public static bool IsNegativeInfinity(double d)
77  {
78  if (d == double.NegativeInfinity)
79  {
80  return true;
81  }
82  return false;
83  }
84 
85  [SecuritySafeCritical]
86  internal unsafe static bool IsNegative(double d)
87  {
88  return (*(long*)(&d) & long.MinValue) == long.MinValue;
89  }
90 
95  [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
96  [SecuritySafeCritical]
97  [NonVersionable]
98  [__DynamicallyInvokable]
99  public unsafe static bool IsNaN(double d)
100  {
101  return (ulong)(*(long*)(&d) & long.MaxValue) > 9218868437227405312uL;
102  }
103 
110  public int CompareTo(object value)
111  {
112  if (value == null)
113  {
114  return 1;
115  }
116  if (value is double)
117  {
118  double num = (double)value;
119  if (this < num)
120  {
121  return -1;
122  }
123  if (this > num)
124  {
125  return 1;
126  }
127  if (this == num)
128  {
129  return 0;
130  }
131  if (IsNaN(this))
132  {
133  if (!IsNaN(num))
134  {
135  return -1;
136  }
137  return 0;
138  }
139  return 1;
140  }
141  throw new ArgumentException(Environment.GetResourceString("Arg_MustBeDouble"));
142  }
143 
147  [__DynamicallyInvokable]
148  public int CompareTo(double value)
149  {
150  if (this < value)
151  {
152  return -1;
153  }
154  if (this > value)
155  {
156  return 1;
157  }
158  if (this == value)
159  {
160  return 0;
161  }
162  if (IsNaN(this))
163  {
164  if (!IsNaN(value))
165  {
166  return -1;
167  }
168  return 0;
169  }
170  return 1;
171  }
172 
177  [__DynamicallyInvokable]
178  public override bool Equals(object obj)
179  {
180  if (!(obj is double))
181  {
182  return false;
183  }
184  double num = (double)obj;
185  if (num == this)
186  {
187  return true;
188  }
189  if (IsNaN(num))
190  {
191  return IsNaN(this);
192  }
193  return false;
194  }
195 
201  [NonVersionable]
202  [__DynamicallyInvokable]
203  public static bool operator ==(double left, double right)
204  {
205  return left == right;
206  }
207 
213  [NonVersionable]
214  [__DynamicallyInvokable]
215  public static bool operator !=(double left, double right)
216  {
217  return left != right;
218  }
219 
225  [NonVersionable]
226  [__DynamicallyInvokable]
227  public static bool operator <(double left, double right)
228  {
229  return left < right;
230  }
231 
237  [NonVersionable]
238  [__DynamicallyInvokable]
239  public static bool operator >(double left, double right)
240  {
241  return left > right;
242  }
243 
249  [NonVersionable]
250  [__DynamicallyInvokable]
251  public static bool operator <=(double left, double right)
252  {
253  return left <= right;
254  }
255 
261  [NonVersionable]
262  [__DynamicallyInvokable]
263  public static bool operator >=(double left, double right)
264  {
265  return left >= right;
266  }
267 
272  [__DynamicallyInvokable]
273  public bool Equals(double obj)
274  {
275  if (obj == this)
276  {
277  return true;
278  }
279  if (IsNaN(obj))
280  {
281  return IsNaN(this);
282  }
283  return false;
284  }
285 
288  [SecuritySafeCritical]
289  [__DynamicallyInvokable]
290  public unsafe override int GetHashCode()
291  {
292  double num = this;
293  if (num == 0.0)
294  {
295  return 0;
296  }
297  long num2 = *(long*)(&num);
298  return (int)num2 ^ (int)(num2 >> 32);
299  }
300 
303  [SecuritySafeCritical]
304  [__DynamicallyInvokable]
305  public override string ToString()
306  {
307  return Number.FormatDouble(this, null, NumberFormatInfo.CurrentInfo);
308  }
309 
315  [SecuritySafeCritical]
316  [__DynamicallyInvokable]
317  public string ToString(string format)
318  {
319  return Number.FormatDouble(this, format, NumberFormatInfo.CurrentInfo);
320  }
321 
325  [SecuritySafeCritical]
326  [__DynamicallyInvokable]
327  public string ToString(IFormatProvider provider)
328  {
329  return Number.FormatDouble(this, null, NumberFormatInfo.GetInstance(provider));
330  }
331 
336  [SecuritySafeCritical]
337  [__DynamicallyInvokable]
338  public string ToString(string format, IFormatProvider provider)
339  {
340  return Number.FormatDouble(this, format, NumberFormatInfo.GetInstance(provider));
341  }
342 
352  [__DynamicallyInvokable]
353  public static double Parse(string s)
354  {
355  return Parse(s, NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite | NumberStyles.AllowLeadingSign | NumberStyles.AllowDecimalPoint | NumberStyles.AllowThousands | NumberStyles.AllowExponent, NumberFormatInfo.CurrentInfo);
356  }
357 
371  [__DynamicallyInvokable]
372  public static double Parse(string s, NumberStyles style)
373  {
374  NumberFormatInfo.ValidateParseStyleFloatingPoint(style);
375  return Parse(s, style, NumberFormatInfo.CurrentInfo);
376  }
377 
388  [__DynamicallyInvokable]
389  public static double Parse(string s, IFormatProvider provider)
390  {
391  return Parse(s, NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite | NumberStyles.AllowLeadingSign | NumberStyles.AllowDecimalPoint | NumberStyles.AllowThousands | NumberStyles.AllowExponent, NumberFormatInfo.GetInstance(provider));
392  }
393 
408  [__DynamicallyInvokable]
409  public static double Parse(string s, NumberStyles style, IFormatProvider provider)
410  {
411  NumberFormatInfo.ValidateParseStyleFloatingPoint(style);
412  return Parse(s, style, NumberFormatInfo.GetInstance(provider));
413  }
414 
415  private static double Parse(string s, NumberStyles style, NumberFormatInfo info)
416  {
417  return Number.ParseDouble(s, style, info);
418  }
419 
425  [__DynamicallyInvokable]
426  public static bool TryParse(string s, out double result)
427  {
428  return TryParse(s, NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite | NumberStyles.AllowLeadingSign | NumberStyles.AllowDecimalPoint | NumberStyles.AllowThousands | NumberStyles.AllowExponent, NumberFormatInfo.CurrentInfo, out result);
429  }
430 
441  [__DynamicallyInvokable]
442  public static bool TryParse(string s, NumberStyles style, IFormatProvider provider, out double result)
443  {
444  NumberFormatInfo.ValidateParseStyleFloatingPoint(style);
445  return TryParse(s, style, NumberFormatInfo.GetInstance(provider), out result);
446  }
447 
448  private static bool TryParse(string s, NumberStyles style, NumberFormatInfo info, out double result)
449  {
450  if (s == null)
451  {
452  result = 0.0;
453  return false;
454  }
455  if (!Number.TryParseDouble(s, style, info, out result))
456  {
457  string text = s.Trim();
458  if (text.Equals(info.PositiveInfinitySymbol))
459  {
460  result = double.PositiveInfinity;
461  }
462  else if (text.Equals(info.NegativeInfinitySymbol))
463  {
464  result = double.NegativeInfinity;
465  }
466  else
467  {
468  if (!text.Equals(info.NaNSymbol))
469  {
470  return false;
471  }
472  result = double.NaN;
473  }
474  }
475  return true;
476  }
477 
481  {
482  return TypeCode.Double;
483  }
484 
489  [__DynamicallyInvokable]
491  {
492  return Convert.ToBoolean(this);
493  }
494 
499  [__DynamicallyInvokable]
501  {
502  throw new InvalidCastException(Environment.GetResourceString("InvalidCast_FromTo", "Double", "Char"));
503  }
504 
508  [__DynamicallyInvokable]
510  {
511  return Convert.ToSByte(this);
512  }
513 
517  [__DynamicallyInvokable]
519  {
520  return Convert.ToByte(this);
521  }
522 
526  [__DynamicallyInvokable]
528  {
529  return Convert.ToInt16(this);
530  }
531 
535  [__DynamicallyInvokable]
537  {
538  return Convert.ToUInt16(this);
539  }
540 
544  [__DynamicallyInvokable]
546  {
547  return Convert.ToInt32(this);
548  }
549 
553  [__DynamicallyInvokable]
555  {
556  return Convert.ToUInt32(this);
557  }
558 
562  [__DynamicallyInvokable]
564  {
565  return Convert.ToInt64(this);
566  }
567 
571  [__DynamicallyInvokable]
573  {
574  return Convert.ToUInt64(this);
575  }
576 
580  [__DynamicallyInvokable]
582  {
583  return Convert.ToSingle(this);
584  }
585 
589  [__DynamicallyInvokable]
591  {
592  return this;
593  }
594 
598  [__DynamicallyInvokable]
600  {
601  return Convert.ToDecimal(this);
602  }
603 
608  [__DynamicallyInvokable]
610  {
611  throw new InvalidCastException(Environment.GetResourceString("InvalidCast_FromTo", "Double", "DateTime"));
612  }
613 
618  [__DynamicallyInvokable]
619  object IConvertible.ToType(Type type, IFormatProvider provider)
620  {
621  return Convert.DefaultToType(this, type, provider);
622  }
623  }
624 }
float ToSingle(IFormatProvider provider)
Converts the value of this instance to an equivalent single-precision floating-point number using the...
Converts a base data type to another base data type.
Definition: Convert.cs:10
static bool IsNegativeInfinity(double d)
Returns a value indicating whether the specified number evaluates to negative infinity.
Definition: Double.cs:76
static long ToInt64(object value)
Converts the value of the specified object to a 64-bit signed integer.
Definition: Convert.cs:2506
string ToString(string format, IFormatProvider provider)
Converts the numeric value of this instance to its equivalent string representation using the specifi...
Definition: Double.cs:338
static double Parse(string s, IFormatProvider provider)
Converts the string representation of a number in a specified culture-specific format to its double-p...
Definition: Double.cs:389
double ToDouble(IFormatProvider provider)
Converts the value of this instance to an equivalent double-precision floating-point number using the...
bool ToBoolean(IFormatProvider provider)
Converts the value of this instance to an equivalent Boolean value using the specified culture-specif...
TypeCode
Specifies the type of an object.
Definition: TypeCode.cs:9
static float ToSingle(object value)
Converts the value of the specified object to a single-precision floating-point number.
Definition: Convert.cs:2982
static bool TryParse(string s, out double result)
Converts the string representation of a number to its double-precision floating-point number equivale...
Definition: Double.cs:426
static double Parse(string s)
Converts the string representation of a number to its double-precision floating-point number equivale...
Definition: Double.cs:353
static bool operator==(double left, double right)
Returns a value that indicates whether two specified T:System.Double values are equal.
Definition: Double.cs:203
Definition: __Canon.cs:3
string ToString(IFormatProvider provider)
Converts the numeric value of this instance to its equivalent string representation using the specifi...
Definition: Double.cs:327
The exception that is thrown for invalid casting or explicit conversion.
const double PositiveInfinity
Represents positive infinity. This field is constant.
Definition: Double.cs:35
char ToChar(IFormatProvider provider)
Converts the value of this instance to an equivalent Unicode character using the specified culture-sp...
Provides a mechanism for retrieving an object to control formatting.
Represents an instant in time, typically expressed as a date and time of day. To browse the ....
Definition: DateTime.cs:13
const double MaxValue
Represents the largest possible value of a T:System.Double. This field is constant.
Definition: Double.cs:23
DateTime ToDateTime(IFormatProvider provider)
Converts the value of this instance to an equivalent T:System.DateTime using the specified culture-sp...
static unsafe bool IsNaN(double d)
Returns a value that indicates whether the specified value is not a number (F:System....
Definition: Double.cs:99
static NumberFormatInfo CurrentInfo
Gets a read-only T:System.Globalization.NumberFormatInfo that formats values based on the current cul...
static bool TryParse(string s, NumberStyles style, IFormatProvider provider, out double result)
Converts the string representation of a number in a specified style and culture-specific format to it...
Definition: Double.cs:442
sbyte ToSByte(IFormatProvider provider)
Converts the value of this instance to an equivalent 8-bit signed integer using the specified culture...
string ToString(string format)
Converts the numeric value of this instance to its equivalent string representation,...
Definition: Double.cs:317
Defines a generalized type-specific comparison method that a value type or class implements to order ...
Definition: IComparable.cs:8
NumberStyles
Determines the styles permitted in numeric string arguments that are passed to the Parse and TryParse...
Definition: NumberStyles.cs:10
static bool operator<(double left, double right)
Returns a value that indicates whether a specified T:System.Double value is less than another specifi...
Definition: Double.cs:227
Cer
Specifies a method's behavior when called within a constrained execution region.
Definition: Cer.cs:5
const double NegativeInfinity
Represents negative infinity. This field is constant.
Definition: Double.cs:31
Represents a double-precision floating-point number.
Definition: Double.cs:13
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
static bool ToBoolean(object value)
Converts the value of a specified object to an equivalent Boolean value.
Definition: Convert.cs:457
static bool operator<=(double left, double right)
Returns a value that indicates whether a specified T:System.Double value is less than or equal to ano...
Definition: Double.cs:251
decimal ToDecimal(IFormatProvider provider)
Converts the value of this instance to an equivalent T:System.Decimal number using the specified cult...
const double Epsilon
Represents the smallest positive T:System.Double value that is greater than zero. This field is const...
Definition: Double.cs:27
TypeCode GetTypeCode()
Returns the T:System.TypeCode for value type T:System.Double.
Definition: Double.cs:480
int ToInt32(IFormatProvider provider)
Converts the value of this instance to an equivalent 32-bit signed integer using the specified cultur...
static NumberFormatInfo GetInstance(IFormatProvider formatProvider)
Gets the T:System.Globalization.NumberFormatInfo associated with the specified T:System....
bool Equals(double obj)
Returns a value indicating whether this instance and a specified T:System.Double object represent the...
Definition: Double.cs:273
const double NaN
Represents a value that is not a number (NaN). This field is constant.
Definition: Double.cs:39
unsafe override int GetHashCode()
Returns the hash code for this instance.
Definition: Double.cs:290
static ushort ToUInt16(object value)
Converts the value of the specified object to a 16-bit unsigned integer.
Definition: Convert.cs:1707
object ToType(Type conversionType, IFormatProvider provider)
Converts the value of this instance to an T:System.Object of the specified T:System....
static sbyte ToSByte(object value)
Converts the value of the specified object to an 8-bit signed integer.
Definition: Convert.cs:909
Converts base data types to an array of bytes, and an array of bytes to base data types.
Definition: BitConverter.cs:7
Represents type declarations: class types, interface types, array types, value types,...
Definition: Type.cs:18
ushort ToUInt16(IFormatProvider provider)
Converts the value of this instance to an equivalent 16-bit unsigned integer using the specified cult...
static bool operator !=(double left, double right)
Returns a value that indicates whether two specified T:System.Double values are not equal.
Definition: Double.cs:215
static uint ToUInt32(object value)
Converts the value of the specified object to a 32-bit unsigned integer.
Definition: Convert.cs:2235
static double Parse(string s, NumberStyles style, IFormatProvider provider)
Converts the string representation of a number in a specified style and culture-specific format to it...
Definition: Double.cs:409
static double Parse(string s, NumberStyles style)
Converts the string representation of a number in a specified style to its double-precision floating-...
Definition: Double.cs:372
static bool operator >=(double left, double right)
Returns a value that indicates whether a specified T:System.Double value is greater than or equal to ...
Definition: Double.cs:263
short ToInt16(IFormatProvider provider)
Converts the value of this instance to an equivalent 16-bit signed integer using the specified cultur...
static decimal ToDecimal(object value)
Converts the value of the specified object to an equivalent decimal number.
Definition: Convert.cs:3394
long ToInt64(IFormatProvider provider)
Converts the value of this instance to an equivalent 64-bit signed integer using the specified cultur...
The exception that is thrown when one of the arguments provided to a method is not valid.
int CompareTo(double value)
Compares this instance to a specified double-precision floating-point number and returns an integer t...
Definition: Double.cs:148
int CompareTo(object value)
Compares this instance to a specified object and returns an integer that indicates whether the value ...
Definition: Double.cs:110
static byte ToByte(object value)
Converts the value of the specified object to an 8-bit unsigned integer.
Definition: Convert.cs:1186
Defines a generalized method that a value type or class implements to create a type-specific method f...
Definition: IEquatable.cs:6
override bool Equals(object obj)
Returns a value indicating whether this instance is equal to a specified object.
Definition: Double.cs:178
override string ToString()
Converts the numeric value of this instance to its equivalent string representation.
Definition: Double.cs:305
static bool IsPositiveInfinity(double d)
Returns a value indicating whether the specified number evaluates to positive infinity.
Definition: Double.cs:61
static short ToInt16(object value)
Converts the value of the specified object to a 16-bit signed integer.
Definition: Convert.cs:1452
Specifies that the class can be serialized.
static bool operator >(double left, double right)
Returns a value that indicates whether a specified T:System.Double value is greater than another spec...
Definition: Double.cs:239
static unsafe bool IsInfinity(double d)
Returns a value indicating whether the specified number evaluates to negative or positive infinity
Definition: Double.cs:50
Consistency
Specifies a reliability contract.
Definition: Consistency.cs:5
ulong ToUInt64(IFormatProvider provider)
Converts the value of this instance to an equivalent 64-bit unsigned integer using the specified cult...
static ulong ToUInt64(object value)
Converts the value of the specified object to a 64-bit unsigned integer.
Definition: Convert.cs:2727
static int ToInt32(object value)
Converts the value of the specified object to a 32-bit signed integer.
Definition: Convert.cs:1974
byte ToByte(IFormatProvider provider)
Converts the value of this instance to an equivalent 8-bit unsigned integer using the specified cultu...
Defines methods that convert the value of the implementing reference or value type to a common langua...
Definition: IConvertible.cs:9
const double MinValue
Represents the smallest possible value of a T:System.Double. This field is constant.
Definition: Double.cs:19
static unsafe double Int64BitsToDouble(long value)
Converts the specified 64-bit signed integer to a double-precision floating point number.
Provides functionality to format the value of an object into a string representation.
Definition: IFormattable.cs:8
uint ToUInt32(IFormatProvider provider)
Converts the value of this instance to an equivalent 32-bit unsigned integer using the specified cult...
Provides culture-specific information for formatting and parsing numeric values.