Csharp/CSharp Tutorial/Data Type/float Point Number

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Floating point ranges

<source lang="csharp">using System; using System.Collections.Generic; using System.Globalization; using System.IO; using System.Text; using System.Security.Cryptography; public class MainClass {

   public static void Main()
   {
       Console.WriteLine("{0}: bytes: {1}, range: [{2},{3}]",
           typeof(float).ToString(), sizeof(float), float.MinValue, float.MaxValue);
       Console.WriteLine("{0}: bytes: {1}, range: [{2},{3}]",
           typeof(double).ToString(), sizeof(double), double.MinValue, double.MaxValue);
       Console.WriteLine("{0}: bytes: {1}, range: [{2},{3}]",
           typeof(decimal).ToString(), sizeof(decimal), decimal.MinValue, decimal.MaxValue);
   }

}</source>

System.Single: bytes: 4, range: [-3.402823E+38,3.402823E+38]
System.Double: bytes: 8, range: [-1.79769313486232E+308,1.79769313486232E+308]
System.Decimal: bytes: 16, range: [-79228162514264337593543950335,79228162514264337593543950335]

Floating-Point Types

The floating-point types represent numbers with fractional components.

There are two kinds of floating-point types:

  1. float,which represent single-precision numbers
  2. double, which represent double-precision numbers
  3. The type float is 32 bits wide and has a range of 1.5E-45 to 3.4E+38.
  4. The double type is 64 bits wide and has a range of 5E-324 to 1.7E+308.

There are two floating-point structures: Double and Single.

  1. Single represents float.
  2. Double represents double.

The floating-point structures implement the following interfaces:

  1. IComparable,
  2. IConvertible, and
  3. Iformattable.

2.21.float Point Number 2.21.1. Floating-Point Types 2.21.2. <A href="/Tutorial/CSharp/0040__Data-Type/TheMethodsandFieldsSupportedbySingle.htm">The Methods and Fields Supported by Single </a> 2.21.3. <A href="/Tutorial/CSharp/0040__Data-Type/TheMethodsandFieldsSupportedbyDouble.htm">The Methods and Fields Supported by Double </a> 2.21.4. <A href="/Tutorial/CSharp/0040__Data-Type/floatpointvalueliteral3281fand5E02.htm">float point value literal: 3.281f and 5E-02</a> 2.21.5. <A href="/Tutorial/CSharp/0040__Data-Type/Floatingpointranges.htm">Floating point ranges</a> 2.21.6. <A href="/Tutorial/CSharp/0040__Data-Type/UnexpectedInequalityDuetoFloatingPointInaccuracies.htm">Unexpected Inequality Due to Floating-Point Inaccuracies</a>

float point value literal: and 5E-02

<source lang="csharp">using System; class MainClass {

 static void Main(string[] args)
 {
   float MyFloat = 3.281f;
   double MyDouble = 5E-02;
 }

}</source>

The Methods and Fields Supported by Double

Method Meaning public int CompareTo(object v) Returns zero if the values are equal. Returns a negative value if the invoking object has a lower value. Returns a positive value if the invoking object has a greater value. public override bool Equals(object v) Returns true if the value of the invoking object equals the value of v. public override int GetHashCode() Returns the hash code. public TypeCode GetTypeCode() Returns the TypeCode enumeration value for Double, which is TypeCode.Double. public static bool IsInfinity(double v) Is v infinit (either positive or negative). public static bool IsNaN(double v) Is v is a number. public static bool IsPositiveInfinity(double v) Does v represent positive infinity. public static bool IsNegativeInfinity(double v) Returns true if v represents negative infinity. Otherwise, returns false. public static double Parse(string str) Parse the string. public static doubleParse(string str,IFormatProvider fmtpvdr) Parse the string using the culture-specific information provided by fmtpvdr. public static doubleParse(string str, NumberStyles styles) Parse the string using the style information provided by styles. public static doubleParse(string str,NumberStyles styles,IFormatProvider fmtpvdr) Returns the binary equivalent of the numeric string in str using the style information provided by styles and the culture-specific format information provided by fmtpvdr. public override string ToString() Convert value to string. public string ToString(string format) Convert value to string by the format. public stringToString(IFormatProvider fmtpvdr) Convert value to string by the culture-specific information specified in fmtpvdr. public stringToString(string format,IFormatProvider fmtpvdr) Convert value to string by the culture-specific information specified in fmtpvdr and the format specified by format. Field Meaning public const double Epsilon The smallest non-zero positive value. public const double MaxValue The largest value that a double can hold. public const double MinValue The smallest value that a double can hold. public const double NaN A value that is not a number. public const double NegativeInfinity A value representing negative infinity. public const double PositiveInfinity A value representing positive infinity.

The Methods and Fields Supported by Single

Method Meaning public int CompareTo(object v) Returns zero if the values are equal. Returns a negative value if the invoking object has a lower value. Returns a positive value if the invoking object has a greater value. public override bool Equals(object v) Returns true if the value of the invoking object equals the value of v. public override int GetHashCode() Returns the hash code. public TypeCode GetTypeCode() Returns the TypeCode enumeration value for Single, which is TypeCode.Single. public static bool IsInfinity(float v) Is it infinity (either positive or negative). public static bool IsNaN(float v) Is v not a number. public static bool IsPositiveInfinity(float v) Is v positive infinity. public static bool IsNegativeInfinity(float v) Is v negative infinity. public static float Parse(string str) Parse the string. If the string does not represent a float value, an exception is thrown. public static floatParse(string str,IFormatProvider fmtpvdr) Parse the string using the culture-specific information provided by fmtpvdr. public static floatParse(string str, NumberStyles styles) Parse string using the style information provided by styles. public static floatParse(string str,NumberStyles styles,IFormatProvider fmtpvdr) Parse the string using the style information provided by styles and the culture-specific format information provided by fmtpvdr. public override string ToString() Convert it to string. public string ToString(string format) Convert it to string using the specified format. public stringToString(IFormatProvider fmtpvdr) Returns the string representation of the value of the invoking object using the culture-specific information specified in fmtpvdr. public stringToString(string format,IFormatProvider fmtpvdr) Returns the string representation of the value of the invoking object using the culture-specific information specified in fmtpvdr and the format specified by format. Field Meaning public const float Epsilon The smallest non-zero positive value. public const float MaxValue The largest value that a float can hold. public const float MinValue The smallest value that a float can hold. public const float NaN A value that is not a number. public const float NegativeInfinity A value representing negative infinity. public const float PositiveInfinity A value representing positive infinity.

Unexpected Inequality Due to Floating-Point Inaccuracies

<source lang="csharp">class MainClass {

 static void Main()
 {
   decimal decimalNumber = 4.1231232M;
   double doubleNumber1 = 0.1F * 42F;
   double doubleNumber2 = 0.1D * 42D;
   float floatNumber = 0.1F * 42F;
   
   System.Console.WriteLine("{0} != {1}", decimalNumber, (decimal)doubleNumber1);
   
   System.Console.WriteLine("{0} != {1}", (double)decimalNumber, doubleNumber1);
   
   System.Console.WriteLine("(float){0}M != {1}F",(float)decimalNumber, floatNumber);
   
   System.Console.WriteLine("{0} != {1}", doubleNumber1, (double)floatNumber);
   
   System.Console.WriteLine("{0} != {1}", doubleNumber1, doubleNumber2);
   
   System.Console.WriteLine("{0}F != {1}D", floatNumber, doubleNumber2);
   
   System.Console.WriteLine("{0} != {1}", (double)4.2F, 4.2D);
   
   System.Console.WriteLine("{0}F != {1}D", 4.2F, 4.2D);
 }

}</source>