mscorlib(4.0.0.0) API with additions
Single.cs
5 using System.Security;
6 
7 namespace System
8 {
10  [Serializable]
11  [ComVisible(true)]
12  [__DynamicallyInvokable]
14  {
15  internal float m_value;
16 
18  [__DynamicallyInvokable]
19  public const float MinValue = -3.40282347E+38f;
20 
22  [__DynamicallyInvokable]
23  public const float Epsilon = 1.401298E-45f;
24 
26  [__DynamicallyInvokable]
27  public const float MaxValue = 3.40282347E+38f;
28 
30  [__DynamicallyInvokable]
31  public const float PositiveInfinity = 1f / 0f;
32 
34  [__DynamicallyInvokable]
35  public const float NegativeInfinity = -1f / 0f;
36 
38  [__DynamicallyInvokable]
39  public const float NaN = 0f / 0f;
40 
45  [SecuritySafeCritical]
46  [NonVersionable]
47  [__DynamicallyInvokable]
48  public unsafe static bool IsInfinity(float f)
49  {
50  return (*(int*)(&f) & int.MaxValue) == 2139095040;
51  }
52 
57  [SecuritySafeCritical]
58  [NonVersionable]
59  [__DynamicallyInvokable]
60  public unsafe static bool IsPositiveInfinity(float f)
61  {
62  return *(int*)(&f) == 2139095040;
63  }
64 
69  [SecuritySafeCritical]
70  [NonVersionable]
71  [__DynamicallyInvokable]
72  public unsafe static bool IsNegativeInfinity(float f)
73  {
74  return *(int*)(&f) == -8388608;
75  }
76 
81  [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
82  [SecuritySafeCritical]
83  [NonVersionable]
84  [__DynamicallyInvokable]
85  public unsafe static bool IsNaN(float f)
86  {
87  return (*(int*)(&f) & int.MaxValue) > 2139095040;
88  }
89 
96  public int CompareTo(object value)
97  {
98  if (value == null)
99  {
100  return 1;
101  }
102  if (value is float)
103  {
104  float num = (float)value;
105  if (this < num)
106  {
107  return -1;
108  }
109  if (this > num)
110  {
111  return 1;
112  }
113  if (this == num)
114  {
115  return 0;
116  }
117  if (IsNaN(this))
118  {
119  if (!IsNaN(num))
120  {
121  return -1;
122  }
123  return 0;
124  }
125  return 1;
126  }
127  throw new ArgumentException(Environment.GetResourceString("Arg_MustBeSingle"));
128  }
129 
133  [__DynamicallyInvokable]
134  public int CompareTo(float value)
135  {
136  if (this < value)
137  {
138  return -1;
139  }
140  if (this > value)
141  {
142  return 1;
143  }
144  if (this == value)
145  {
146  return 0;
147  }
148  if (IsNaN(this))
149  {
150  if (!IsNaN(value))
151  {
152  return -1;
153  }
154  return 0;
155  }
156  return 1;
157  }
158 
164  [NonVersionable]
165  [__DynamicallyInvokable]
166  public static bool operator ==(float left, float right)
167  {
168  return left == right;
169  }
170 
176  [NonVersionable]
177  [__DynamicallyInvokable]
178  public static bool operator !=(float left, float right)
179  {
180  return left != right;
181  }
182 
188  [NonVersionable]
189  [__DynamicallyInvokable]
190  public static bool operator <(float left, float right)
191  {
192  return left < right;
193  }
194 
200  [NonVersionable]
201  [__DynamicallyInvokable]
202  public static bool operator >(float left, float right)
203  {
204  return left > right;
205  }
206 
212  [NonVersionable]
213  [__DynamicallyInvokable]
214  public static bool operator <=(float left, float right)
215  {
216  return left <= right;
217  }
218 
224  [NonVersionable]
225  [__DynamicallyInvokable]
226  public static bool operator >=(float left, float right)
227  {
228  return left >= right;
229  }
230 
235  [__DynamicallyInvokable]
236  public override bool Equals(object obj)
237  {
238  if (!(obj is float))
239  {
240  return false;
241  }
242  float num = (float)obj;
243  if (num == this)
244  {
245  return true;
246  }
247  if (IsNaN(num))
248  {
249  return IsNaN(this);
250  }
251  return false;
252  }
253 
258  [__DynamicallyInvokable]
259  public bool Equals(float obj)
260  {
261  if (obj == this)
262  {
263  return true;
264  }
265  if (IsNaN(obj))
266  {
267  return IsNaN(this);
268  }
269  return false;
270  }
271 
274  [SecuritySafeCritical]
275  [__DynamicallyInvokable]
276  public unsafe override int GetHashCode()
277  {
278  float num = this;
279  if (num == 0f)
280  {
281  return 0;
282  }
283  return *(int*)(&num);
284  }
285 
288  [SecuritySafeCritical]
289  [__DynamicallyInvokable]
290  public override string ToString()
291  {
292  return Number.FormatSingle(this, null, NumberFormatInfo.CurrentInfo);
293  }
294 
298  [SecuritySafeCritical]
299  [__DynamicallyInvokable]
300  public string ToString(IFormatProvider provider)
301  {
302  return Number.FormatSingle(this, null, NumberFormatInfo.GetInstance(provider));
303  }
304 
310  [SecuritySafeCritical]
311  [__DynamicallyInvokable]
312  public string ToString(string format)
313  {
314  return Number.FormatSingle(this, format, NumberFormatInfo.CurrentInfo);
315  }
316 
321  [SecuritySafeCritical]
322  [__DynamicallyInvokable]
323  public string ToString(string format, IFormatProvider provider)
324  {
325  return Number.FormatSingle(this, format, NumberFormatInfo.GetInstance(provider));
326  }
327 
337  [__DynamicallyInvokable]
338  public static float Parse(string s)
339  {
340  return Parse(s, NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite | NumberStyles.AllowLeadingSign | NumberStyles.AllowDecimalPoint | NumberStyles.AllowThousands | NumberStyles.AllowExponent, NumberFormatInfo.CurrentInfo);
341  }
342 
356  [__DynamicallyInvokable]
357  public static float Parse(string s, NumberStyles style)
358  {
359  NumberFormatInfo.ValidateParseStyleFloatingPoint(style);
360  return Parse(s, style, NumberFormatInfo.CurrentInfo);
361  }
362 
373  [__DynamicallyInvokable]
374  public static float Parse(string s, IFormatProvider provider)
375  {
376  return Parse(s, NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite | NumberStyles.AllowLeadingSign | NumberStyles.AllowDecimalPoint | NumberStyles.AllowThousands | NumberStyles.AllowExponent, NumberFormatInfo.GetInstance(provider));
377  }
378 
393  [__DynamicallyInvokable]
394  public static float Parse(string s, NumberStyles style, IFormatProvider provider)
395  {
396  NumberFormatInfo.ValidateParseStyleFloatingPoint(style);
397  return Parse(s, style, NumberFormatInfo.GetInstance(provider));
398  }
399 
400  private static float Parse(string s, NumberStyles style, NumberFormatInfo info)
401  {
402  return Number.ParseSingle(s, style, info);
403  }
404 
410  [__DynamicallyInvokable]
411  public static bool TryParse(string s, out float result)
412  {
413  return TryParse(s, NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite | NumberStyles.AllowLeadingSign | NumberStyles.AllowDecimalPoint | NumberStyles.AllowThousands | NumberStyles.AllowExponent, NumberFormatInfo.CurrentInfo, out result);
414  }
415 
426  [__DynamicallyInvokable]
427  public static bool TryParse(string s, NumberStyles style, IFormatProvider provider, out float result)
428  {
429  NumberFormatInfo.ValidateParseStyleFloatingPoint(style);
430  return TryParse(s, style, NumberFormatInfo.GetInstance(provider), out result);
431  }
432 
433  private static bool TryParse(string s, NumberStyles style, NumberFormatInfo info, out float result)
434  {
435  if (s == null)
436  {
437  result = 0f;
438  return false;
439  }
440  if (!Number.TryParseSingle(s, style, info, out result))
441  {
442  string text = s.Trim();
443  if (text.Equals(info.PositiveInfinitySymbol))
444  {
445  result = float.PositiveInfinity;
446  }
447  else if (text.Equals(info.NegativeInfinitySymbol))
448  {
449  result = float.NegativeInfinity;
450  }
451  else
452  {
453  if (!text.Equals(info.NaNSymbol))
454  {
455  return false;
456  }
457  result = float.NaN;
458  }
459  }
460  return true;
461  }
462 
466  {
467  return TypeCode.Single;
468  }
469 
474  [__DynamicallyInvokable]
476  {
477  return Convert.ToBoolean(this);
478  }
479 
484  [__DynamicallyInvokable]
486  {
487  throw new InvalidCastException(Environment.GetResourceString("InvalidCast_FromTo", "Single", "Char"));
488  }
489 
493  [__DynamicallyInvokable]
495  {
496  return Convert.ToSByte(this);
497  }
498 
502  [__DynamicallyInvokable]
504  {
505  return Convert.ToByte(this);
506  }
507 
511  [__DynamicallyInvokable]
513  {
514  return Convert.ToInt16(this);
515  }
516 
520  [__DynamicallyInvokable]
522  {
523  return Convert.ToUInt16(this);
524  }
525 
529  [__DynamicallyInvokable]
531  {
532  return Convert.ToInt32(this);
533  }
534 
538  [__DynamicallyInvokable]
540  {
541  return Convert.ToUInt32(this);
542  }
543 
547  [__DynamicallyInvokable]
549  {
550  return Convert.ToInt64(this);
551  }
552 
556  [__DynamicallyInvokable]
558  {
559  return Convert.ToUInt64(this);
560  }
561 
565  [__DynamicallyInvokable]
567  {
568  return this;
569  }
570 
574  [__DynamicallyInvokable]
576  {
577  return Convert.ToDouble(this);
578  }
579 
583  [__DynamicallyInvokable]
585  {
586  return Convert.ToDecimal(this);
587  }
588 
593  [__DynamicallyInvokable]
595  {
596  throw new InvalidCastException(Environment.GetResourceString("InvalidCast_FromTo", "Single", "DateTime"));
597  }
598 
603  [__DynamicallyInvokable]
604  object IConvertible.ToType(Type type, IFormatProvider provider)
605  {
606  return Convert.DefaultToType(this, type, provider);
607  }
608  }
609 }
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 float Parse(string s)
Converts the string representation of a number to its single-precision floating-point number equivale...
Definition: Single.cs:338
static long ToInt64(object value)
Converts the value of the specified object to a 64-bit signed integer.
Definition: Convert.cs:2506
static bool TryParse(string s, NumberStyles style, IFormatProvider provider, out float result)
Converts the string representation of a number in a specified style and culture-specific format to it...
Definition: Single.cs:427
static double ToDouble(object value)
Converts the value of the specified object to a double-precision floating-point number.
Definition: Convert.cs:3189
string ToString(string format)
Converts the numeric value of this instance to its equivalent string representation,...
Definition: Single.cs:312
const float MaxValue
Represents the largest possible value of T:System.Single. This field is constant.
Definition: Single.cs:27
const float NaN
Represents not a number (NaN). This field is constant.
Definition: Single.cs:39
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
Definition: __Canon.cs:3
The exception that is thrown for invalid casting or explicit conversion.
char ToChar(IFormatProvider provider)
Converts the value of this instance to an equivalent Unicode character using the specified culture-sp...
static bool operator !=(float left, float right)
Returns a value that indicates whether two specified T:System.Single values are not equal.
Definition: Single.cs:178
static unsafe bool IsNaN(float f)
Returns a value that indicates whether the specified value is not a number (F:System....
Definition: Single.cs:85
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
int CompareTo(object value)
Compares this instance to a specified object and returns an integer that indicates whether the value ...
Definition: Single.cs:96
DateTime ToDateTime(IFormatProvider provider)
Converts the value of this instance to an equivalent T:System.DateTime using the specified culture-sp...
static NumberFormatInfo CurrentInfo
Gets a read-only T:System.Globalization.NumberFormatInfo that formats values based on the current cul...
sbyte ToSByte(IFormatProvider provider)
Converts the value of this instance to an equivalent 8-bit signed integer using the specified culture...
Defines a generalized type-specific comparison method that a value type or class implements to order ...
Definition: IComparable.cs:8
static bool operator<(float left, float right)
Returns a value that indicates whether a specified T:System.Single value is less than another specifi...
Definition: Single.cs:190
NumberStyles
Determines the styles permitted in numeric string arguments that are passed to the Parse and TryParse...
Definition: NumberStyles.cs:10
Cer
Specifies a method's behavior when called within a constrained execution region.
Definition: Cer.cs:5
unsafe override int GetHashCode()
Returns the hash code for this instance.
Definition: Single.cs:276
const float Epsilon
Represents the smallest positive T:System.Single value that is greater than zero. This field is const...
Definition: Single.cs:23
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
decimal ToDecimal(IFormatProvider provider)
Converts the value of this instance to an equivalent T:System.Decimal number using the specified cult...
int ToInt32(IFormatProvider provider)
Converts the value of this instance to an equivalent 32-bit signed integer using the specified cultur...
static bool operator==(float left, float right)
Returns a value that indicates whether two specified T:System.Single values are equal.
Definition: Single.cs:166
static NumberFormatInfo GetInstance(IFormatProvider formatProvider)
Gets the T:System.Globalization.NumberFormatInfo associated with the specified T:System....
const float NegativeInfinity
Represents negative infinity. This field is constant.
Definition: Single.cs:35
static bool operator<=(float left, float right)
Returns a value that indicates whether a specified T:System.Single value is less than or equal to ano...
Definition: Single.cs:214
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
override string ToString()
Converts the numeric value of this instance to its equivalent string representation.
Definition: Single.cs:290
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 TryParse(string s, out float result)
Converts the string representation of a number to its single-precision floating-point number equivale...
Definition: Single.cs:411
static bool operator >=(float left, float right)
Returns a value that indicates whether a specified T:System.Single value is greater than or equal to ...
Definition: Single.cs:226
static uint ToUInt32(object value)
Converts the value of the specified object to a 32-bit unsigned integer.
Definition: Convert.cs:2235
Represents a single-precision floating-point number.
Definition: Single.cs:13
string ToString(string format, IFormatProvider provider)
Converts the numeric value of this instance to its equivalent string representation using the specifi...
Definition: Single.cs:323
TypeCode GetTypeCode()
Returns the T:System.TypeCode for value type T:System.Single.
Definition: Single.cs:465
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
static unsafe bool IsPositiveInfinity(float f)
Returns a value indicating whether the specified number evaluates to positive infinity.
Definition: Single.cs:60
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.
static float Parse(string s, NumberStyles style)
Converts the string representation of a number in a specified style to its single-precision floating-...
Definition: Single.cs:357
static float 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: Single.cs:394
static unsafe bool IsInfinity(float f)
Returns a value indicating whether the specified number evaluates to negative or positive infinity.
Definition: Single.cs:48
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
static bool operator >(float left, float right)
Returns a value that indicates whether a specified T:System.Single value is greater than another spec...
Definition: Single.cs:202
int CompareTo(float value)
Compares this instance to a specified single-precision floating-point number and returns an integer t...
Definition: Single.cs:134
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.
override bool Equals(object obj)
Returns a value indicating whether this instance is equal to a specified object.
Definition: Single.cs:236
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
static float Parse(string s, IFormatProvider provider)
Converts the string representation of a number in a specified culture-specific format to its single-p...
Definition: Single.cs:374
Provides functionality to format the value of an object into a string representation.
Definition: IFormattable.cs:8
const float PositiveInfinity
Represents positive infinity. This field is constant.
Definition: Single.cs:31
const float MinValue
Represents the smallest possible value of T:System.Single. This field is constant.
Definition: Single.cs:19
static unsafe bool IsNegativeInfinity(float f)
Returns a value indicating whether the specified number evaluates to negative infinity.
Definition: Single.cs:72
uint ToUInt32(IFormatProvider provider)
Converts the value of this instance to an equivalent 32-bit unsigned integer using the specified cult...
string ToString(IFormatProvider provider)
Converts the numeric value of this instance to its equivalent string representation using the specifi...
Definition: Single.cs:300
bool Equals(float obj)
Returns a value indicating whether this instance and a specified T:System.Single object represent the...
Definition: Single.cs:259
Provides culture-specific information for formatting and parsing numeric values.