Single Single Single Single Struct

定義

単精度浮動小数点数を表します。Represents a single-precision floating-point number.

public value class Single : IComparable, IComparable<float>, IConvertible, IEquatable<float>, IFormattable
[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public struct Single : IComparable, IComparable<float>, IConvertible, IEquatable<float>, IFormattable
type single = struct
    interface IFormattable
    interface IConvertible
Public Structure Single
Implements IComparable, IComparable(Of Single), IConvertible, IEquatable(Of Single), IFormattable
継承
属性
実装

注釈

Single値型は、正または負のゼロと 3.402823 e 38、~ 3.402823 e 38 の負の値から範囲の値を単精度 32 ビットの数値を表しますPositiveInfinityNegativeInfinity、および非数 (NaN)。The Single value type represents a single-precision 32-bit number with values ranging from negative 3.402823e38 to positive 3.402823e38, as well as positive or negative zero, PositiveInfinity, NegativeInfinity, and not a number (NaN). 非常に高くなって (惑星または銀河系間の距離) など、または (キログラムの物質の分子質量) など、非常に小さいとする多くの場合、正確ではない (地球から別の太陽系までの距離などの値を表すものでは).It is intended to represent values that are extremely large (such as distances between planets or galaxies) or extremely small (such as the molecular mass of a substance in kilograms) and that often are imprecise (such as the distance from earth to another solar system). Single IEC 60559: 1989 (IEEE 754) バイナリ浮動小数点演算の標準に準拠している型。The Single type complies with the IEC 60559:1989 (IEEE 754) standard for binary floating-point arithmetic.

このトピックは、次のセクションで構成されています。This topic consists of the following sections:

System.Single インスタンスの値を文字列表現に変換する数値の文字列形式をこの型のインスタンスに変換して、この型のインスタンスを比較するメソッドを提供します。System.Single provides methods to compare instances of this type, to convert the value of an instance to its string representation, and to convert the string representation of a number to an instance of this type. 書式指定コードが値型の文字列形式を制御する方法については、次を参照してください型の書式設定Standard Numeric Format Strings、およびカスタム数値。書式指定文字列します。For information about how format specification codes control the string representation of value types, see Formatting Types, Standard Numeric Format Strings, and Custom Numeric Format Strings.

精度の浮動小数点表現Floating-point representation and precision

Singleデータ型は、次の表に示すように、32 ビットのバイナリ形式で単精度浮動小数点値を格納します。The Single data type stores single-precision floating-point values in a 32-bit binary format, as shown in the following table:

パーツPart ビットBits
仮数または仮数部Significand or mantissa 0-220-22
指数Exponent 23-3023-30
サインイン (0 1、正の数値を = = 負の値)Sign (0 = positive, 1 = negative) 3131

小数がいくつかの小数部の値を正確に表すことがない場合と同様 (1/3 などまたはMath.PI)、バイナリの分数は、いくつかの小数部の値を表すことはできません。Just as decimal fractions are unable to precisely represent some fractional values (such as 1/3 or Math.PI), binary fractions are unable to represent some fractional values. たとえば、2 小数として.2 で正確に表される、10 は、無限大パターン「1100」繰り返しのバイナリの分数として.0011111001001100 で表されます。For example, 2/10, which is represented precisely by .2 as a decimal fraction, is represented by .0011111001001100 as a binary fraction, with the pattern "1100" repeating to infinity. この場合、浮動小数点値は、それが表す番号の不正確な表現を提供します。In this case, the floating-point value provides an imprecise representation of the number that it represents. 多くの場合、元の浮動小数点値で追加の数学的な操作を実行すると、その有効桁数が不足しているが増加します。Performing additional mathematical operations on the original floating-point value often increases its lack of precision. たとえば、乗算よりも 8 個より多くの操作が伴うのでは場合.3 で 10 を乗算した結果を比較して、.3 に.3 を 9 回追加すると、その追加が表示されますより精度の低いの結果を生成しています。For example, if you compare the results of multiplying .3 by 10 and adding .3 to .3 nine times, you will see that addition produces the less precise result, because it involves eight more operations than multiplication. このような違いは明らかなは、2 つを表示する場合にのみ、 Single "R"を使用して値標準数値書式指定文字列であり、必要に応じてがすべて 9 桁でサポートされている有効桁数を表示するかどうか、Single型。Note that this disparity is apparent only if you display the two Single values by using the "R" standard numeric format string, which, if necessary, displays all 9 digits of precision supported by the Single type.

using System;

public class Example
{
   public static void Main()
   {
      Single value = .2f;
      Single result1 = value * 10f;
      Single result2 = 0f;
      for (int ctr = 1; ctr <= 10; ctr++)
         result2 += value;

      Console.WriteLine(".2 * 10:           {0:R}", result1);
      Console.WriteLine(".2 Added 10 times: {0:R}", result2);
   }
}
// The example displays the following output:
//       .2 * 10:           2
//       .2 Added 10 times: 2.00000024
Module Example
   Public Sub Main()
      Dim value As Single = .2
      Dim result1 As Single = value * 10
      Dim result2 As Single
      For ctr As Integer = 1 To 10
         result2 += value
      Next
      Console.WriteLine(".2 * 10:           {0:R}", result1)
      Console.WriteLine(".2 Added 10 times: {0:R}", result2)
   End Sub
End Module
' The example displays the following output:
'       .2 * 10:           2
'       .2 Added 10 times: 2.00000024

いくつかの数字は、バイナリ値の小数部として正確に表されることはできません、ため、浮動小数点数には、おおよその実数のみことができます。Because some numbers cannot be represented exactly as fractional binary values, floating-point numbers can only approximate real numbers.

すべての浮動小数点数もどれだけ正確に浮動小数点値に近い実数を決定する有効桁数には、限られています。All floating-point numbers have a limited number of significant digits, which also determines how accurately a floating-point value approximates a real number. ASingle最大 9 桁が内部的に保持されて、値が最大 7 桁の有効桁数。A Single value has up to 7 decimal digits of precision, although a maximum of 9 digits is maintained internally. これは、いくつかの浮動小数点演算の精度浮動小数点値を変更することがありますがないことを意味します。This means that some floating-point operations may lack the precision to change a floating-point value. 次の例は、大規模な単精度浮動小数点値を定義しの製品を追加し、Single.Epsilonと 1,000兆にします。The following example defines a large single-precision floating-point value, and then adds the product of Single.Epsilon and one quadrillion to it. ただし、製品は、元の浮動小数点値を変更するには小さすぎます。However, the product is too small to modify the original floating-point value. その最下位の桁は 1/10、1/100、1/1000、製品の最上位桁は 10-30します。Its least significant digit is thousandths, whereas the most significant digit in the product is 10-30.

using System;

public class Example
{
   public static void Main()
   {
      Single value = 123.456f;
      Single additional = Single.Epsilon * 1e15f;
      Console.WriteLine($"{value} + {additional} = {value + additional}");
   }
}
// The example displays the following output:
//    123.456 + 1.401298E-30 = 123.456
Module Example
   Public Sub Main()
      Dim value As Single = 123.456
      Dim additional As Single = Single.Epsilon * 1e15
      Console.WriteLine($"{value} + {additional} = {value + additional}")
   End Sub
End Module
' The example displays the following output:
'   123.456 + 1.401298E-30 = 123.456

浮動小数点数の制限された有効桁数には、複数の結果があります。The limited precision of a floating-point number has several consequences:

  • 特定の有効桁数で等しく見える 2 つの浮動小数点数が等しくない有効桁数が異なるためです。Two floating-point numbers that appear equal for a particular precision might not compare equal because their least significant digits are different. 次の例では、一連の数字を加算し、その合計が、予想される合計と比較されます。In the following example, a series of numbers are added together, and their total is compared with their expected total. 2 つの値は同じであるへの呼び出しに見えますが、Equalsメソッドがないことを示します。Although the two values appear to be the same, a call to the Equals method indicates that they are not.

    using System;
    
    public class Example
    {
       public static void Main()
       {
          Single[] values = { 10.01f, 2.88f, 2.88f, 2.88f, 9.0f };
          Single result = 27.65f;
          Single total = 0f;
          foreach (var value in values)
             total += value;
    
          if (total.Equals(result))
             Console.WriteLine("The sum of the values equals the total.");
          else
             Console.WriteLine("The sum of the values ({0}) does not equal the total ({1}).",
                               total, result); 
       }
    }
    // The example displays the following output:
    //      The sum of the values (27.65) does not equal the total (27.65).   
    //
    // If the index items in the Console.WriteLine statement are changed to {0:R},
    // the example displays the following output:
    //       The sum of the values (27.6500015) does not equal the total (27.65).   
    
    Module Example
       Public Sub Main()
          Dim values() As Single = { 10.01, 2.88, 2.88, 2.88, 9.0 }
          Dim result As Single = 27.65
          Dim total As Single
          For Each value In values
             total += value
          Next
          If total.Equals(result) Then
             Console.WriteLine("The sum of the values equals the total.")
          Else
             Console.WriteLine("The sum of the values ({0}) does not equal the total ({1}).",
                               total, result) 
          End If     
       End Sub
    End Module
    ' The example displays the following output:
    '      The sum of the values (27.65) does not equal the total (27.65).   
    '
    ' If the index items in the Console.WriteLine statement are changed to {0:R},
    ' the example displays the following output:
    '       The sum of the values (27.639999999999997) does not equal the total (27.64).   
    

    書式項目を変更する場合、Console.WriteLine(String, Object, Object)ステートメントから{0}{1}{0:R}{1:R}、2 つのすべての有効桁数を表示するSingle値、2 つの値が等しくないことは明らかですので、追加の操作中に有効桁数が失われる。If you change the format items in the Console.WriteLine(String, Object, Object) statement from {0} and {1} to {0:R} and {1:R} to display all significant digits of the two Single values, it is clear that the two values are unequal because of a loss of precision during the addition operations. 問題を解決して呼び出すことによって、ここで、Math.Round(Double, Int32)丸めるメソッド、Single比較を実行する前に必要な有効桁数の値。In this case, the issue can be resolved by calling the Math.Round(Double, Int32) method to round the Single values to the desired precision before performing the comparison.

  • 浮動小数点数を使用する数学的演算または比較操作が生成されない、同じ結果を 10 進数を使用する場合バイナリ浮動小数点数が 10 進数と等しくないため、します。A mathematical or comparison operation that uses a floating-point number might not yield the same result if a decimal number is used, because the binary floating-point number might not equal the decimal number. 前の例では、.3 で 10 を乗算することと.3 に.3 9 回の追加の結果を表示することでこれを説明します。A previous example illustrated this by displaying the result of multiplying .3 by 10 and adding .3 to .3 nine times.

    小数部の値を持つ数値演算の精度が重要で、使用、Decimal型の代わりに、Single型。When accuracy in numeric operations with fractional values is important, use the Decimal type instead of the Single type. ときに精度数値演算の範囲外の整数値で、Int64またはUInt64使用の種類は重要では、BigInteger型。When accuracy in numeric operations with integral values beyond the range of the Int64 or UInt64 types is important, use the BigInteger type.

  • 値のラウンドト リップしない場合は、浮動小数点数が含まれている可能性があります。A value might not round-trip if a floating-point number is involved. 操作は、元の浮動小数点数を別の形式に変換します、逆の操作が、浮動小数点数に変換されたフォームを変換し、最終的な浮動小数点数が、元と等しい場合、値がラウンドト リップさせると言います浮動小数点数。A value is said to round-trip if an operation converts an original floating-point number to another form, an inverse operation transforms the converted form back to a floating-point number, and the final floating-point number is equal to the original floating-point number. 1 つ以上の最小有効桁数が紛失または変換で変更されたために、ラウンド トリップが失敗することがあります。The round trip might fail because one or more least significant digits are lost or changed in a conversion. 次の例では、次の 3 つSingle値が文字列に変換され、ファイルに保存します。In the following example, three Single values are converted to strings and saved in a file. 出力を同一である値が表示されますが、復元された値が元の値に等しくないです。As the output shows, although the values appear to be identical, the restored values are not equal to the original values.

    using System;
    using System.IO;
    
    public class Example
    {
       public static void Main()
       {
          StreamWriter sw = new StreamWriter(@".\Singles.dat");
          Single[] values = { 3.2f/1.11f, 1.0f/3f, (float) Math.PI };
          for (int ctr = 0; ctr < values.Length; ctr++) {
             sw.Write(values[ctr].ToString());
             if (ctr != values.Length - 1)
                sw.Write("|");
          }      
          sw.Close();
          
          Single[] restoredValues = new Single[values.Length];
          StreamReader sr = new StreamReader(@".\Singles.dat");
          string temp = sr.ReadToEnd();
          string[] tempStrings = temp.Split('|');
          for (int ctr = 0; ctr < tempStrings.Length; ctr++)
             restoredValues[ctr] = Single.Parse(tempStrings[ctr]);   
    
    
          for (int ctr = 0; ctr < values.Length; ctr++)
             Console.WriteLine("{0} {2} {1}", values[ctr], 
                               restoredValues[ctr],
                               values[ctr].Equals(restoredValues[ctr]) ? "=" : "<>");
       }
    }
    // The example displays the following output:
    //       2.882883 <> 2.882883
    //       0.3333333 <> 0.3333333
    //       3.141593 <> 3.141593
    
    Imports System.IO
    
    Module Example
       Public Sub Main()
          Dim sw As New StreamWriter(".\Singles.dat")
          Dim values() As Single = { 3.2/1.11, 1.0/3, CSng(Math.PI)  }
          For ctr As Integer = 0 To values.Length - 1
             sw.Write(values(ctr).ToString())
             If ctr <> values.Length - 1 Then sw.Write("|")
          Next      
          sw.Close()
          
          Dim restoredValues(values.Length - 1) As Single
          Dim sr As New StreamReader(".\Singles.dat")
          Dim temp As String = sr.ReadToEnd()
          Dim tempStrings() As String = temp.Split("|"c)
          For ctr As Integer = 0 To tempStrings.Length - 1
             restoredValues(ctr) = Single.Parse(tempStrings(ctr))   
          Next 
    
          For ctr As Integer = 0 To values.Length - 1
             Console.WriteLine("{0} {2} {1}", values(ctr), 
                               restoredValues(ctr),
                               If(values(ctr).Equals(restoredValues(ctr)), "=", "<>"))
          Next
       End Sub
    End Module
    ' The example displays the following output:
    '        2.882883 <> 2.882883
    '        0.3333333 <> 0.3333333
    '        3.141593 <> 3.141593
    

    ここで、値は正常にラウンドト リップ"G9"を使用して標準数値書式指定文字列の完全な有効桁数を保持するためにSingle値は、次の例を示します。In this case, the values can be successfully round-tripped by using the "G9" standard numeric format string to preserve the full precision of Single values, as the following example shows.

    using System;
    using System.IO;
    
    public class Example
    {
       public static void Main()
       {
          StreamWriter sw = new StreamWriter(@".\Singles.dat");
          Single[] values = { 3.2f/1.11f, 1.0f/3f, (float) Math.PI };
          for (int ctr = 0; ctr < values.Length; ctr++) 
             sw.Write("{0:G9}{1}", values[ctr], ctr < values.Length - 1 ? "|" : "" );
          
          sw.Close();
          
          Single[] restoredValues = new Single[values.Length];
          StreamReader sr = new StreamReader(@".\Singles.dat");
          string temp = sr.ReadToEnd();
          string[] tempStrings = temp.Split('|');
          for (int ctr = 0; ctr < tempStrings.Length; ctr++)
             restoredValues[ctr] = Single.Parse(tempStrings[ctr]);   
    
    
          for (int ctr = 0; ctr < values.Length; ctr++)
             Console.WriteLine("{0} {2} {1}", values[ctr], 
                               restoredValues[ctr],
                               values[ctr].Equals(restoredValues[ctr]) ? "=" : "<>");
       }
    }
    // The example displays the following output:
    //       2.882883 = 2.882883
    //       0.3333333 = 0.3333333
    //       3.141593 = 3.141593
    
    Imports System.IO
    
    Module Example
       Public Sub Main()
          Dim sw As New StreamWriter(".\Singles.dat")
          Dim values() As Single = { 3.2/1.11, 1.0/3, CSng(Math.PI)  }
          For ctr As Integer = 0 To values.Length - 1
             sw.Write("{0:G9}{1}", values(ctr), 
                      If(ctr < values.Length - 1, "|", ""))
          Next      
          sw.Close()
          
          Dim restoredValues(values.Length - 1) As Single
          Dim sr As New StreamReader(".\Singles.dat")
          Dim temp As String = sr.ReadToEnd()
          Dim tempStrings() As String = temp.Split("|"c)
          For ctr As Integer = 0 To tempStrings.Length - 1
             restoredValues(ctr) = Single.Parse(tempStrings(ctr))   
          Next 
    
          For ctr As Integer = 0 To values.Length - 1
             Console.WriteLine("{0} {2} {1}", values(ctr), 
                               restoredValues(ctr),
                               If(values(ctr).Equals(restoredValues(ctr)), "=", "<>"))
          Next
       End Sub
    End Module
    ' The example displays the following output:
    '       2.882883 = 2.882883
    '       0.3333333 = 0.3333333
    '       3.141593 = 3.141593
    
  • Single 値があるよりも精度が低いDouble値。Single values have less precision than Double values. ASingle一見同等に変換される値Double多くの場合と一致しません、Double有効桁数の違いのための値。A Single value that is converted to a seemingly equivalent Double often does not equal the Double value because of differences in precision. 次の例では、同一の除算演算の結果が割り当てられている、Double値とSingle値。In the following example, the result of identical division operations is assigned to a Double value and a Single value. 後に、Singleに値がキャストをDouble、2 つの値の比較は、それらが等しくないことを示しています。After the Single value is cast to a Double, a comparison of the two values shows that they are unequal.

    using System;
    
    public class Example
    {
       public static void Main()
       {
          Double value1 = 1/3.0;
          Single sValue2 = 1/3.0f;
          Double value2 = (Double) sValue2;
          Console.WriteLine("{0:R} = {1:R}: {2}", value1, value2, 
                                              value1.Equals(value2));
       }
    }
    // The example displays the following output:
    //        0.33333333333333331 = 0.3333333432674408: False
    
    Module Example
       Public Sub Main()
          Dim value1 As Double = 1/3
          Dim sValue2 As Single = 1/3
          Dim value2 As Double = CDbl(sValue2)
          Console.WriteLine("{0} = {1}: {2}", value1, value2, value1.Equals(value2))
       End Sub
    End Module
    ' The example displays the following output:
    '       0.33333333333333331 = 0.3333333432674408: False
    

    この問題を回避するために使用するか、Doubleデータの型の代わりに、Singleデータ型、または使用して、Roundメソッド両方の値が同じの有効桁数を含めるようにします。To avoid this problem, either use the Double data type in place of the Single data type, or use the Round method so that both values have the same precision.

等しいかどうかテストTesting for equality

等しいと見なされる 2 つSingle値が同一の値を表す必要があります。To be considered equal, two Single values must represent identical values. ただし、有効桁数値の間の違いにより、または 1 つまたは両方の値によって精度が失われるのためには、多くの場合と同じで予想される浮動小数点値に有効桁数の違いによりに等しくない場合します。However, because of differences in precision between values, or because of a loss of precision by one or both values, floating-point values that are expected to be identical often turn out to be unequal due to differences in their least significant digits. 結果として、呼び出し、Equalsする 2 つの値が等しいかどうかを判断するメソッドまたはへの呼び出し、CompareToする 2 つの間のリレーションシップを判断するメソッドSingle値、多くの場合、予期しない結果を生成します。As a result, calls to the Equals method to determine whether two values are equal, or calls to the CompareTo method to determine the relationship between two Single values, often yield unexpected results. 明らかに等しい 2 つ、これは次の例では、明らかなSingle値が等しくないと、2 番目の値では、9 は、最初の値がある 7 桁の精度、ため、わかった。This is evident in the following example, where two apparently equal Single values turn out to be unequal, because the first value has 7 digits of precision, whereas the second value has 9.

using System;

public class Example
{
   public static void Main()
   {
      float value1 = .3333333f;
      float value2 = 1.0f/3;
      Console.WriteLine("{0:R} = {1:R}: {2}", value1, value2, value1.Equals(value2));
   }
}
// The example displays the following output:
//        0.3333333 = 0.333333343: False
Module Example
   Public Sub Main()
      Dim value1 As Single = .3333333
      Dim value2 As Single = 1/3
      Console.WriteLine("{0:R} = {1:R}: {2}", value1, value2, value1.Equals(value2))
   End Sub
End Module
' The example displays the following output:
'       0.3333333 = 0.333333343: False

等しくないと計算値を次のコード パスが異なると、多くの場合、さまざまな方法で操作するを証明します。Calculated values that follow different code paths and that are manipulated in different ways often prove to be unequal. 次の例では、1 つSingle値が 2 乗し、元の値を復元する平方根が計算されます。In the following example, one Single value is squared, and then the square root is calculated to restore the original value. 1 秒あたりSingle3.51 で乗算され、前に、結果の平方根が元の値を復元する 3.51 で割った値を 2 乗します。A second Single is multiplied by 3.51 and squared before the square root of the result is divided by 3.51 to restore the original value. 同じに見えますが、2 つの値への呼び出し、Equals(Single)メソッドでは、それらが等しくないことを示します。Although the two values appear to be identical, a call to the Equals(Single) method indicates that they are not equal. それぞれの有効桁数がすべてを表示する結果文字列を返す"G9"標準書式指定文字列を使用してSingle値は、2 番目の値が.0000000000001 を示しています。 最初よりも少ない。Using the "G9" standard format string to return a result string that displays all the significant digits of each Single value shows that the second value is .0000000000001 less than the first.

using System;

public class Example
{
   public static void Main()
   {
      float value1 = 10.201438f;
      value1 = (float) Math.Sqrt((float) Math.Pow(value1, 2));
      float value2 = (float) Math.Pow((float) value1 * 3.51f, 2);
      value2 = ((float) Math.Sqrt(value2)) / 3.51f;
      Console.WriteLine("{0} = {1}: {2}\n", 
                        value1, value2, value1.Equals(value2)); 
      Console.WriteLine("{0:G9} = {1:G9}", value1, value2); 
   }
}
// The example displays the following output:
//       10.20144 = 10.20144: False
//       
//       10.201438 = 10.2014389
Module Example
   Public Sub Main()
      Dim value1 As Single = 10.201438
      value1 = CSng(Math.Sqrt(CSng(Math.Pow(value1, 2))))
      Dim value2 As Single = CSng(Math.Pow(value1 * CSng(3.51), 2))
      value2 = CSng(Math.Sqrt(value2) / CSng(3.51))
      Console.WriteLine("{0} = {1}: {2}", 
                        value1, value2, value1.Equals(value2)) 
      Console.WriteLine()
      Console.WriteLine("{0:G9} = {1:G9}", value1, value2) 
   End Sub
End Module
' The example displays the following output:
'       10.20144 = 10.20144: False
'       
'       10.201438 = 10.2014389

精度の損失が比較の結果に影響する可能性がある場合、呼び出す代わりに、次の手法を使用することができます、EqualsまたはCompareToメソッド。In cases where a loss of precision is likely to affect the result of a comparison, you can use the following techniques instead of calling the Equals or CompareTo method:

  • 呼び出す、Math.Round両方の値は、同じ精度でいることを確認します。Call the Math.Round method to ensure that both values have the same precision. 次の例は、2 つの小数部の値が等しいされるように、このアプローチを使用する前の例を変更します。The following example modifies a previous example to use this approach so that two fractional values are equivalent.

    using System;
    
    public class Example
    {
       public static void Main()
       {
          float value1 = .3333333f;
          float value2 = 1.0f/3;
          int precision = 7;
          value1 = (float) Math.Round(value1, precision);
          value2 = (float) Math.Round(value2, precision);
          Console.WriteLine("{0:R} = {1:R}: {2}", value1, value2, value1.Equals(value2));
       }
    }
    // The example displays the following output:
    //        0.3333333 = 0.3333333: True
    
    Module Example
       Public Sub Main()
          Dim value1 As Single = .3333333
          Dim value2 As Single = 1/3
          Dim precision As Integer = 7
          value1 = CSng(Math.Round(value1, precision))
          value2 = CSng(Math.Round(value2, precision))
          Console.WriteLine("{0:R} = {1:R}: {2}", value1, value2, value1.Equals(value2))
       End Sub
    End Module
    ' The example displays the following output:
    '       0.3333333 = 0.3333333: True
    

    中間値の丸め処理に精度の問題が引き続き適用されることに注意してください。Note that the problem of precision still applies to rounding of midpoint values. 詳細については、Math.Round(Double, Int32, MidpointRounding) メソッドを参照してください。For more information, see the Math.Round(Double, Int32, MidpointRounding) method.

  • 等値ではなくおおよそ等しいかどうかをテストします。Test for approximate equality instead of equality. この手法では、いずれか絶対パスを定義する必要があります量を 2 つの値は異なるが、引き続きに等しいか、またはをより小さい値が大きい方の値から分岐できます相対的な量を定義します。This technique requires that you define either an absolute amount by which the two values can differ but still be equal, or that you define a relative amount by which the smaller value can diverge from the larger value.

    警告

    Single.Epsilon 2 つの間の距離の絶対的な測定値として提供される場合がありますSingle等しいかどうかをテストするときの値します。Single.Epsilon is sometimes used as an absolute measure of the distance between two Single values when testing for equality. ただし、Single.Epsilon追加、またはから減算できる最小有効値を測定するSingle値が 0 です。However, Single.Epsilon measures the smallest possible value that can be added to, or subtracted from, a Single whose value is zero. ほとんどの正と負Single値、値のSingle.Epsilonが小さすぎてを検出します。For most positive and negative Single values, the value of Single.Epsilon is too small to be detected. そのため、ゼロ値を除くしないでの等価性テストで使用します。Therefore, except for values that are zero, we do not recommend its use in tests for equality.

    次の例では、後者のアプローチを使用して、定義、 IsApproximatelyEqual 2 つの値の相対的な差をテストするメソッド。The following example uses the latter approach to define an IsApproximatelyEqual method that tests the relative difference between two values. 呼び出しの結果も対照的です、IsApproximatelyEqualメソッドとEquals(Single)メソッド。It also contrasts the result of calls to the IsApproximatelyEqual method and the Equals(Single) method.

    using System;
    
    public class Example
    {
       public static void Main()
       {
          float one1 = .1f * 10;
          float one2 = 0f;
          for (int ctr = 1; ctr <= 10; ctr++)
             one2 += .1f;
    
          Console.WriteLine("{0:R} = {1:R}: {2}", one1, one2, one1.Equals(one2));
          Console.WriteLine("{0:R} is approximately equal to {1:R}: {2}", 
                            one1, one2, 
                            IsApproximatelyEqual(one1, one2, .000001f));   
       }
    
       static bool IsApproximatelyEqual(float value1, float value2, float epsilon)
       {
          // If they are equal anyway, just return True.
          if (value1.Equals(value2))
             return true;
    
          // Handle NaN, Infinity.
          if (Double.IsInfinity(value1) | Double.IsNaN(value1))
             return value1.Equals(value2);
          else if (Double.IsInfinity(value2) | Double.IsNaN(value2))
             return value1.Equals(value2);
    
          // Handle zero to avoid division by zero
          double divisor = Math.Max(value1, value2);
          if (divisor.Equals(0)) 
             divisor = Math.Min(value1, value2);
          
          return Math.Abs(value1 - value2)/divisor <= epsilon;           
       } 
    }
    // The example displays the following output:
    //       1 = 1.00000012: False
    //       1 is approximately equal to 1.00000012: True
    
    Module Example
       Public Sub Main()
          Dim one1 As Single = .1 * 10
          Dim one2 As Single = 0
          For ctr As Integer = 1 To 10
             one2 += CSng(.1)
          Next
          Console.WriteLine("{0:R} = {1:R}: {2}", one1, one2, one1.Equals(one2))
          Console.WriteLine("{0:R} is approximately equal to {1:R}: {2}", 
                            one1, one2, 
                            IsApproximatelyEqual(one1, one2, .000001))   
       End Sub
    
       Function IsApproximatelyEqual(value1 As Single, value2 As Single, 
                                     epsilon As Single) As Boolean
          ' If they are equal anyway, just return True.
          If value1.Equals(value2) Then Return True
          
          ' Handle NaN, Infinity.
          If Single.IsInfinity(value1) Or Single.IsNaN(value1) Then
             Return value1.Equals(value2)
          Else If Single.IsInfinity(value2) Or Single.IsNaN(value2)
             Return value1.Equals(value2)
          End If
          
          ' Handle zero to avoid division by zero
          Dim divisor As Single = Math.Max(value1, value2)
          If divisor.Equals(0) Then
             divisor = Math.Min(value1, value2)
          End If 
          
          Return Math.Abs(value1 - value2)/divisor <= epsilon           
       End Function
    End Module
    ' The example displays the following output:
    '       1 = 1.00000012: False
    '       1 is approximately equal to 1.00000012: True
    

浮動小数点値と例外Floating-point values and exceptions

浮動小数点値を持つ操作は、整数型の 0 またはオーバーフローによる除算などの無効な操作の場合に例外をスローする操作とは異なり、例外をスローしません。Operations with floating-point values do not throw exceptions, unlike operations with integral types, which throw exceptions in cases of illegal operations such as division by zero or overflow. 代わりに、このような場合は、浮動小数点演算の結果は 0、正の無限大、負の無限大、または非数 (NaN)。Instead, in these situations, the result of a floating-point operation is zero, positive infinity, negative infinity, or not a number (NaN):

  • 浮動小数点演算の結果が小さすぎる変換先の形式の場合、結果は 0 です。If the result of a floating-point operation is too small for the destination format, the result is zero. これは、次の例のように 2 つの非常に小さい浮動小数点数値を乗算される場合に発生します。This can occur when two very small floating-point numbers are multiplied, as the following example shows.

    using System;
    
    public class Example
    {
       public static void Main()
       {
          float value1 = 1.163287e-36f;
          float value2 = 9.164234e-25f;
          float result = value1 * value2;
          Console.WriteLine("{0} * {1} = {2}", value1, value2, result);
          Console.WriteLine("{0} = 0: {1}", result, result.Equals(0.0f));
       }
    }
    // The example displays the following output:
    //       1.163287E-36 * 9.164234E-25 = 0
    //       0 = 0: True
    
    Module Example
       Public Sub Main()
          Dim value1 As Single = 1.163287e-36
          Dim value2 As Single = 9.164234e-25
          Dim result As Single = value1 * value2
          Console.WriteLine("{0} * {1} = {2:R}", value1, value2, result)
          Console.WriteLine("{0} = 0: {1}", result, result.Equals(0))
       End Sub
    End Module
    ' The example displays the following output:
    '       1.163287E-36 * 9.164234E-25 = 0
    '       0 = 0: True
    
  • 浮動小数点演算の結果の大きさが変換先の形式の範囲を超えている場合、操作の結果はPositiveInfinityまたはNegativeInfinity、必要に応じて、結果の符号にします。If the magnitude of the result of a floating-point operation exceeds the range of the destination format, the result of the operation is PositiveInfinity or NegativeInfinity, as appropriate for the sign of the result. オーバーフローが発生する操作の結果Single.MaxValuePositiveInfinity、オーバーフローが発生する操作の結果とSingle.MinValueNegativeInfinity次の例に示すように、します。The result of an operation that overflows Single.MaxValue is PositiveInfinity, and the result of an operation that overflows Single.MinValue is NegativeInfinity, as the following example shows.

    using System;
    
    public class Example
    {
       public static void Main()
       {
          float value1 = 3.065e35f;
          float value2 = 6.9375e32f;
          float result = value1 * value2;
          Console.WriteLine("PositiveInfinity: {0}", 
                             Single.IsPositiveInfinity(result));
          Console.WriteLine("NegativeInfinity: {0}\n", 
                            Single.IsNegativeInfinity(result));
    
          value1 = -value1;
          result = value1 * value2;
          Console.WriteLine("PositiveInfinity: {0}", 
                             Single.IsPositiveInfinity(result));
          Console.WriteLine("NegativeInfinity: {0}", 
                            Single.IsNegativeInfinity(result));
       }
    }                                                                 
    
    // The example displays the following output:
    //       PositiveInfinity: True
    //       NegativeInfinity: False
    //       
    //       PositiveInfinity: False
    //       NegativeInfinity: True
    
    Module Example
       Public Sub Main()
          Dim value1 As Single = 3.065e35
          Dim value2 As Single = 6.9375e32
          Dim result As Single = value1 * value2
          Console.WriteLine("PositiveInfinity: {0}", 
                             Single.IsPositiveInfinity(result))
          Console.WriteLine("NegativeInfinity: {0}", 
                            Single.IsNegativeInfinity(result))
          Console.WriteLine()                  
          value1 = -value1
          result = value1 * value2
          Console.WriteLine("PositiveInfinity: {0}", 
                             Single.IsPositiveInfinity(result))
          Console.WriteLine("NegativeInfinity: {0}", 
                            Single.IsNegativeInfinity(result))
       End Sub
    End Module
    ' The example displays the following output:
    '       PositiveInfinity: True
    '       NegativeInfinity: False
    '       
    '       PositiveInfinity: False
    '       NegativeInfinity: True
    

    PositiveInfinity 正の被除数と 0 で除算によっても発生し、NegativeInfinity被除数を負の値に 0 による除算の結果します。PositiveInfinity also results from a division by zero with a positive dividend, and NegativeInfinity results from a division by zero with a negative dividend.

  • 浮動小数点演算が有効でない場合、操作の結果はNaNします。If a floating-point operation is invalid, the result of the operation is NaN. たとえば、NaN次の操作の結果します。For example, NaN results from the following operations:

    • 0 の被除数とゼロによる除算。Division by zero with a dividend of zero. いずれかで結果がゼロで除算の他のケースに注意してくださいPositiveInfinityまたはNegativeInfinityします。Note that other cases of division by zero result in either PositiveInfinity or NegativeInfinity.

    • 無効な入力の浮動小数点演算には。Any floating-point operation with invalid input. 負の値の平方根を返しますを検索しようとしてなどNaNします。For example, attempting to find the square root of a negative value returns NaN.

    • 値が引数を持つすべての操作Single.NaNします。Any operation with an argument whose value is Single.NaN.

型変換と 1 つの構造Type conversions and the Single structure

Single構造体は、明示的または暗黙的な変換演算子を定義していません; 代わりに、変換は、コンパイラによって実装されます。The Single structure does not define any explicit or implicit conversion operators; instead, conversions are implemented by the compiler.

次の表は、他のプリミティブ数値型に値の可能な変換をSingle値も示されますかどうかと、変換を拡大または縮小するかどうか、その結果Singleよりも精度が低い場合があります、元の値。The following table lists the possible conversions of a value of the other primitive numeric types to a Single value, It also indicates whether the conversion is widening or narrowing and whether the resulting Single may have less precision than the original value.

変換 (Conversion from 拡大/縮小Widening/narrowing 有効桁数の損失の可能性ありPossible loss of precision
Byte WideningWidening ×No
Decimal WideningWidening

(C#) が必要であるキャスト演算子に注意してください。Note that C# requires a cast operator.
はい。Yes. Decimal 29 桁の精度をサポートしていますSingle 9 をサポートしています。Decimal supports 29 decimal digits of precision; Single supports 9.
Double 縮小変換します。範囲外の値に変換されますDouble.NegativeInfinityまたはDouble.PositiveInfinityします。Narrowing; out-of-range values are converted to Double.NegativeInfinity or Double.PositiveInfinity. はい。Yes. Double 17 桁の精度をサポートしていますSingle 9 をサポートしています。Double supports 17 decimal digits of precision; Single supports 9.
Int16 WideningWidening ×No
Int32 WideningWidening はい。Yes. Int32 単精度の 10 個の 10 進数字をサポートしていますSingle 9 をサポートしています。Int32 supports 10 decimal digits of precision; Single supports 9.
Int64 WideningWidening はい。Yes. Int64 19 桁の精度をサポートしていますSingle 9 をサポートしています。Int64 supports 19 decimal digits of precision; Single supports 9.
SByte WideningWidening ×No
UInt16 WideningWidening ×No
UInt32 WideningWidening はい。Yes. UInt32 単精度の 10 個の 10 進数字をサポートしていますSingle 9 をサポートしています。UInt32 supports 10 decimal digits of precision; Single supports 9.
UInt64 WideningWidening はい。Yes. Int64 20 桁の精度をサポートしていますSingle 9 をサポートしています。Int64 supports 20 decimal digits of precision; Single supports 9.

次の例に、他のプリミティブ数値型の最小値または最大値の変換、Single値。The following example converts the minimum or maximum value of other primitive numeric types to a Single value.

using System;

public class Example
{
   public static void Main()
   {
      dynamic[] values = { Byte.MinValue, Byte.MaxValue, Decimal.MinValue,
                           Decimal.MaxValue, Double.MinValue, Double.MaxValue,
                           Int16.MinValue, Int16.MaxValue, Int32.MinValue,
                           Int32.MaxValue, Int64.MinValue, Int64.MaxValue,
                           SByte.MinValue, SByte.MaxValue, UInt16.MinValue,
                           UInt16.MaxValue, UInt32.MinValue, UInt32.MaxValue,
                           UInt64.MinValue, UInt64.MaxValue };
      float sngValue;
      foreach (var value in values) {
         if (value.GetType() == typeof(Decimal) ||
             value.GetType() == typeof(Double))
            sngValue = (float) value;
         else
            sngValue = value;
         Console.WriteLine("{0} ({1}) --> {2:R} ({3})",
                           value, value.GetType().Name,
                           sngValue, sngValue.GetType().Name);
      }
   }
}
// The example displays the following output:
//       0 (Byte) --> 0 (Single)
//       255 (Byte) --> 255 (Single)
//       -79228162514264337593543950335 (Decimal) --> -7.92281625E+28 (Single)
//       79228162514264337593543950335 (Decimal) --> 7.92281625E+28 (Single)
//       -1.79769313486232E+308 (Double) --> -Infinity (Single)
//       1.79769313486232E+308 (Double) --> Infinity (Single)
//       -32768 (Int16) --> -32768 (Single)
//       32767 (Int16) --> 32767 (Single)
//       -2147483648 (Int32) --> -2.14748365E+09 (Single)
//       2147483647 (Int32) --> 2.14748365E+09 (Single)
//       -9223372036854775808 (Int64) --> -9.223372E+18 (Single)
//       9223372036854775807 (Int64) --> 9.223372E+18 (Single)
//       -128 (SByte) --> -128 (Single)
//       127 (SByte) --> 127 (Single)
//       0 (UInt16) --> 0 (Single)
//       65535 (UInt16) --> 65535 (Single)
//       0 (UInt32) --> 0 (Single)
//       4294967295 (UInt32) --> 4.2949673E+09 (Single)
//       0 (UInt64) --> 0 (Single)
//       18446744073709551615 (UInt64) --> 1.84467441E+19 (Single)
Module Example
   Public Sub Main()
      Dim values() As Object = { Byte.MinValue, Byte.MaxValue, Decimal.MinValue,
                                 Decimal.MaxValue, Double.MinValue, Double.MaxValue,
                                 Int16.MinValue, Int16.MaxValue, Int32.MinValue,
                                 Int32.MaxValue, Int64.MinValue, Int64.MaxValue,
                                 SByte.MinValue, SByte.MaxValue, UInt16.MinValue,
                                 UInt16.MaxValue, UInt32.MinValue, UInt32.MaxValue,
                                 UInt64.MinValue, UInt64.MaxValue }
      Dim sngValue As Single
      For Each value In values
         If value.GetType() = GetType(Double) Then
            sngValue = CSng(value)
         Else
            sngValue = value
         End If
         Console.WriteLine("{0} ({1}) --> {2:R} ({3})",
                           value, value.GetType().Name,
                           sngValue, sngValue.GetType().Name)
      Next
   End Sub
End Module
' The example displays the following output:
'       0 (Byte) --> 0 (Single)
'       255 (Byte) --> 255 (Single)
'       -79228162514264337593543950335 (Decimal) --> -7.92281625E+28 (Single)
'       79228162514264337593543950335 (Decimal) --> 7.92281625E+28 (Single)
'       -1.79769313486232E+308 (Double) --> -Infinity (Single)
'       1.79769313486232E+308 (Double) --> Infinity (Single)
'       -32768 (Int16) --> -32768 (Single)
'       32767 (Int16) --> 32767 (Single)
'       -2147483648 (Int32) --> -2.14748365E+09 (Single)
'       2147483647 (Int32) --> 2.14748365E+09 (Single)
'       -9223372036854775808 (Int64) --> -9.223372E+18 (Single)
'       9223372036854775807 (Int64) --> 9.223372E+18 (Single)
'       -128 (SByte) --> -128 (Single)
'       127 (SByte) --> 127 (Single)
'       0 (UInt16) --> 0 (Single)
'       65535 (UInt16) --> 65535 (Single)
'       0 (UInt32) --> 0 (Single)
'       4294967295 (UInt32) --> 4.2949673E+09 (Single)
'       0 (UInt64) --> 0 (Single)
'       18446744073709551615 (UInt64) --> 1.84467441E+19 (Single)

さらに、DoubleDouble.NaNDouble.PositiveInfinity、およびDouble.NegativeInfinityを隠ぺいされたSingle.NaNSingle.PositiveInfinity、およびSingle.NegativeInfinity、それぞれします。In addition, the Double values Double.NaN, Double.PositiveInfinity, and Double.NegativeInfinity covert to Single.NaN, Single.PositiveInfinity, and Single.NegativeInfinity, respectively.

注意するいくつかの数値型の値の変換、Single値は、精度の損失を伴うことができます。Note that the conversion of the value of some numeric types to a Single value can involve a loss of precision. 変換するときに、例に示すように、精度が失われることはDecimalDoubleInt32Int64UInt32、およびUInt64Single値。As the example illustrates, a loss of precision is possible when converting Decimal, Double, Int32, Int64, UInt32, and UInt64 values to Single values.

変換、Single値をDoubleは拡大変換です。The conversion of a Single value to a Double is a widening conversion. 場合、変換で生成の有効桁数が失われる可能性があります、Doubleの正確な表現がない型、Single値。The conversion may result in a loss of precision if the Double type does not have a precise representation for the Single value.

変換、Single以外の任意のプリミティブ数値データ型の値に値をDouble縮小変換は、キャスト演算子 (C# の場合) または (Visual Basic) で変換メソッドが必要です。The conversion of a Single value to a value of any primitive numeric data type other than a Double is a narrowing conversion and requires a cast operator (in C#) or a conversion method (in Visual Basic). 対象の種類が定義されている対象のデータ型の範囲外にある値MinValueMaxValueプロパティ、動作は次の表に示すようにします。Values that are outside the range of the target data type, which are defined by the target type's MinValue and MaxValue properties, behave as shown in the following table.

変換後の型Target type 結果Result
任意の整数型Any integral type OverflowException変換は checked コンテキストで発生した場合は例外です。An OverflowException exception if the conversion occurs in a checked context.

Unchecked コンテキスト (c# で既定値) に変換する場合は、変換操作は成功しますが、値をオーバーフローしています。If the conversion occurs in an unchecked context (the default in C#), the conversion operation succeeds but the value overflows.
Decimal OverflowException例外An OverflowException exception,

さらに、 Single.NaNSingle.PositiveInfinity、およびSingle.NegativeInfinityスロー、 OverflowException checked コンテキストがこれらの値のオーバーフロー unchecked コンテキストで整数に変換されるときの整数に変換します。In addition, Single.NaN, Single.PositiveInfinity, and Single.NegativeInfinity throw an OverflowException for conversions to integers in a checked context, but these values overflow when converted to integers in an unchecked context. 変換Decimal、常にスローされます、OverflowExceptionします。For conversions to Decimal, they always throw an OverflowException. 変換Doubleには変換Double.NaNDouble.PositiveInfinity、およびDouble.NegativeInfinity、それぞれします。For conversions to Double, they convert to Double.NaN, Double.PositiveInfinity, and Double.NegativeInfinity, respectively.

変換によって精度の損失が可能性に注意してください、Singleを別の数値型の値。Note that a loss of precision may result from converting a Single value to another numeric type. 変換の整数以外の場合Single値、例の出力に示す、小数部はときに失われます、Single値が (Visual Basic) のように丸められますまたは (c#) のように切り捨てられます。In the case of converting non-integral Single values, as the output from the example shows, the fractional component is lost when the Single value is either rounded (as in Visual Basic) or truncated (as in C#). 変換Decimal値、Single値では、対象のデータ型を正確に表現できません。For conversions to Decimal values, the Single value may not have a precise representation in the target data type.

次の例の数の変換Single他のいくつかの数値型の値。The following example converts a number of Single values to several other numeric types. Visual Basic (既定値) と c# では、checked コンテキストで、変換が行われます (のため、チェックキーワード)。The conversions occur in a checked context in Visual Basic (the default) and in C# (because of the checked keyword). 例の出力が両方チェックで変換の結果を示しています unchecked コンテキスト。The output from the example shows the result for conversions in both a checked an unchecked context. コンパイルする Visual Basic で unchecked コンテキストでの変換を行うことができます、/removeintchecks+コンパイラ スイッチと c# コメント アウトすることによって、checkedステートメント。You can perform conversions in an unchecked context in Visual Basic by compiling with the /removeintchecks+ compiler switch and in C# by commenting out the checked statement.

using System;

public class Example
{
   public static void Main()
   {
      float[] values = { Single.MinValue, -67890.1234f, -12345.6789f,
                         12345.6789f, 67890.1234f, Single.MaxValue,
                         Single.NaN, Single.PositiveInfinity,
                         Single.NegativeInfinity };
      checked {
         foreach (var value in values) {
            try {
                Int64 lValue = (long) value;
                Console.WriteLine("{0} ({1}) --> {2} (0x{2:X16}) ({3})",
                                  value, value.GetType().Name,
                                  lValue, lValue.GetType().Name);
            }
            catch (OverflowException) {
               Console.WriteLine("Unable to convert {0} to Int64.", value);
            }
            try {
                UInt64 ulValue = (ulong) value;
                Console.WriteLine("{0} ({1}) --> {2} (0x{2:X16}) ({3})",
                                  value, value.GetType().Name,
                                  ulValue, ulValue.GetType().Name);
            }
            catch (OverflowException) {
               Console.WriteLine("Unable to convert {0} to UInt64.", value);
            }
            try {
                Decimal dValue = (decimal) value;
                Console.WriteLine("{0} ({1}) --> {2} ({3})",
                                  value, value.GetType().Name,
                                  dValue, dValue.GetType().Name);
            }
            catch (OverflowException) {
               Console.WriteLine("Unable to convert {0} to Decimal.", value);
            }

            Double dblValue = value;
            Console.WriteLine("{0} ({1}) --> {2} ({3})",
                              value, value.GetType().Name,
                              dblValue, dblValue.GetType().Name);
            Console.WriteLine();
         }
      }
   }
}
// The example displays the following output for conversions performed
// in a checked context:
//       Unable to convert -3.402823E+38 to Int64.
//       Unable to convert -3.402823E+38 to UInt64.
//       Unable to convert -3.402823E+38 to Decimal.
//       -3.402823E+38 (Single) --> -3.40282346638529E+38 (Double)
//
//       -67890.13 (Single) --> -67890 (0xFFFFFFFFFFFEF6CE) (Int64)
//       Unable to convert -67890.13 to UInt64.
//       -67890.13 (Single) --> -67890.12 (Decimal)
//       -67890.13 (Single) --> -67890.125 (Double)
//
//       -12345.68 (Single) --> -12345 (0xFFFFFFFFFFFFCFC7) (Int64)
//       Unable to convert -12345.68 to UInt64.
//       -12345.68 (Single) --> -12345.68 (Decimal)
//       -12345.68 (Single) --> -12345.6787109375 (Double)
//
//       12345.68 (Single) --> 12345 (0x0000000000003039) (Int64)
//       12345.68 (Single) --> 12345 (0x0000000000003039) (UInt64)
//       12345.68 (Single) --> 12345.68 (Decimal)
//       12345.68 (Single) --> 12345.6787109375 (Double)
//
//       67890.13 (Single) --> 67890 (0x0000000000010932) (Int64)
//       67890.13 (Single) --> 67890 (0x0000000000010932) (UInt64)
//       67890.13 (Single) --> 67890.12 (Decimal)
//       67890.13 (Single) --> 67890.125 (Double)
//
//       Unable to convert 3.402823E+38 to Int64.
//       Unable to convert 3.402823E+38 to UInt64.
//       Unable to convert 3.402823E+38 to Decimal.
//       3.402823E+38 (Single) --> 3.40282346638529E+38 (Double)
//
//       Unable to convert NaN to Int64.
//       Unable to convert NaN to UInt64.
//       Unable to convert NaN to Decimal.
//       NaN (Single) --> NaN (Double)
//
//       Unable to convert Infinity to Int64.
//       Unable to convert Infinity to UInt64.
//       Unable to convert Infinity to Decimal.
//       Infinity (Single) --> Infinity (Double)
//
//       Unable to convert -Infinity to Int64.
//       Unable to convert -Infinity to UInt64.
//       Unable to convert -Infinity to Decimal.
//       -Infinity (Single) --> -Infinity (Double)
// The example displays the following output for conversions performed
// in an unchecked context:
//       -3.402823E+38 (Single) --> -9223372036854775808 (0x8000000000000000) (Int64)
//       -3.402823E+38 (Single) --> 9223372036854775808 (0x8000000000000000) (UInt64)
//       Unable to convert -3.402823E+38 to Decimal.
//       -3.402823E+38 (Single) --> -3.40282346638529E+38 (Double)
//
//       -67890.13 (Single) --> -67890 (0xFFFFFFFFFFFEF6CE) (Int64)
//       -67890.13 (Single) --> 18446744073709483726 (0xFFFFFFFFFFFEF6CE) (UInt64)
//       -67890.13 (Single) --> -67890.12 (Decimal)
//       -67890.13 (Single) --> -67890.125 (Double)
//
//       -12345.68 (Single) --> -12345 (0xFFFFFFFFFFFFCFC7) (Int64)
//       -12345.68 (Single) --> 18446744073709539271 (0xFFFFFFFFFFFFCFC7) (UInt64)
//       -12345.68 (Single) --> -12345.68 (Decimal)
//       -12345.68 (Single) --> -12345.6787109375 (Double)
//
//       12345.68 (Single) --> 12345 (0x0000000000003039) (Int64)
//       12345.68 (Single) --> 12345 (0x0000000000003039) (UInt64)
//       12345.68 (Single) --> 12345.68 (Decimal)
//       12345.68 (Single) --> 12345.6787109375 (Double)
//
//       67890.13 (Single) --> 67890 (0x0000000000010932) (Int64)
//       67890.13 (Single) --> 67890 (0x0000000000010932) (UInt64)
//       67890.13 (Single) --> 67890.12 (Decimal)
//       67890.13 (Single) --> 67890.125 (Double)
//
//       3.402823E+38 (Single) --> -9223372036854775808 (0x8000000000000000) (Int64)
//       3.402823E+38 (Single) --> 0 (0x0000000000000000) (UInt64)
//       Unable to convert 3.402823E+38 to Decimal.
//       3.402823E+38 (Single) --> 3.40282346638529E+38 (Double)
//
//       NaN (Single) --> -9223372036854775808 (0x8000000000000000) (Int64)
//       NaN (Single) --> 0 (0x0000000000000000) (UInt64)
//       Unable to convert NaN to Decimal.
//       NaN (Single) --> NaN (Double)
//
//       Infinity (Single) --> -9223372036854775808 (0x8000000000000000) (Int64)
//       Infinity (Single) --> 0 (0x0000000000000000) (UInt64)
//       Unable to convert Infinity to Decimal.
//       Infinity (Single) --> Infinity (Double)
//
//       -Infinity (Single) --> -9223372036854775808 (0x8000000000000000) (Int64)
//       -Infinity (Single) --> 9223372036854775808 (0x8000000000000000) (UInt64)
//       Unable to convert -Infinity to Decimal.
//       -Infinity (Single) --> -Infinity (Double)
Module Example
   Public Sub Main()
      Dim values() As Single = { Single.MinValue, -67890.1234, -12345.6789,
                                 12345.6789, 67890.1234, Single.MaxValue,
                                 Single.NaN, Single.PositiveInfinity,
                                 Single.NegativeInfinity }
      For Each value In values
         Try
             Dim lValue As Long = CLng(value)
             Console.WriteLine("{0} ({1}) --> {2} (0x{2:X16}) ({3})",
                               value, value.GetType().Name,
                               lValue, lValue.GetType().Name)
         Catch e As OverflowException
            Console.WriteLine("Unable to convert {0} to Int64.", value)
         End Try
         Try
             Dim ulValue As UInt64 = CULng(value)
             Console.WriteLine("{0} ({1}) --> {2} (0x{2:X16}) ({3})",
                               value, value.GetType().Name,
                               ulValue, ulValue.GetType().Name)
         Catch e As OverflowException
            Console.WriteLine("Unable to convert {0} to UInt64.", value)
         End Try
         Try
             Dim dValue As Decimal = CDec(value)
             Console.WriteLine("{0} ({1}) --> {2} ({3})",
                               value, value.GetType().Name,
                               dValue, dValue.GetType().Name)
         Catch e As OverflowException
            Console.WriteLine("Unable to convert {0} to Decimal.", value)
         End Try

         Dim dblValue As Double = value
         Console.WriteLine("{0} ({1}) --> {2} ({3})",
                           value, value.GetType().Name,
                           dblValue, dblValue.GetType().Name)
         Console.WriteLine()
      Next
   End Sub
End Module
' The example displays the following output for conversions performed
' in a checked context:
'       Unable to convert -3.402823E+38 to Int64.
'       Unable to convert -3.402823E+38 to UInt64.
'       Unable to convert -3.402823E+38 to Decimal.
'       -3.402823E+38 (Single) --> -3.40282346638529E+38 (Double)
'
'       -67890.13 (Single) --> -67890 (0xFFFFFFFFFFFEF6CE) (Int64)
'       Unable to convert -67890.13 to UInt64.
'       -67890.13 (Single) --> -67890.12 (Decimal)
'       -67890.13 (Single) --> -67890.125 (Double)
'
'       -12345.68 (Single) --> -12346 (0xFFFFFFFFFFFFCFC6) (Int64)
'       Unable to convert -12345.68 to UInt64.
'       -12345.68 (Single) --> -12345.68 (Decimal)
'       -12345.68 (Single) --> -12345.6787109375 (Double)
'
'       12345.68 (Single) --> 12346 (0x000000000000303A) (Int64)
'       12345.68 (Single) --> 12346 (0x000000000000303A) (UInt64)
'       12345.68 (Single) --> 12345.68 (Decimal)
'       12345.68 (Single) --> 12345.6787109375 (Double)
'
'       67890.13 (Single) --> 67890 (0x0000000000010932) (Int64)
'       67890.13 (Single) --> 67890 (0x0000000000010932) (UInt64)
'       67890.13 (Single) --> 67890.12 (Decimal)
'       67890.13 (Single) --> 67890.125 (Double)
'
'       Unable to convert 3.402823E+38 to Int64.
'       Unable to convert 3.402823E+38 to UInt64.
'       Unable to convert 3.402823E+38 to Decimal.
'       3.402823E+38 (Single) --> 3.40282346638529E+38 (Double)
'
'       Unable to convert NaN to Int64.
'       Unable to convert NaN to UInt64.
'       Unable to convert NaN to Decimal.
'       NaN (Single) --> NaN (Double)
'
'       Unable to convert Infinity to Int64.
'       Unable to convert Infinity to UInt64.
'       Unable to convert Infinity to Decimal.
'       Infinity (Single) --> Infinity (Double)
'
'       Unable to convert -Infinity to Int64.
'       Unable to convert -Infinity to UInt64.
'       Unable to convert -Infinity to Decimal.
'       -Infinity (Single) --> -Infinity (Double)
' The example displays the following output for conversions performed
' in an unchecked context:
'       -3.402823E+38 (Single) --> -9223372036854775808 (0x8000000000000000) (Int64)
'       -3.402823E+38 (Single) --> 9223372036854775808 (0x8000000000000000) (UInt64)
'       Unable to convert -3.402823E+38 to Decimal.
'       -3.402823E+38 (Single) --> -3.40282346638529E+38 (Double)
'
'       -67890.13 (Single) --> -67890 (0xFFFFFFFFFFFEF6CE) (Int64)
'       -67890.13 (Single) --> 18446744073709483726 (0xFFFFFFFFFFFEF6CE) (UInt64)
'       -67890.13 (Single) --> -67890.12 (Decimal)
'       -67890.13 (Single) --> -67890.125 (Double)
'
'       -12345.68 (Single) --> -12346 (0xFFFFFFFFFFFFCFC6) (Int64)
'       -12345.68 (Single) --> 18446744073709539270 (0xFFFFFFFFFFFFCFC6) (UInt64)
'       -12345.68 (Single) --> -12345.68 (Decimal)
'       -12345.68 (Single) --> -12345.6787109375 (Double)
'
'       12345.68 (Single) --> 12346 (0x000000000000303A) (Int64)
'       12345.68 (Single) --> 12346 (0x000000000000303A) (UInt64)
'       12345.68 (Single) --> 12345.68 (Decimal)
'       12345.68 (Single) --> 12345.6787109375 (Double)
'
'       67890.13 (Single) --> 67890 (0x0000000000010932) (Int64)
'       67890.13 (Single) --> 67890 (0x0000000000010932) (UInt64)
'       67890.13 (Single) --> 67890.12 (Decimal)
'       67890.13 (Single) --> 67890.125 (Double)
'
'       3.402823E+38 (Single) --> -9223372036854775808 (0x8000000000000000) (Int64)
'       3.402823E+38 (Single) --> 0 (0x0000000000000000) (UInt64)
'       Unable to convert 3.402823E+38 to Decimal.
'       3.402823E+38 (Single) --> 3.40282346638529E+38 (Double)
'
'       NaN (Single) --> -9223372036854775808 (0x8000000000000000) (Int64)
'       NaN (Single) --> 0 (0x0000000000000000) (UInt64)
'       Unable to convert NaN to Decimal.
'       NaN (Single) --> NaN (Double)
'
'       Infinity (Single) --> -9223372036854775808 (0x8000000000000000) (Int64)
'       Infinity (Single) --> 0 (0x0000000000000000) (UInt64)
'       Unable to convert Infinity to Decimal.
'       Infinity (Single) --> Infinity (Double)
'
'       -Infinity (Single) --> -9223372036854775808 (0x8000000000000000) (Int64)
'       -Infinity (Single) --> 9223372036854775808 (0x8000000000000000) (UInt64)
'       Unable to convert -Infinity to Decimal.
'       -Infinity (Single) --> -Infinity (Double)

数値型の変換の詳細については、次を参照してください。 .NET Framework における型変換型変換の表します。For more information on the conversion of numeric types, see Type Conversion in the .NET Framework and Type Conversion Tables.

浮動小数点の機能Floating-point functionality

Single構造および関連する型は、次のカテゴリの操作を実行するメソッドを提供します。The Single structure and related types provide methods to perform the following categories of operations:

  • 値の比較します。Comparison of values. 呼び出すことができます、Equalsする 2 つかどうかを判断するメソッドSingle値が等しいか、またはCompareToメソッドを 2 つの値の間の関係を決定します。You can call the Equals method to determine whether two Single values are equal, or the CompareTo method to determine the relationship between two values.

    Single構造体には、比較演算子の完全なセットもサポートしています。The Single structure also supports a complete set of comparison operators. たとえば、等しいか、テストまたは 1 つの値が別の値以上かどうかを判断できます。For example, you can test for equality or inequality, or determine whether one value is greater than or equal to another value. オペランドの 1 つの場合、 DoubleSingle値に変換されます、Double比較を実行する前にします。If one of the operands is a Double, the Single value is converted to a Double before performing the comparison. 変換されます、オペランドの 1 つが整数型の場合、Single比較を実行する前にします。If one of the operands is an integral type, it is converted to a Single before performing the comparison. ただし、これらの拡大変換は、精度の損失が含まれます。Although these are widening conversions, they may involve a loss of precision.

    警告

    有効桁数の違いにより 2 つSingle値と同じと想定している可能性がありますに、等しくない場合、比較の結果に影響します。Because of differences in precision, two Single values that you expect to be equal may turn out to be unequal, which affects the result of the comparison. 参照してください、等しいかどうかテスト比較する 2 つの詳細については、セクションSingle値。See the Testing for equality section for more information about comparing two Single values.

    呼び出すことも、 IsNaNIsInfinityIsPositiveInfinity、およびIsNegativeInfinityこれらの特殊な値をテストするメソッド。You can also call the IsNaN, IsInfinity, IsPositiveInfinity, and IsNegativeInfinity methods to test for these special values.

  • 数値演算します。Mathematical operations. 加算、減算、乗算、および除算などの一般的な算術演算はではなく、言語コンパイラおよび共通中間言語 (CIL) の手順で実装されているSingleメソッド。Common arithmetic operations such as addition, subtraction, multiplication, and division are implemented by language compilers and Common Intermediate Language (CIL) instructions rather than by Single methods. 数学的演算のもう一方のオペランドの場合、 DoubleSingleに変換されます、Doubleも、操作と操作の結果を実行する前に、Double値。If the other operand in a mathematical operation is a Double, the Single is converted to a Double before performing the operation, and the result of the operation is also a Double value. もう一方のオペランドが整数型の場合に変換されます、Singleも、操作と操作の結果を実行する前に、Single値。If the other operand is an integral type, it is converted to a Single before performing the operation, and the result of the operation is also a Single value.

    他の算術演算を実行するには、呼び出すstatic(Shared Visual Basic で) 内のメソッド、System.Mathクラス。You can perform other mathematical operations by calling static (Shared in Visual Basic) methods in the System.Math class. 演算に一般的に使用される追加のメソッドが含まれます (などMath.AbsMath.Sign、およびMath.Sqrt)、geometry (などMath.CosMath.Sin)、および微積分 (などMath.Log)。These include additional methods commonly used for arithmetic (such as Math.Abs, Math.Sign, and Math.Sqrt), geometry (such as Math.Cos and Math.Sin), and calculus (such as Math.Log). すべてのケースで、Single値に変換されます、Doubleします。In all cases, the Single value is converted to a Double.

    内の個々 のビットを操作することも、Single値。You can also manipulate the individual bits in a Single value. BitConverter.GetBytes(Single)メソッドは、バイト配列内のビット パターンを返します。The BitConverter.GetBytes(Single) method returns its bit pattern in a byte array. そのバイト配列を渡すことによって、BitConverter.ToInt32メソッドも保持できます、 Single 32 ビット整数値ビット パターンです。By passing that byte array to the BitConverter.ToInt32 method, you can also preserve the Single value's bit pattern in a 32-bit integer.

  • 丸め処理を行うします。Rounding. 丸め処理としてよく使用する手法と精度の浮動小数点表現の問題が原因で値の間の相違点の影響を軽減できます。Rounding is often used as a technique for reducing the impact of differences between values caused by problems of floating-point representation and precision. 丸めることができます、Single値を呼び出すことによって、Math.Roundメソッド。You can round a Single value by calling the Math.Round method. ただし、注意、Single値に変換されます、Doubleメソッドが呼び出されると、前に、変換は、精度の損失を伴うことができます。However, note that the Single value is converted to a Double before the method is called, and the conversion can involve a loss of precision.

  • 書式設定します。Formatting. 変換することができます、Singleを呼び出すことによって文字列形式の値、ToStringメソッドまたはを使用して、複合書式指定機能します。You can convert a Single value to its string representation by calling the ToString method or by using the composite formatting feature. 書式指定文字列で浮動小数点値の文字列形式を制御する方法については、次を参照してください。、 Standard Numeric Format Stringsカスタム数値書式指定文字列トピック。For information about how format strings control the string representation of floating-point values, see the Standard Numeric Format Strings and Custom Numeric Format Strings topics.

  • 文字列の解析します。Parsing strings. 浮動小数点値の文字列形式に変換することができます、Single値を呼び出すことによって、ParseまたはTryParseメソッド。You can convert the string representation of a floating-point value to a Single value by calling the Parse or TryParse method. 解析操作に失敗した場合、Parseメソッドは、例外をスローする、TryParseメソッドを返します。falseします。If the parse operation fails, the Parse method throws an exception, whereas the TryParse method returns false.

  • 変換の入力します。Type conversion. Single構造体の明示的なインターフェイス実装を提供する、IConvertibleインターフェイスで、2 つの標準的な .NET Framework データ型間の変換をサポートしています。The Single structure provides an explicit interface implementation for the IConvertible interface, which supports conversion between any two standard .NET Framework data types. 言語コンパイラは、すべての他の標準的な数値型の変換以外の値の暗黙的な変換をサポートするもDoubleSingle値。Language compilers also support the implicit conversion of values for all other standard numeric types except for the conversion of Double to Single values. 任意の標準的な数値型以外の値の変換、DoubleSingle拡大変換は、キャスト演算子または変換メソッドの使用が必要としません。Conversion of a value of any standard numeric type other than a Double to a Single is a widening conversion and does not require the use of a casting operator or conversion method.

    ただし、32 ビットおよび 64 ビットの整数値の変換には、精度の損失が含まれます。However, conversion of 32-bit and 64-bit integer values can involve a loss of precision. 次の表に、32 ビット、64 ビットの有効桁数の違いとDouble型。The following table lists the differences in precision for 32-bit, 64-bit, and Double types:

    Type (10 進数字) の最大有効桁数Maximum precision (in decimal digits) (10 進数字) の内部の有効桁数Internal precision (in decimal digits)
    Double 1615 1717
    Int32 および UInt32Int32 and UInt32 1010 1010
    Int64 および UInt64Int64 and UInt64 1919 1919
    Single 77 99

    有効桁数の問題に最も頻繁に影響Single値に変換されるDouble値。The problem of precision most frequently affects Single values that are converted to Double values. 次の例では、同一の除算演算によって生成された 2 つの値が等しくないに変換される単精度浮動小数点値には、値のいずれかであるため、Doubleします。In the following example, two values produced by identical division operations are unequal, because one of the values is a single-precision floating point value that is converted to a Double.

    using System;
    
    public class Example
    {
       public static void Main()
       {
          Double value1 = 1/3.0;
          Single sValue2 = 1/3.0f;
          Double value2 = (Double) sValue2;
          Console.WriteLine("{0:R} = {1:R}: {2}", value1, value2, 
                                              value1.Equals(value2));
       }
    }
    // The example displays the following output:
    //        0.33333333333333331 = 0.3333333432674408: False
    
    Module Example
       Public Sub Main()
          Dim value1 As Double = 1/3
          Dim sValue2 As Single = 1/3
          Dim value2 As Double = CDbl(sValue2)
          Console.WriteLine("{0} = {1}: {2}", value1, value2, value1.Equals(value2))
       End Sub
    End Module
    ' The example displays the following output:
    '       0.33333333333333331 = 0.3333333432674408: False
    

フィールド

Epsilon Epsilon Epsilon Epsilon

ゼロより大きい最小の Single 値を表します。Represents the smallest positive Single value that is greater than zero. このフィールドは定数です。This field is constant.

MaxValue MaxValue MaxValue MaxValue

Single の最大有効値を表します。Represents the largest possible value of Single. このフィールドは定数です。This field is constant.

MinValue MinValue MinValue MinValue

Single の最小有効値を表します。Represents the smallest possible value of Single. このフィールドは定数です。This field is constant.

NaN NaN NaN NaN

非数 (NaN) を表します。Represents not a number (NaN). このフィールドは定数です。This field is constant.

NegativeInfinity NegativeInfinity NegativeInfinity NegativeInfinity

負の無限大を表します。Represents negative infinity. このフィールドは定数です。This field is constant.

PositiveInfinity PositiveInfinity PositiveInfinity PositiveInfinity

正の無限大を表します。Represents positive infinity. このフィールドは定数です。This field is constant.

方法

CompareTo(Object) CompareTo(Object) CompareTo(Object) CompareTo(Object)

このインスタンスと指定したオブジェクトを比較し、このインスタンスの値が指定したオブジェクトの値よりも小さいか、同じか、または大きいかを示す整数を返します。Compares this instance to a specified object and returns an integer that indicates whether the value of this instance is less than, equal to, or greater than the value of the specified object.

CompareTo(Single) CompareTo(Single) CompareTo(Single) CompareTo(Single)

このインスタンスと指定した単精度浮動小数点数を比較し、このインスタンスの値が指定した単精度浮動小数点数の値よりも小さいか、同じか、それとも大きいかを示す整数を返します。Compares this instance to a specified single-precision floating-point number and returns an integer that indicates whether the value of this instance is less than, equal to, or greater than the value of the specified single-precision floating-point number.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

このインスタンスが指定されたオブジェクトに等しいかどうかを示す値を返します。Returns a value indicating whether this instance is equal to a specified object.

Equals(Single) Equals(Single) Equals(Single) Equals(Single)

このインスタンスと指定した Single オブジェクトが同じ値を表しているかどうかを示す値を返します。Returns a value indicating whether this instance and a specified Single object represent the same value.

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

このインスタンスのハッシュ コードを返します。Returns the hash code for this instance.

GetTypeCode() GetTypeCode() GetTypeCode() GetTypeCode()

TypeCode 値型の Single を返します。Returns the TypeCode for value type Single.

IsFinite(Single) IsFinite(Single) IsFinite(Single) IsFinite(Single)
IsInfinity(Single) IsInfinity(Single) IsInfinity(Single) IsInfinity(Single)

指定した数値が負または正の無限大と評価されるかどうかを示す値を返します。Returns a value indicating whether the specified number evaluates to negative or positive infinity.

IsNaN(Single) IsNaN(Single) IsNaN(Single) IsNaN(Single)

指定した値が非数値 (NaN) かどうかを示す値を返します。Returns a value that indicates whether the specified value is not a number (NaN).

IsNegative(Single) IsNegative(Single) IsNegative(Single) IsNegative(Single)
IsNegativeInfinity(Single) IsNegativeInfinity(Single) IsNegativeInfinity(Single) IsNegativeInfinity(Single)

指定した数値が負の無限大と評価されるかどうかを示す値を返します。Returns a value indicating whether the specified number evaluates to negative infinity.

IsNormal(Single) IsNormal(Single) IsNormal(Single) IsNormal(Single)
IsPositiveInfinity(Single) IsPositiveInfinity(Single) IsPositiveInfinity(Single) IsPositiveInfinity(Single)

指定した数値が正の無限大と評価されるかどうかを示す値を返します。Returns a value indicating whether the specified number evaluates to positive infinity.

IsSubnormal(Single) IsSubnormal(Single) IsSubnormal(Single) IsSubnormal(Single)
Parse(String, NumberStyles, IFormatProvider) Parse(String, NumberStyles, IFormatProvider) Parse(String, NumberStyles, IFormatProvider) Parse(String, NumberStyles, IFormatProvider)

指定したスタイルおよびカルチャに固有の書式による数値の文字列形式を、それと等価な単精度浮動小数点数に変換します。Converts the string representation of a number in a specified style and culture-specific format to its single-precision floating-point number equivalent.

Parse(String, IFormatProvider) Parse(String, IFormatProvider) Parse(String, IFormatProvider) Parse(String, IFormatProvider)

指定したカルチャに固有の書式による数値の文字列形式を、それと等価な単精度浮動小数点数に変換します。Converts the string representation of a number in a specified culture-specific format to its single-precision floating-point number equivalent.

Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider) Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider) Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider) Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider)
Parse(String) Parse(String) Parse(String) Parse(String)

数値の文字列形式を、それと等しい単精度浮動小数点数に変換します。Converts the string representation of a number to its single-precision floating-point number equivalent.

Parse(String, NumberStyles) Parse(String, NumberStyles) Parse(String, NumberStyles) Parse(String, NumberStyles)

指定したスタイルでの数値の文字列形式を、それと等価な単精度浮動小数点数に変換します。Converts the string representation of a number in a specified style to its single-precision floating-point number equivalent.

ToString(String, IFormatProvider) ToString(String, IFormatProvider) ToString(String, IFormatProvider) ToString(String, IFormatProvider)

このインスタンスの数値を、指定した書式およびカルチャ固有の書式情報を使用して、それと等価な文字列形式に変換します。Converts the numeric value of this instance to its equivalent string representation using the specified format and culture-specific format information.

ToString(String) ToString(String) ToString(String) ToString(String)

指定した書式を使用して、このインスタンスの数値を、それと等価な文字列形式に変換します。Converts the numeric value of this instance to its equivalent string representation, using the specified format.

ToString(IFormatProvider) ToString(IFormatProvider) ToString(IFormatProvider) ToString(IFormatProvider)

このインスタンスの数値を、指定したカルチャ固有の書式情報を使用して、それと等価な文字列形式に変換します。Converts the numeric value of this instance to its equivalent string representation using the specified culture-specific format information.

ToString() ToString() ToString() ToString()

このインスタンスの数値を、それと等価な文字列形式に変換します。Converts the numeric value of this instance to its equivalent string representation.

TryFormat(Span<Char>, Int32, ReadOnlySpan<Char>, IFormatProvider) TryFormat(Span<Char>, Int32, ReadOnlySpan<Char>, IFormatProvider) TryFormat(Span<Char>, Int32, ReadOnlySpan<Char>, IFormatProvider) TryFormat(Span<Char>, Int32, ReadOnlySpan<Char>, IFormatProvider)
TryParse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider, Single) TryParse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider, Single) TryParse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider, Single) TryParse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider, Single)
TryParse(String, Single) TryParse(String, Single) TryParse(String, Single) TryParse(String, Single)

数値の文字列形式を、それと等しい単精度浮動小数点数に変換します。Converts the string representation of a number to its single-precision floating-point number equivalent. 戻り値は変換が成功したか失敗したかを示します。A return value indicates whether the conversion succeeded or failed.

TryParse(ReadOnlySpan<Char>, Single) TryParse(ReadOnlySpan<Char>, Single) TryParse(ReadOnlySpan<Char>, Single) TryParse(ReadOnlySpan<Char>, Single)
TryParse(String, NumberStyles, IFormatProvider, Single) TryParse(String, NumberStyles, IFormatProvider, Single) TryParse(String, NumberStyles, IFormatProvider, Single) TryParse(String, NumberStyles, IFormatProvider, Single)

指定したスタイルおよびカルチャに固有の書式による数値の文字列形式を、それと等価な単精度浮動小数点数に変換します。Converts the string representation of a number in a specified style and culture-specific format to its single-precision floating-point number equivalent. 戻り値は変換が成功したか失敗したかを示します。A return value indicates whether the conversion succeeded or failed.

演算子

Equality(Single, Single) Equality(Single, Single) Equality(Single, Single) Equality(Single, Single)

指定した 2 つの Single 値が等しいかどうかを示す値を返します。Returns a value that indicates whether two specified Single values are equal.

GreaterThan(Single, Single) GreaterThan(Single, Single) GreaterThan(Single, Single) GreaterThan(Single, Single)

指定した Single 値が、指定したもう 1 つの Single 値より大きいかどうかを示す値を返します。Returns a value that indicates whether a specified Single value is greater than another specified Single value.

GreaterThanOrEqual(Single, Single) GreaterThanOrEqual(Single, Single) GreaterThanOrEqual(Single, Single) GreaterThanOrEqual(Single, Single)

指定した Single 値が、指定したもう 1 つの Single 値以上かどうかを示す値を返します。Returns a value that indicates whether a specified Single value is greater than or equal to another specified Single value.

Inequality(Single, Single) Inequality(Single, Single) Inequality(Single, Single) Inequality(Single, Single)

指定した 2 つの Single 値が等しくないかどうかを示す値を返します。Returns a value that indicates whether two specified Single values are not equal.

LessThan(Single, Single) LessThan(Single, Single) LessThan(Single, Single) LessThan(Single, Single)

指定した Single 値が、指定したもう 1 つの Single 値より小さいかどうかを示す値を返します。Returns a value that indicates whether a specified Single value is less than another specified Single value.

LessThanOrEqual(Single, Single) LessThanOrEqual(Single, Single) LessThanOrEqual(Single, Single) LessThanOrEqual(Single, Single)

指定した Single 値が、指定したもう 1 つの Single 値以下かどうかを示す値を返します。Returns a value that indicates whether a specified Single value is less than or equal to another specified Single value.

明示的なインターフェイスの実装

IComparable.CompareTo(Object) IComparable.CompareTo(Object) IComparable.CompareTo(Object) IComparable.CompareTo(Object)
IConvertible.GetTypeCode() IConvertible.GetTypeCode() IConvertible.GetTypeCode() IConvertible.GetTypeCode()
IConvertible.ToBoolean(IFormatProvider) IConvertible.ToBoolean(IFormatProvider) IConvertible.ToBoolean(IFormatProvider) IConvertible.ToBoolean(IFormatProvider)

このメンバーの詳細については、「ToBoolean(IFormatProvider)」をご覧ください。For a description of this member, see ToBoolean(IFormatProvider).

IConvertible.ToByte(IFormatProvider) IConvertible.ToByte(IFormatProvider) IConvertible.ToByte(IFormatProvider) IConvertible.ToByte(IFormatProvider)

このメンバーの詳細については、「ToByte(IFormatProvider)」をご覧ください。For a description of this member, see ToByte(IFormatProvider).

IConvertible.ToChar(IFormatProvider) IConvertible.ToChar(IFormatProvider) IConvertible.ToChar(IFormatProvider) IConvertible.ToChar(IFormatProvider)

この変換はサポートされていません。This conversion is not supported. このメソッドを使用しようとすると、InvalidCastException がスローされます。Attempting to use this method throws an InvalidCastException.

IConvertible.ToDateTime(IFormatProvider) IConvertible.ToDateTime(IFormatProvider) IConvertible.ToDateTime(IFormatProvider) IConvertible.ToDateTime(IFormatProvider)

この変換はサポートされていません。This conversion is not supported. このメソッドを使用しようとすると、InvalidCastException がスローされます。Attempting to use this method throws an InvalidCastException.

IConvertible.ToDecimal(IFormatProvider) IConvertible.ToDecimal(IFormatProvider) IConvertible.ToDecimal(IFormatProvider) IConvertible.ToDecimal(IFormatProvider)

このメンバーの詳細については、「ToDecimal(IFormatProvider)」をご覧ください。For a description of this member, see ToDecimal(IFormatProvider).

IConvertible.ToDouble(IFormatProvider) IConvertible.ToDouble(IFormatProvider) IConvertible.ToDouble(IFormatProvider) IConvertible.ToDouble(IFormatProvider)

このメンバーの詳細については、「ToDouble(IFormatProvider)」をご覧ください。For a description of this member, see ToDouble(IFormatProvider).

IConvertible.ToInt16(IFormatProvider) IConvertible.ToInt16(IFormatProvider) IConvertible.ToInt16(IFormatProvider) IConvertible.ToInt16(IFormatProvider)

このメンバーの詳細については、「ToInt16(IFormatProvider)」をご覧ください。For a description of this member, see ToInt16(IFormatProvider).

IConvertible.ToInt32(IFormatProvider) IConvertible.ToInt32(IFormatProvider) IConvertible.ToInt32(IFormatProvider) IConvertible.ToInt32(IFormatProvider)

このメンバーの詳細については、「ToInt32(IFormatProvider)」をご覧ください。For a description of this member, see ToInt32(IFormatProvider).

IConvertible.ToInt64(IFormatProvider) IConvertible.ToInt64(IFormatProvider) IConvertible.ToInt64(IFormatProvider) IConvertible.ToInt64(IFormatProvider)

このメンバーの詳細については、「ToInt64(IFormatProvider)」をご覧ください。For a description of this member, see ToInt64(IFormatProvider).

IConvertible.ToSByte(IFormatProvider) IConvertible.ToSByte(IFormatProvider) IConvertible.ToSByte(IFormatProvider) IConvertible.ToSByte(IFormatProvider)

このメンバーの詳細については、「ToSByte(IFormatProvider)」をご覧ください。For a description of this member, see ToSByte(IFormatProvider).

IConvertible.ToSingle(IFormatProvider) IConvertible.ToSingle(IFormatProvider) IConvertible.ToSingle(IFormatProvider) IConvertible.ToSingle(IFormatProvider)

このメンバーの詳細については、「ToSingle(IFormatProvider)」をご覧ください。For a description of this member, see ToSingle(IFormatProvider).

IConvertible.ToType(Type, IFormatProvider) IConvertible.ToType(Type, IFormatProvider) IConvertible.ToType(Type, IFormatProvider) IConvertible.ToType(Type, IFormatProvider)

このメンバーの詳細については、「ToType(Type, IFormatProvider)」をご覧ください。For a description of this member, see ToType(Type, IFormatProvider).

IConvertible.ToUInt16(IFormatProvider) IConvertible.ToUInt16(IFormatProvider) IConvertible.ToUInt16(IFormatProvider) IConvertible.ToUInt16(IFormatProvider)

このメンバーの詳細については、「ToUInt16(IFormatProvider)」をご覧ください。For a description of this member, see ToUInt16(IFormatProvider).

IConvertible.ToUInt32(IFormatProvider) IConvertible.ToUInt32(IFormatProvider) IConvertible.ToUInt32(IFormatProvider) IConvertible.ToUInt32(IFormatProvider)

このメンバーの詳細については、「ToUInt32(IFormatProvider)」をご覧ください。For a description of this member, see ToUInt32(IFormatProvider).

IConvertible.ToUInt64(IFormatProvider) IConvertible.ToUInt64(IFormatProvider) IConvertible.ToUInt64(IFormatProvider) IConvertible.ToUInt64(IFormatProvider)

このメンバーの詳細については、「ToUInt64(IFormatProvider)」をご覧ください。For a description of this member, see ToUInt64(IFormatProvider).

適用対象

スレッド セーフ

この種類のすべてのメンバーは、スレッド セーフです。All members of this type are thread safe. インスタンスの状態を変更する表示されるメンバーは、実際に新しい値で初期化された新しいインスタンスを返します。Members that appear to modify instance state actually return a new instance initialized with the new value. として、他の種類と読み取りと書き込みをこの型のインスタンスを含む共有変数を保護する必要があるスレッド セーフを確保するロックによって。As with any other type, reading and writing to a shared variable that contains an instance of this type must be protected by a lock to guarantee thread safety.

こちらもご覧ください