3 // Copyright (c) Microsoft Corporation. All rights reserved.
6 /*============================================================
11 ** Purpose: A wrapper class for the primitive type float.
14 ===========================================================*/
17 using System.Globalization;
20 /// using System.Numerics;
22 using System.Runtime.InteropServices;
23 using System.Runtime.CompilerServices;
24 using System.Runtime.ConstrainedExecution;
25 using System.Diagnostics.Contracts;
28 [System.Runtime.InteropServices.StructLayout(LayoutKind.Sequential)]
29 [System.Runtime.InteropServices.ComVisible(true)]
31 public struct Single : IComparable, IFormattable, IConvertible
32 , IComparable<Single>, IEquatable<Single>
33 /// , IArithmetic<Single>
35 public struct Single : IComparable, IFormattable, IConvertible
38 internal float m_value;
43 public const float MinValue = (float)-3.40282346638528859e+38;
44 public const float Epsilon = (float)1.4e-45;
45 public const float MaxValue = (float)3.40282346638528859e+38;
46 public const float PositiveInfinity = (float)1.0 / (float)0.0;
47 public const float NegativeInfinity = (float)-1.0 / (float)0.0;
48 public const float NaN = (float)0.0 / (float)0.0;
51 [System.Security.SecuritySafeCritical] // auto-generated
52 [System.Runtime.Versioning.NonVersionable]
53 public unsafe static bool IsInfinity(float f) {
54 return (*(int*)(&f) & 0x7FFFFFFF) == 0x7F800000;
58 [System.Security.SecuritySafeCritical] // auto-generated
59 [System.Runtime.Versioning.NonVersionable]
60 public unsafe static bool IsPositiveInfinity(float f) {
61 return *(int*)(&f) == 0x7F800000;
65 [System.Security.SecuritySafeCritical] // auto-generated
66 [System.Runtime.Versioning.NonVersionable]
67 public unsafe static bool IsNegativeInfinity(float f) {
68 return *(int*)(&f) == unchecked((int)0xFF800000);
72 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
73 [System.Security.SecuritySafeCritical]
74 [System.Runtime.Versioning.NonVersionable]
75 public unsafe static bool IsNaN(float f) {
76 return (*(int*)(&f) & 0x7FFFFFFF) > 0x7F800000;
79 // Compares this object to another object, returning an integer that
80 // indicates the relationship.
81 // Returns a value less than zero if this object
82 // null is considered to be less than any instance.
83 // If object is not of type Single, this method throws an ArgumentException.
85 public int CompareTo(Object value) {
89 if (value is Single) {
90 float f = (float)value;
91 if (m_value < f) return -1;
92 if (m_value > f) return 1;
93 if (m_value == f) return 0;
95 // At least one of the values is NaN.
97 return (IsNaN(f) ? 0 : -1);
101 throw new ArgumentException (Environment.GetResourceString("Arg_MustBeSingle"));
105 public int CompareTo(Single value) {
106 if (m_value < value) return -1;
107 if (m_value > value) return 1;
108 if (m_value == value) return 0;
110 // At least one of the values is NaN.
112 return (IsNaN(value) ? 0 : -1);
117 [System.Runtime.Versioning.NonVersionable]
118 public static bool operator ==(Single left, Single right) {
119 return left == right;
122 [System.Runtime.Versioning.NonVersionable]
123 public static bool operator !=(Single left, Single right) {
124 return left != right;
127 [System.Runtime.Versioning.NonVersionable]
128 public static bool operator <(Single left, Single right) {
132 [System.Runtime.Versioning.NonVersionable]
133 public static bool operator >(Single left, Single right) {
137 [System.Runtime.Versioning.NonVersionable]
138 public static bool operator <=(Single left, Single right) {
139 return left <= right;
142 [System.Runtime.Versioning.NonVersionable]
143 public static bool operator >=(Single left, Single right) {
144 return left >= right;
147 public override bool Equals(Object obj) {
148 if (!(obj is Single)) {
151 float temp = ((Single)obj).m_value;
152 if (temp == m_value) {
156 return IsNaN(temp) && IsNaN(m_value);
159 public bool Equals(Single obj)
161 if (obj == m_value) {
165 return IsNaN(obj) && IsNaN(m_value);
168 [System.Security.SecuritySafeCritical] // auto-generated
169 public unsafe override int GetHashCode() {
172 // Ensure that 0 and -0 have the same hash code
179 [System.Security.SecuritySafeCritical] // auto-generated
180 public override String ToString() {
181 Contract.Ensures(Contract.Result<String>() != null);
182 return Number.FormatSingle(m_value, null, NumberFormatInfo.CurrentInfo);
185 [System.Security.SecuritySafeCritical] // auto-generated
186 public String ToString(IFormatProvider provider) {
187 Contract.Ensures(Contract.Result<String>() != null);
188 return Number.FormatSingle(m_value, null, NumberFormatInfo.GetInstance(provider));
191 [System.Security.SecuritySafeCritical] // auto-generated
192 public String ToString(String format) {
193 Contract.Ensures(Contract.Result<String>() != null);
194 return Number.FormatSingle(m_value, format, NumberFormatInfo.CurrentInfo);
197 [System.Security.SecuritySafeCritical] // auto-generated
198 public String ToString(String format, IFormatProvider provider) {
199 Contract.Ensures(Contract.Result<String>() != null);
200 return Number.FormatSingle(m_value, format, NumberFormatInfo.GetInstance(provider));
203 // Parses a float from a String in the given style. If
204 // a NumberFormatInfo isn't specified, the current culture's
205 // NumberFormatInfo is assumed.
207 // This method will not throw an OverflowException, but will return
208 // PositiveInfinity or NegativeInfinity for a number that is too
209 // large or too small.
211 public static float Parse(String s) {
212 return Parse(s, NumberStyles.Float | NumberStyles.AllowThousands, NumberFormatInfo.CurrentInfo);
215 public static float Parse(String s, NumberStyles style) {
216 NumberFormatInfo.ValidateParseStyleFloatingPoint(style);
217 return Parse(s, style, NumberFormatInfo.CurrentInfo);
220 public static float Parse(String s, IFormatProvider provider) {
221 return Parse(s, NumberStyles.Float | NumberStyles.AllowThousands, NumberFormatInfo.GetInstance(provider));
224 public static float Parse(String s, NumberStyles style, IFormatProvider provider) {
225 NumberFormatInfo.ValidateParseStyleFloatingPoint(style);
226 return Parse(s, style, NumberFormatInfo.GetInstance(provider));
229 private static float Parse(String s, NumberStyles style, NumberFormatInfo info) {
230 return Number.ParseSingle(s, style, info);
233 public static Boolean TryParse(String s, out Single result) {
234 return TryParse(s, NumberStyles.Float | NumberStyles.AllowThousands, NumberFormatInfo.CurrentInfo, out result);
237 public static Boolean TryParse(String s, NumberStyles style, IFormatProvider provider, out Single result) {
238 NumberFormatInfo.ValidateParseStyleFloatingPoint(style);
239 return TryParse(s, style, NumberFormatInfo.GetInstance(provider), out result);
242 private static Boolean TryParse(String s, NumberStyles style, NumberFormatInfo info, out Single result) {
247 bool success = Number.TryParseSingle(s, style, info, out result);
249 String sTrim = s.Trim();
250 if (sTrim.Equals(info.PositiveInfinitySymbol)) {
251 result = PositiveInfinity;
252 } else if (sTrim.Equals(info.NegativeInfinitySymbol)) {
253 result = NegativeInfinity;
254 } else if (sTrim.Equals(info.NaNSymbol)) {
257 return false; // We really failed
264 // IConvertible implementation
267 public TypeCode GetTypeCode() {
268 return TypeCode.Single;
273 bool IConvertible.ToBoolean(IFormatProvider provider) {
274 return Convert.ToBoolean(m_value);
278 char IConvertible.ToChar(IFormatProvider provider) {
279 throw new InvalidCastException(Environment.GetResourceString("InvalidCast_FromTo", "Single", "Char"));
283 sbyte IConvertible.ToSByte(IFormatProvider provider) {
284 return Convert.ToSByte(m_value);
288 byte IConvertible.ToByte(IFormatProvider provider) {
289 return Convert.ToByte(m_value);
293 short IConvertible.ToInt16(IFormatProvider provider) {
294 return Convert.ToInt16(m_value);
298 ushort IConvertible.ToUInt16(IFormatProvider provider) {
299 return Convert.ToUInt16(m_value);
303 int IConvertible.ToInt32(IFormatProvider provider) {
304 return Convert.ToInt32(m_value);
308 uint IConvertible.ToUInt32(IFormatProvider provider) {
309 return Convert.ToUInt32(m_value);
313 long IConvertible.ToInt64(IFormatProvider provider) {
314 return Convert.ToInt64(m_value);
318 ulong IConvertible.ToUInt64(IFormatProvider provider) {
319 return Convert.ToUInt64(m_value);
323 float IConvertible.ToSingle(IFormatProvider provider) {
328 double IConvertible.ToDouble(IFormatProvider provider) {
329 return Convert.ToDouble(m_value);
333 Decimal IConvertible.ToDecimal(IFormatProvider provider) {
334 return Convert.ToDecimal(m_value);
338 DateTime IConvertible.ToDateTime(IFormatProvider provider) {
339 throw new InvalidCastException(Environment.GetResourceString("InvalidCast_FromTo", "Single", "DateTime"));
343 Object IConvertible.ToType(Type type, IFormatProvider provider) {
344 return Convert.DefaultToType((IConvertible)this, type, provider);
348 /// // IArithmetic<Single> implementation
351 /// /// <internalonly/>
352 /// Single IArithmetic<Single>.AbsoluteValue(out bool overflowed) {
353 /// Single abs = (m_value < 0 ? -m_value : m_value);
354 /// overflowed = IsInfinity(abs) || IsNaN(abs);
358 /// /// <internalonly/>
359 /// Single IArithmetic<Single>.Negate(out bool overflowed) {
360 /// Single neg= -m_value;
361 /// overflowed = IsInfinity(neg) || IsNaN(neg);
365 /// /// <internalonly/>
366 /// Single IArithmetic<Single>.Sign(out bool overflowed) {
367 /// overflowed = IsNaN(m_value);
368 /// if (overflowed) {
371 /// return (m_value >= 0 ? (m_value == 0 ? 0 : 1) : -1);
374 /// /// <internalonly/>
375 /// Single IArithmetic<Single>.Add(Single addend, out bool overflowed) {
376 /// Single s = m_value + addend;
377 /// overflowed = IsInfinity(s) || IsNaN(s);
381 /// /// <internalonly/>
382 /// Single IArithmetic<Single>.Subtract(Single subtrahend, out bool overflowed) {
383 /// Single s = m_value - subtrahend;
384 /// overflowed = IsInfinity(s) || IsNaN(s);
388 /// /// <internalonly/>
389 /// Single IArithmetic<Single>.Multiply(Single multiplier, out bool overflowed) {
390 /// Single s = m_value * multiplier;
391 /// overflowed = IsInfinity(s) || IsNaN(s);
396 /// /// <internalonly/>
397 /// Single IArithmetic<Single>.Divide(Single divisor, out bool overflowed) {
398 /// Single s = m_value / divisor;
399 /// overflowed = IsInfinity(s) || IsNaN(s);
403 /// /// <internalonly/>
404 /// Single IArithmetic<Single>.DivideRemainder(Single divisor, out Single remainder, out bool overflowed) {
405 /// remainder = m_value % divisor;
406 /// Single s = m_value / divisor;
407 /// overflowed = IsInfinity(s) || IsInfinity(remainder) || IsNaN(s) || IsNaN(remainder);
411 /// /// <internalonly/>
412 /// Single IArithmetic<Single>.Remainder(Single divisor, out bool overflowed) {
413 /// Single s = m_value % divisor;
414 /// overflowed = IsInfinity(s) || IsNaN(s);
418 /// /// <internalonly/>
419 /// ArithmeticDescriptor<Single> IArithmetic<Single>.GetDescriptor() {
420 /// if (s_descriptor == null) {
421 /// s_descriptor = new SingleArithmeticDescriptor( ArithmeticCapabilities.One
422 /// | ArithmeticCapabilities.Zero
423 /// | ArithmeticCapabilities.MaxValue
424 /// | ArithmeticCapabilities.MinValue
425 /// | ArithmeticCapabilities.PositiveInfinity
426 /// | ArithmeticCapabilities.NegativeInfinity);
428 /// return s_descriptor;
431 /// private static SingleArithmeticDescriptor s_descriptor;
433 /// class SingleArithmeticDescriptor : ArithmeticDescriptor<Single> {
434 /// public SingleArithmeticDescriptor(ArithmeticCapabilities capabilities) : base(capabilities) {}
435 /// public override Single One {
437 /// return (Single) 1;
441 /// public override Single Zero {
443 /// return (Single) 0;
447 /// public override Single MinValue {
449 /// return Single.MinValue;
453 /// public override Single MaxValue {
455 /// return Single.MaxValue;
459 /// public override Single PositiveInfinity {
461 /// return Single.PositiveInfinity;
465 /// public override Single NegativeInfinity {
467 /// return Single.NegativeInfinity;
472 ///#endif // #if GENERICS_WORK