Single 構造体
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
単精度浮動小数点数を表します。
public value class float : IComparable, IComparable<float>, IConvertible, IEquatable<float>, IFormattable
public value class float : IComparable, IComparable<float>, IConvertible, IEquatable<float>, ISpanFormattable
public value class float : IComparable<float>, IConvertible, IEquatable<float>, IParsable<float>, ISpanParsable<float>, System::Numerics::IAdditionOperators<float, float, float>, System::Numerics::IAdditiveIdentity<float, float>, System::Numerics::IBinaryFloatingPointIeee754<float>, System::Numerics::IBinaryNumber<float>, System::Numerics::IBitwiseOperators<float, float, float>, System::Numerics::IComparisonOperators<float, float, bool>, System::Numerics::IDecrementOperators<float>, System::Numerics::IDivisionOperators<float, float, float>, System::Numerics::IEqualityOperators<float, float, bool>, System::Numerics::IExponentialFunctions<float>, System::Numerics::IFloatingPoint<float>, System::Numerics::IFloatingPointConstants<float>, System::Numerics::IFloatingPointIeee754<float>, System::Numerics::IHyperbolicFunctions<float>, System::Numerics::IIncrementOperators<float>, System::Numerics::ILogarithmicFunctions<float>, System::Numerics::IMinMaxValue<float>, System::Numerics::IModulusOperators<float, float, float>, System::Numerics::IMultiplicativeIdentity<float, float>, System::Numerics::IMultiplyOperators<float, float, float>, System::Numerics::INumber<float>, System::Numerics::INumberBase<float>, System::Numerics::IPowerFunctions<float>, System::Numerics::IRootFunctions<float>, System::Numerics::ISignedNumber<float>, System::Numerics::ISubtractionOperators<float, float, float>, System::Numerics::ITrigonometricFunctions<float>, System::Numerics::IUnaryNegationOperators<float, float>, System::Numerics::IUnaryPlusOperators<float, float>
public value class float : IComparable<float>, IConvertible, IEquatable<float>, IParsable<float>, ISpanParsable<float>, IUtf8SpanFormattable, IUtf8SpanParsable<float>, System::Numerics::IAdditionOperators<float, float, float>, System::Numerics::IAdditiveIdentity<float, float>, System::Numerics::IBinaryFloatingPointIeee754<float>, System::Numerics::IBinaryNumber<float>, System::Numerics::IBitwiseOperators<float, float, float>, System::Numerics::IComparisonOperators<float, float, bool>, System::Numerics::IDecrementOperators<float>, System::Numerics::IDivisionOperators<float, float, float>, System::Numerics::IEqualityOperators<float, float, bool>, System::Numerics::IExponentialFunctions<float>, System::Numerics::IFloatingPoint<float>, System::Numerics::IFloatingPointConstants<float>, System::Numerics::IFloatingPointIeee754<float>, System::Numerics::IHyperbolicFunctions<float>, System::Numerics::IIncrementOperators<float>, System::Numerics::ILogarithmicFunctions<float>, System::Numerics::IMinMaxValue<float>, System::Numerics::IModulusOperators<float, float, float>, System::Numerics::IMultiplicativeIdentity<float, float>, System::Numerics::IMultiplyOperators<float, float, float>, System::Numerics::INumber<float>, System::Numerics::INumberBase<float>, System::Numerics::IPowerFunctions<float>, System::Numerics::IRootFunctions<float>, System::Numerics::ISignedNumber<float>, System::Numerics::ISubtractionOperators<float, float, float>, System::Numerics::ITrigonometricFunctions<float>, System::Numerics::IUnaryNegationOperators<float, float>, System::Numerics::IUnaryPlusOperators<float, float>
public value class float : IComparable, IConvertible, IFormattable
public value class float : IComparable, IComparable<float>, IEquatable<float>, IFormattable
public struct Single : IComparable, IComparable<float>, IConvertible, IEquatable<float>, IFormattable
public readonly struct Single : IComparable, IComparable<float>, IConvertible, IEquatable<float>, IFormattable
public readonly struct Single : IComparable, IComparable<float>, IConvertible, IEquatable<float>, ISpanFormattable
public readonly struct Single : IComparable<float>, IConvertible, IEquatable<float>, IParsable<float>, ISpanParsable<float>, System.Numerics.IAdditionOperators<float,float,float>, System.Numerics.IAdditiveIdentity<float,float>, System.Numerics.IBinaryFloatingPointIeee754<float>, System.Numerics.IBinaryNumber<float>, System.Numerics.IBitwiseOperators<float,float,float>, System.Numerics.IComparisonOperators<float,float,bool>, System.Numerics.IDecrementOperators<float>, System.Numerics.IDivisionOperators<float,float,float>, System.Numerics.IEqualityOperators<float,float,bool>, System.Numerics.IExponentialFunctions<float>, System.Numerics.IFloatingPoint<float>, System.Numerics.IFloatingPointConstants<float>, System.Numerics.IFloatingPointIeee754<float>, System.Numerics.IHyperbolicFunctions<float>, System.Numerics.IIncrementOperators<float>, System.Numerics.ILogarithmicFunctions<float>, System.Numerics.IMinMaxValue<float>, System.Numerics.IModulusOperators<float,float,float>, System.Numerics.IMultiplicativeIdentity<float,float>, System.Numerics.IMultiplyOperators<float,float,float>, System.Numerics.INumber<float>, System.Numerics.INumberBase<float>, System.Numerics.IPowerFunctions<float>, System.Numerics.IRootFunctions<float>, System.Numerics.ISignedNumber<float>, System.Numerics.ISubtractionOperators<float,float,float>, System.Numerics.ITrigonometricFunctions<float>, System.Numerics.IUnaryNegationOperators<float,float>, System.Numerics.IUnaryPlusOperators<float,float>
public readonly struct Single : IComparable<float>, IConvertible, IEquatable<float>, IParsable<float>, ISpanParsable<float>, IUtf8SpanFormattable, IUtf8SpanParsable<float>, System.Numerics.IAdditionOperators<float,float,float>, System.Numerics.IAdditiveIdentity<float,float>, System.Numerics.IBinaryFloatingPointIeee754<float>, System.Numerics.IBinaryNumber<float>, System.Numerics.IBitwiseOperators<float,float,float>, System.Numerics.IComparisonOperators<float,float,bool>, System.Numerics.IDecrementOperators<float>, System.Numerics.IDivisionOperators<float,float,float>, System.Numerics.IEqualityOperators<float,float,bool>, System.Numerics.IExponentialFunctions<float>, System.Numerics.IFloatingPoint<float>, System.Numerics.IFloatingPointConstants<float>, System.Numerics.IFloatingPointIeee754<float>, System.Numerics.IHyperbolicFunctions<float>, System.Numerics.IIncrementOperators<float>, System.Numerics.ILogarithmicFunctions<float>, System.Numerics.IMinMaxValue<float>, System.Numerics.IModulusOperators<float,float,float>, System.Numerics.IMultiplicativeIdentity<float,float>, System.Numerics.IMultiplyOperators<float,float,float>, System.Numerics.INumber<float>, System.Numerics.INumberBase<float>, System.Numerics.IPowerFunctions<float>, System.Numerics.IRootFunctions<float>, System.Numerics.ISignedNumber<float>, System.Numerics.ISubtractionOperators<float,float,float>, System.Numerics.ITrigonometricFunctions<float>, System.Numerics.IUnaryNegationOperators<float,float>, System.Numerics.IUnaryPlusOperators<float,float>
[System.Serializable]
public struct Single : IComparable, IConvertible, IFormattable
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public struct Single : IComparable, IComparable<float>, IConvertible, IEquatable<float>, IFormattable
public struct Single : IComparable, IComparable<float>, IEquatable<float>, IFormattable
type single = struct
interface IConvertible
interface IFormattable
type single = struct
interface IConvertible
interface ISpanFormattable
interface IFormattable
type single = struct
interface IConvertible
interface IFormattable
interface IParsable<single>
interface ISpanFormattable
interface ISpanParsable<single>
interface IAdditionOperators<single, single, single>
interface IAdditiveIdentity<single, single>
interface IBinaryFloatingPointIeee754<single>
interface IBinaryNumber<single>
interface IBitwiseOperators<single, single, single>
interface IComparisonOperators<single, single, bool>
interface IEqualityOperators<single, single, bool>
interface IDecrementOperators<single>
interface IDivisionOperators<single, single, single>
interface IIncrementOperators<single>
interface IModulusOperators<single, single, single>
interface IMultiplicativeIdentity<single, single>
interface IMultiplyOperators<single, single, single>
interface INumber<single>
interface INumberBase<single>
interface ISubtractionOperators<single, single, single>
interface IUnaryNegationOperators<single, single>
interface IUnaryPlusOperators<single, single>
interface IExponentialFunctions<single>
interface IFloatingPointConstants<single>
interface IFloatingPoint<single>
interface ISignedNumber<single>
interface IFloatingPointIeee754<single>
interface IHyperbolicFunctions<single>
interface ILogarithmicFunctions<single>
interface IPowerFunctions<single>
interface IRootFunctions<single>
interface ITrigonometricFunctions<single>
interface IMinMaxValue<single>
type single = struct
interface IConvertible
interface IFormattable
interface IParsable<single>
interface ISpanFormattable
interface ISpanParsable<single>
interface IAdditionOperators<single, single, single>
interface IAdditiveIdentity<single, single>
interface IBinaryFloatingPointIeee754<single>
interface IBinaryNumber<single>
interface IBitwiseOperators<single, single, single>
interface IComparisonOperators<single, single, bool>
interface IEqualityOperators<single, single, bool>
interface IDecrementOperators<single>
interface IDivisionOperators<single, single, single>
interface IIncrementOperators<single>
interface IModulusOperators<single, single, single>
interface IMultiplicativeIdentity<single, single>
interface IMultiplyOperators<single, single, single>
interface INumber<single>
interface INumberBase<single>
interface ISubtractionOperators<single, single, single>
interface IUnaryNegationOperators<single, single>
interface IUnaryPlusOperators<single, single>
interface IUtf8SpanParsable<single>
interface IExponentialFunctions<single>
interface IFloatingPointConstants<single>
interface IFloatingPoint<single>
interface ISignedNumber<single>
interface IFloatingPointIeee754<single>
interface IHyperbolicFunctions<single>
interface ILogarithmicFunctions<single>
interface IPowerFunctions<single>
interface IRootFunctions<single>
interface ITrigonometricFunctions<single>
interface IMinMaxValue<single>
interface IUtf8SpanFormattable
[<System.Serializable>]
type single = struct
interface IFormattable
interface IConvertible
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type single = struct
interface IFormattable
interface IConvertible
type single = struct
interface IFormattable
Public Structure Single
Implements IComparable, IComparable(Of Single), IConvertible, IEquatable(Of Single), IFormattable
Public Structure Single
Implements IComparable, IComparable(Of Single), IConvertible, IEquatable(Of Single), ISpanFormattable
Public Structure Single
Implements IAdditionOperators(Of Single, Single, Single), IAdditiveIdentity(Of Single, Single), IBinaryFloatingPointIeee754(Of Single), IBinaryNumber(Of Single), IBitwiseOperators(Of Single, Single, Single), IComparable(Of Single), IComparisonOperators(Of Single, Single, Boolean), IConvertible, IDecrementOperators(Of Single), IDivisionOperators(Of Single, Single, Single), IEqualityOperators(Of Single, Single, Boolean), IEquatable(Of Single), IExponentialFunctions(Of Single), IFloatingPoint(Of Single), IFloatingPointConstants(Of Single), IFloatingPointIeee754(Of Single), IHyperbolicFunctions(Of Single), IIncrementOperators(Of Single), ILogarithmicFunctions(Of Single), IMinMaxValue(Of Single), IModulusOperators(Of Single, Single, Single), IMultiplicativeIdentity(Of Single, Single), IMultiplyOperators(Of Single, Single, Single), INumber(Of Single), INumberBase(Of Single), IParsable(Of Single), IPowerFunctions(Of Single), IRootFunctions(Of Single), ISignedNumber(Of Single), ISpanParsable(Of Single), ISubtractionOperators(Of Single, Single, Single), ITrigonometricFunctions(Of Single), IUnaryNegationOperators(Of Single, Single), IUnaryPlusOperators(Of Single, Single)
Public Structure Single
Implements IAdditionOperators(Of Single, Single, Single), IAdditiveIdentity(Of Single, Single), IBinaryFloatingPointIeee754(Of Single), IBinaryNumber(Of Single), IBitwiseOperators(Of Single, Single, Single), IComparable(Of Single), IComparisonOperators(Of Single, Single, Boolean), IConvertible, IDecrementOperators(Of Single), IDivisionOperators(Of Single, Single, Single), IEqualityOperators(Of Single, Single, Boolean), IEquatable(Of Single), IExponentialFunctions(Of Single), IFloatingPoint(Of Single), IFloatingPointConstants(Of Single), IFloatingPointIeee754(Of Single), IHyperbolicFunctions(Of Single), IIncrementOperators(Of Single), ILogarithmicFunctions(Of Single), IMinMaxValue(Of Single), IModulusOperators(Of Single, Single, Single), IMultiplicativeIdentity(Of Single, Single), IMultiplyOperators(Of Single, Single, Single), INumber(Of Single), INumberBase(Of Single), IParsable(Of Single), IPowerFunctions(Of Single), IRootFunctions(Of Single), ISignedNumber(Of Single), ISpanParsable(Of Single), ISubtractionOperators(Of Single, Single, Single), ITrigonometricFunctions(Of Single), IUnaryNegationOperators(Of Single, Single), IUnaryPlusOperators(Of Single, Single), IUtf8SpanFormattable, IUtf8SpanParsable(Of Single)
Public Structure Single
Implements IComparable, IConvertible, IFormattable
Public Structure Single
Implements IComparable, IComparable(Of Single), IEquatable(Of Single), IFormattable
- 継承
- 属性
- 実装
-
IComparable IComparable<Single> IConvertible IEquatable<Single> IFormattable ISpanFormattable IComparable<TSelf> IEquatable<TSelf> IParsable<Single> IParsable<TSelf> ISpanParsable<Single> ISpanParsable<TSelf> IAdditionOperators<Single,Single,Single> IAdditionOperators<TSelf,TSelf,TSelf> IAdditiveIdentity<Single,Single> IAdditiveIdentity<TSelf,TSelf> IBinaryFloatingPointIeee754<Single> IBinaryNumber<Single> IBinaryNumber<TSelf> IBitwiseOperators<Single,Single,Single> IBitwiseOperators<TSelf,TSelf,TSelf> IComparisonOperators<Single,Single,Boolean> IComparisonOperators<TSelf,TSelf,Boolean> IDecrementOperators<Single> IDecrementOperators<TSelf> IDivisionOperators<Single,Single,Single> IDivisionOperators<TSelf,TSelf,TSelf> IEqualityOperators<Single,Single,Boolean> IEqualityOperators<TSelf,TOther,TResult> IEqualityOperators<TSelf,TSelf,Boolean> IExponentialFunctions<Single> IExponentialFunctions<TSelf> IFloatingPoint<Single> IFloatingPoint<TSelf> IFloatingPointConstants<Single> IFloatingPointConstants<TSelf> IFloatingPointIeee754<Single> IFloatingPointIeee754<TSelf> IHyperbolicFunctions<Single> IHyperbolicFunctions<TSelf> IIncrementOperators<Single> IIncrementOperators<TSelf> ILogarithmicFunctions<Single> ILogarithmicFunctions<TSelf> IMinMaxValue<Single> IModulusOperators<Single,Single,Single> IModulusOperators<TSelf,TSelf,TSelf> IMultiplicativeIdentity<Single,Single> IMultiplicativeIdentity<TSelf,TSelf> IMultiplyOperators<Single,Single,Single> IMultiplyOperators<TSelf,TSelf,TSelf> INumber<Single> INumber<TSelf> INumberBase<Single> INumberBase<TSelf> IPowerFunctions<Single> IPowerFunctions<TSelf> IRootFunctions<Single> IRootFunctions<TSelf> ISignedNumber<Single> ISignedNumber<TSelf> ISubtractionOperators<Single,Single,Single> ISubtractionOperators<TSelf,TSelf,TSelf> ITrigonometricFunctions<Single> ITrigonometricFunctions<TSelf> IUnaryNegationOperators<Single,Single> IUnaryNegationOperators<TSelf,TSelf> IUnaryPlusOperators<Single,Single> IUnaryPlusOperators<TSelf,TSelf> IUtf8SpanFormattable IUtf8SpanParsable<Single> IUtf8SpanParsable<TSelf>
注釈
値型は Single 、負の 3.402823e38 から正の 3.402823e38 までの範囲の値を持つ単精度 32 ビット数値を表し、正または負のゼロ 、、 PositiveInfinity、 NegativeInfinityおよび数値 (NaN) を表します。 これは、非常に大きい値 (惑星や銀河間の距離など) または非常に小さい値 (1kg の物質の分子質量など) を表し、多くの場合、不正確な値 (地球から別の太陽系への距離など) を表すことを目的としています。 この型は Single 、2 項浮動小数点演算の IEC 60559:1989 (IEEE 754) 標準に準拠しています。
この記事は、次のセクションで構成されています。
System.Single には、この型のインスタンスを比較するメソッド、インスタンスの値をその文字列表現に変換するメソッド、および数値の文字列表現をこの型のインスタンスに変換するメソッドが用意されています。 書式指定コードで値型の文字列表現を制御する方法については、「 書式指定の型」、「 標準の数値書式指定文字列」、「カスタム数値書式指定文字列」を参照 してください。
浮動小数点表現と精度
データ型は Single 、次の表に示すように、単精度浮動小数点値を 32 ビットのバイナリ形式で格納します。
パーツ | Bits |
---|---|
仮数または仮数 | 0-22 |
指数 | 23-30 |
符号 (0 = 正、1 = 負) | 31 |
小数部が一部の小数部の値 (1/3 やなど) を正確に表すことができないのと Math.PI同様に、二項分数では一部の小数部を表すことができません。 たとえば、.2 で正確に表される 2/10 は、.0011111001001100 で二項分数として表され、パターン "1100" は無限大に繰り返されます。 この場合、浮動小数点値は、それが表す数値の不正確な表現を提供します。 元の浮動小数点値に対して追加の算術演算を実行すると、多くの場合、精度が不足します。 たとえば、.3 を 10 で乗算し、.3 を .3 に 9 回加算した結果を比較すると、乗算よりも 8 つの演算が含まれるため、加算の結果の精度が低くなります。 この不一致は、"R" 標準の数値書式指定文字列を使用して 2 つのSingle値を表示する場合にのみ明らかであり、必要に応じて、型でSingleサポートされている有効桁数の 9 桁すべてを表示します。
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
let value = 0.2f
let result1 = value * 10f
let mutable result2 = 0f
for _ = 1 to 10 do
result2 <- result2 + value
printfn $".2 * 10: {result1:R}"
printfn $".2 Added 10 times: {result2:R}"
// 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
一部の数値は小数部のバイナリ値として正確に表すことができないため、浮動小数点数は実数のみを近似できます。
すべての浮動小数点数には有効桁数が制限されています。これにより、浮動小数点値が実数に近似する精度も決定されます。 Single値の有効桁数は最大 7 桁ですが、内部的には最大 9 桁が維持されます。 つまり、浮動小数点演算によっては、浮動小数点値を変更する精度が不足している可能性があります。 次の例では、大きな単精度浮動小数点値を定義し、 の積 Single.Epsilon と 1 つの四分の一を加算します。 ただし、製品が小さすぎて元の浮動小数点値を変更できません。 その最下位桁は 1000 分の 1 ですが、製品の最上位桁は 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
open System
let value = 123.456f
let additional = Single.Epsilon * 1e15f
printfn $"{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
浮動小数点数の有効桁数が制限されている場合、次のような結果が生じる場合があります。
特定の有効桁数で等しく見える 2 つの浮動小数点数が、最小有効数字が異なっているために等しくない場合があります。 次の例では、一連の数値が加算され、その合計が予想される合計と比較されます。 2 つの値は同じであるように見えますが、 メソッドの
Equals
呼び出しは、それらが同じではないことを示します。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).
let values = [| 10.01f; 2.88f; 2.88f; 2.88f; 9f |] let result = 27.65f let mutable total = 0f for value in values do total <- total + value if total.Equals result then printfn "The sum of the values equals the total." else printfn "The sum of the values ({total}) does not equal the 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}
に{0:R}
変更し{1}
{1:R}
、2 つのSingle値のすべての有効桁数を表示する場合、加算操作中に精度が失われるため、2 つの値が等しくないことは明らかです。 この場合、メソッドを呼び出して、比較を Math.Round(Double, Int32) 実行する前に目的の Single 精度に値を丸めることで問題を解決できます。浮動小数点数を使用する算術演算または比較演算では、10 進数を使用すると同じ結果が得られない可能性があります。これは、2 進数の浮動小数点数が 10 進数と等しくない可能性があるためです。 前の例では、.3 に 10 を乗算し、.3 を .3 に 9 回加算した結果を表示します。
小数部の値を持つ数値演算の精度が重要な場合は、 型ではなく 型をSingle使用Decimalします。 または UInt64 型の範囲を超える整数値を持つ数値演算のInt64精度が重要な場合は、 型を使用しますBigInteger。
浮動小数点数が関係している場合、値がラウンドトリップしない可能性があります。 演算が元の浮動小数点数を別の形式に変換し、逆演算によって変換されたフォームが浮動小数点数に変換され、最終的な浮動小数点数が元の浮動小数点数と等しい場合、値はラウンドトリップと言われます。 変換で 1 つ以上の最下位桁が失われたり変更されたりするため、ラウンド トリップが失敗する可能性があります。 次の例では、3 つの Single 値が文字列に変換され、ファイルに保存されます。 出力が示すように、値は同一のように見えますが、復元された値は元の値と等しくありません。
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
open System open System.IO let values = [| 3.2f / 1.11f; 1f / 3f; MathF.PI |] do use sw = new StreamWriter(@".\Singles.dat") for i = 0 to values.Length - 1 do sw.Write(string values[i]) if i <> values.Length - 1 then sw.Write "|" let restoredValues = use sr = new StreamReader(@".\Singles.dat") sr.ReadToEnd().Split '|' |> Array.map Single.Parse for i = 0 to values.Length - 1 do printfn $"""{values[i]} {if values[i].Equals restoredValues[i] then "=" else "<>"} {restoredValues[i]}""" // 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 完全な精度を保持することで、値を丸めることができます。
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
open System open System.IO let values = [| 3.2f / 1.11f; 1f / 3f; MathF.PI |] do use sw = new StreamWriter(@".\Singles.dat") for i = 0 to values.Length - 1 do sw.Write $"""{values[i]:G9}{if i < values.Length - 1 then "|" else ""}""" let restoredValues = use sr = new StreamReader(@".\Singles.dat") sr.ReadToEnd().Split '|' |> Array.map Single.Parse for i = 0 to values.Length - 1 do printfn $"""{values[i]} {if values[i].Equals restoredValues[i] then "=" else "<>"} {restoredValues[i]}""" // 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一見等価Doubleに変換される値は、多くの場合、精度のDouble違いのために値と等しくありません。 次の例では、同じ除算操作の結果が値とSingle値にDouble割り当てられます。 値が Single に Doubleキャストされた後、2 つの値を比較すると、それらが等しくないことが示されます。
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
open System let value1 = 1. / 3. let sValue2 = 1f /3f let value2 = double sValue2 printfn $"{value1:R} = {value2:R}: {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 両方の値の精度が同じになるようにします。
等価性のテスト
等しいと見なすには、2 つの Single 値が同じ値を表す必要があります。 ただし、値間の精度の違い、または一方または両方の値による精度の損失により、同一であると予想される浮動小数点値は、多くの場合、最下位桁数の違いにより等しくないことが判明します。 その結果、 メソッドを呼び出して Equals 2 つの値が等しいかどうかを判断するか、 メソッドを呼び出して CompareTo 2 つの Single 値間の関係を判断すると、多くの場合、予期しない結果が得られます。 これは、次の例では明らかに等しい Single 2 つの値が等しくないことがわかります。最初の値の有効桁数は 7 桁で、2 番目の値は 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
let value1 = 0.3333333f
let value2 = 1f / 3f
printfn $"{value1:R} = {value2:R}: {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
さまざまなコード パスに従い、さまざまな方法で操作される計算値は、多くの場合、等しくないことが証明されます。 次の例では、1 つの Single 値が 2 乗され、元の値を復元するために平方根が計算されます。 1 秒は Single 3.51 で乗算され、結果の平方根が 3.51 で割られる前に 2 乗して元の値を復元します。 2 つの値は同一のように見えますが、 メソッドの Equals(Single) 呼び出しは、それらが等しくないことを示します。 "G9" 標準書式指定文字列を使用して、各 Single 値のすべての有効桁数を表示する結果文字列を返すと、2 番目の値が最初の値より .0000000000001小さいことが示されます。
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
let value1 =
10.201438f ** 2f
|> sqrt
let value2 =
((value1 * 3.51f) ** 2f |> sqrt) / 3.51f
printfn $"{value1} = {value2}: {value1.Equals value2}\n"
printfn $"{value1:G9} = {value2:G9}"
// 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
精度の低下が比較の結果に影響する可能性がある場合は、 メソッドまたは CompareTo メソッドを呼び出す代わりに、次の手法をEquals使用できます。
両方の値の Math.Round 精度が同じであることを確認するには、 メソッドを呼び出します。 次の例では、2 つの小数部の値が同等になるように、この方法を使用するように前の例を変更します。
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
open System let value1 = 0.3333333f let value2 = 1f / 3f let precision = 7 let value1r = Math.Round(float value1, precision) |> float32 let value2r = Math.Round(float value2, precision) |> float32 printfn $"{value1:R} = {value2:R}: {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
精度の問題は、引き続き中間値の丸めにも適用されます。 詳細については、Math.Round(Double, Int32, MidpointRounding) メソッドを参照してください。
等値ではなく、おおよその等値をテストします。 この手法では、2 つの値が異なる可能性があるが等しい絶対量を定義するか、小さい値が大きい値から分岐できる相対量を定義する必要があります。
警告
Single.Epsilon は、等しいかどうかをテストするときに、2 つの Single 値間の距離の絶対メジャーとして使用される場合があります。 ただし、 Single.Epsilon 値が 0 の に加算または減算 Single できる、可能な最小の値を測定します。 ほとんどの正と負 Single の値では、 の Single.Epsilon 値が小さすぎて検出できません。 したがって、0 の値を除き、等しいかどうかをテストで使用することはお勧めしません。
次の例では、後者の方法を使用して、
IsApproximatelyEqual
2 つの値の相対差をテストするメソッドを定義します。 また、 メソッドと メソッドの呼び出しIsApproximatelyEqual
の結果も比較します Equals(Single) 。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
open System let isApproximatelyEqual value1 value2 epsilon = // If they are equal anyway, just return True. if value1.Equals value2 then true // Handle NaN, Infinity. elif Single.IsInfinity value1 || Single.IsNaN value1 then value1.Equals value2 elif Single.IsInfinity value2 || Single.IsNaN value2 then value1.Equals value2 else // Handle zero to avoid division by zero let divisor = max value1 value2 let divisor = if divisor.Equals 0 then min value1 value2 else divisor abs (value1 - value2) / divisor <= epsilon let one1 = 0.1f * 10f let mutable one2 = 0f for _ = 1 to 10 do one2 <- one2 + 0.1f printfn $"{one1:R} = {one2:R}: {one1.Equals one2}" printfn $"{one1:R} is approximately equal to {one2:R}: {isApproximatelyEqual one1 one2 0.000001f}" // 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
浮動小数点値と例外
整数型を使用する操作とは異なり、浮動小数点値を持つ操作は例外をスローしません。これは、ゼロによる除算やオーバーフローなどの無効な操作の場合に例外をスローします。 このような状況では、浮動小数点演算の結果は 0、正の無限大、負の無限大、または数値 (NaN) ではありません。
浮動小数点演算の結果が変換先の形式に対して小さすぎる場合、結果は 0 になります。 これは、次の例に示すように、2 つの非常に小さい浮動小数点数を乗算した場合に発生する可能性があります。
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
let value1 = 1.163287e-36f let value2 = 9.164234e-25f let result = value1 * value2 printfn $"{value1} * {value2} = {result}" printfn $"{result} = 0: {result.Equals(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になります。 オーバーフローする操作の結果は でPositiveInfinity、オーバーフロー Single.MaxValueSingle.MinValueする操作の結果は NegativeInfinityです。次の例に示すように。
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
open System let value1 = 3.065e35f let value2 = 6.9375e32f let result = value1 * value2 printfn $"PositiveInfinity: {Single.IsPositiveInfinity result}" printfn $"NegativeInfinity: {Single.IsNegativeInfinity result}\n" let value3 = -value1 let result2 = value3 * value2 printfn $"PositiveInfinity: {Single.IsPositiveInfinity result}" printfn $"NegativeInfinity: {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 また、正の配当でゼロで除算した結果、 NegativeInfinity 負の配当を持つゼロで除算した結果もあります。
浮動小数点演算が無効な場合、操作の結果は になります NaN。 たとえば、 NaN 次の操作の結果が得られます。
0 で除算し、除数を 0 にします。 ゼロ除算の他のケースでは、 または NegativeInfinityがPositiveInfinity発生します。
入力が無効な浮動小数点演算。 たとえば、負の値の平方根を検索しようとすると、 が返されます NaN。
値 Single.NaNが である引数を持つすべての操作。
型変換と Single 構造体
構造体では Single 、明示的または暗黙的な変換演算子は定義されません。代わりに、コンパイラによって変換が実装されます。
次の表に、他のプリミティブ数値型の値から値への Single 変換の可能性を示します。また、変換が拡大または縮小されているかどうか、および結果 Single の精度が元の値よりも小さい可能性があるかどうかを示します。
変換 ( | 拡大/縮小 | 精度が失われる可能性があります |
---|---|---|
Byte | Widening | いいえ |
Decimal | Widening C# にはキャスト演算子が必要であることに注意してください。 |
はい。 Decimal は 29 桁の有効桁数をサポートしています。 Single は 9 をサポートします。 |
Double | 縮小;範囲外の値は または Double.PositiveInfinityにDouble.NegativeInfinity変換されます。 | はい。 Double は 17 桁の有効桁数をサポートしています。 Single は 9 をサポートします。 |
Int16 | Widening | いいえ |
Int32 | Widening | はい。 Int32 は 10 桁の有効桁数をサポートします。 Single は 9 をサポートします。 |
Int64 | Widening | はい。 Int64 は 19 桁の有効桁数をサポートしています。 Single は 9 をサポートします。 |
SByte | Widening | いいえ |
UInt16 | Widening | いいえ |
UInt32 | Widening | はい。 UInt32 は 10 桁の有効桁数をサポートします。 Single は 9 をサポートします。 |
UInt64 | Widening | はい。 Int64 は 20 桁の有効桁数をサポートしています。 Single は 9 をサポートします。 |
次の例では、他のプリミティブ数値型の最小値または最大値を Single 値に変換します。
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)
open System
let values: obj list =
[ 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 ]
for value in values do
let sngValue =
match value with
| :? byte as v -> float32 v
| :? decimal as v -> float32 v
| :? double as v -> float32 v
| :? int16 as v -> float32 v
| :? int as v -> float32 v
| :? int64 as v -> float32 v
| :? int8 as v -> float32 v
| :? uint16 as v -> float32 v
| :? uint as v -> float32 v
| :? uint64 as v -> float32 v
| _ -> raise (NotImplementedException "Unknown Type")
printfn $"{value} ({value.GetType().Name}) --> {sngValue:R} ({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)
さらに、Double値 Double.NaN、Double.PositiveInfinity、および Double.NegativeInfinity は、それぞれ 、Single.PositiveInfinity、および Single.NegativeInfinityに変換Single.NaNされます。
一部の数値型の値を値に Single 変換すると、精度が失われる可能性があることに注意してください。 例が示すように、、および の値を値SingleにInt32DoubleInt64DecimalUInt32変換すると、精度が失われる可能性があります。UInt64
値から へのSingleDouble変換は拡大変換です。 型に値の正確な表現がない場合、 Double 変換によって精度が Single 失われる可能性があります。
以外の Single プリミティブ数値データ型 Double の値への値の変換は縮小変換であり、キャスト演算子 (C#) または変換メソッド (Visual Basic の場合) が必要です。 ターゲットのデータ型とプロパティによって定義されるターゲット データ型の範囲外の値は、次のMinValue
MaxValue
表に示すように動作します。
変換後の型 | 結果 |
---|---|
任意の整数型 | OverflowExceptionチェックされたコンテキストで変換が発生した場合の例外。 オフのコンテキスト (C# の既定値) で変換が発生した場合、変換操作は成功しますが、値はオーバーフローします。 |
Decimal | 例外 OverflowException 。 |
さらに、、Single.NaNSingle.PositiveInfinity、 Single.NegativeInfinity は、チェックされたコンテキスト内の整数への変換に 対して をOverflowExceptionスローしますが、チェックされていないコンテキストで整数に変換すると、これらの値がオーバーフローします。 への Decimal変換では、常に がスローされます OverflowException。 へのDouble変換の場合、それぞれ 、Double.PositiveInfinity、および Double.NegativeInfinityにDouble.NaN変換されます。
精度が失われると、値が別の数値型に変換 Single される可能性があることに注意してください。 整数 Single 以外の値を変換する場合、例の出力に示すように、値が丸められた場合 (Visual Basic の場合と同様)、または切り捨てられた (C# と F# のように) 小数部分が失われます Single 。 値への Decimal 変換の場合、値が Single ターゲット データ型に正確に表現されていない可能性があります。
次の例では、複数の値を他のいくつかの Single 数値型に変換します。 変換は、Visual Basic (既定)、C# (チェックされたキーワード (keyword)のため)、F# (ステートメントのためopen Checked
) のチェックされたコンテキストで発生します。 この例の出力は、チェックが解除されたコンテキストの両方で変換の結果を示しています。 Visual Basic では、コンパイラ スイッチを使用してコンパイルし、C# で ステートメントを /removeintchecks+
コメントアウトし、F# で ステートメントをコメントアウト checked
することで、オフのコンテキストで変換を open Checked
実行できます。
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)
open System
open Checked
let values =
[ Single.MinValue; -67890.1234f; -12345.6789f
12345.6789f; 67890.1234f; Single.MaxValue
Single.NaN; Single.PositiveInfinity
Single.NegativeInfinity ]
for value in values do
try
let lValue = int64 value
printfn $"{value} ({value.GetType().Name}) --> {lValue} (0x{lValue:X16}) ({lValue.GetType().Name})"
with :? OverflowException ->
printfn $"Unable to convert {value} to Int64."
try
let ulValue = uint64 value
printfn $"{value} ({value.GetType().Name}) --> {ulValue} (0x{ulValue:X16}) ({ulValue.GetType().Name})"
with :? OverflowException ->
printfn $"Unable to convert {value} to UInt64."
try
let dValue = decimal value
printfn $"{value} ({value.GetType().Name}) --> {dValue} ({dValue.GetType().Name})"
with :? OverflowException ->
printfn $"Unable to convert {value} to Decimal."
let dblValue = double value
printfn $"{value} ({value.GetType().Name}) --> {dblValue} ({dblValue.GetType().Name})\n"
// 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の型変換」および「型変換テーブル」を参照してください。
浮動小数点機能
構造体と関連する型には Single 、次のカテゴリの操作を実行するメソッドが用意されています。
値の比較。 メソッドを Equals 呼び出して、2 つの Single 値が等しいかどうかを判断するか、 メソッドを CompareTo 呼び出して 2 つの値間の関係を判断できます。
構造体では Single 、比較演算子の完全なセットもサポートされています。 たとえば、等しいか等しくないかをテストしたり、1 つの値が別の値以上であるかどうかを判断したりできます。 オペランドの 1 つが の Double場合、比較を Single 実行する前に 値が に Double 変換されます。 オペランドの 1 つが整数型の場合、比較を実行する前に に Single 変換されます。 これらは拡大変換ですが、精度が低下する可能性があります。
警告
精度の違いにより、等しいと予想される 2 つの Single 値が等しくない可能性があり、これは比較の結果に影響します。 2 つのSingle値の比較の詳細については、「等価性のテスト」セクションを参照してください。
また、、IsInfinity、IsPositiveInfinity、および IsNegativeInfinity メソッドをIsNaN呼び出して、これらの特別な値をテストすることもできます。
数学演算。 加算、減算、乗算、除算などの一般的な算術演算は、メソッドではなく、言語コンパイラと共通中間言語 (CIL) 命令によって Single 実装されます。 算術演算のもう一方のオペランドが の場合、 はDoubleSingle演算を実行する前に にDouble変換され、演算の結果も値になりますDouble。 もう一方のオペランドが整数型の場合、演算を実行する前に に Single 変換され、演算の結果も値になります Single 。
クラスで (
Shared
Visual Basic では) メソッドを呼び出static
すことで、他の算術演算をSystem.Math実行できます。 これには、算術 (、Math.Sign、、 Math.SqrtなどMath.Abs)、geometry (や などMath.CosMath.Sin)、微積分 (などMath.Log) に一般的に使用される追加のメソッドが含まれます。 いずれの場合も、 Single 値は に Double変換されます。値内の個々のビットを Single 操作することもできます。 メソッドは BitConverter.GetBytes(Single) 、バイト配列内のビット パターンを返します。 そのバイト配列を メソッドに BitConverter.ToInt32 渡すことで、値の Single ビット パターンを 32 ビット整数で保持することもできます。
丸め。 丸めは、浮動小数点表現と精度の問題に起因する値間の差の影響を軽減するための手法としてよく使用されます。 メソッドを Single 呼び出すことで値を Math.Round 丸めることができます。 ただし、メソッドが Single 呼び出される前に 値が に Double 変換され、変換に精度が失われる可能性があることに注意してください。
書式設定。 メソッドをSingle呼び出すか、複合書式指定機能をToString使用して、値を文字列形式に変換できます。 書式指定文字列が浮動小数点値の文字列表現を制御する方法については、「 標準の数値書式指定文字列 」および 「カスタム数値書式指定文字列 」トピックを参照してください。
文字列の解析。 浮動小数点値の文字列形式を 値に変換するには、 Single メソッドまたは TryParse メソッドをParse呼び出します。 解析操作が失敗した場合、メソッドは Parse 例外をスローしますが、 メソッドは を TryParse 返します
false
。型変換。 構造体はSingle、任意の 2 つの標準.NET Frameworkデータ型間の変換をサポートする インターフェイスの明示的なインターフェイス実装IConvertibleを提供します。 言語コンパイラでは、値への変換を除く、他のすべての標準数値型の値のDoubleSingle暗黙的な変換もサポートされています。 以外 Double の標準の数値型の値を への Single 変換は拡大変換であり、キャスト演算子または変換メソッドを使用する必要はありません。
ただし、32 ビットと 64 ビットの整数値を変換すると、精度が低下する可能性があります。 次の表に、32 ビット、64 ビット、型 Double の有効桁数の違いを示します。
種類 最大有効桁数 (10 進数) 内部有効桁数 (10 進数) Double 15 17 Int32 および UInt32 10 10 Int64 および UInt64 19 19 Single 7 9 精度の問題は、値に変換される値に最も頻繁にDouble影響Singleします。 次の例では、同一の除算操作によって生成される 2 つの値は等しくありません。値の 1 つは に変換される 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
let value1 = 1. / 3. let sValue2 = 1f / 3f let value2 = double sValue2 printfn $"{value1:R} = {value2:R}: {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
フィールド
E |
定数 e で指定された自然対数底を表します。 |
Epsilon |
ゼロより大きい最小の Single 値を表します。 このフィールドは定数です。 |
MaxValue |
Single の最大有効値を表します。 このフィールドは定数です。 |
MinValue |
Single の最小有効値を表します。 このフィールドは定数です。 |
NaN |
非数 ( |
NegativeInfinity |
負の無限大を表します。 このフィールドは定数です。 |
NegativeZero |
負の 0 (-0) の数値を表します。 |
Pi |
定数 (π) を指定して、円の直径に対する円周の割合を表します。 |
PositiveInfinity |
正の無限大を表します。 このフィールドは定数です。 |
Tau |
定数 τ によって指定される、1 回のターンのラジアン数を表します。 |
メソッド
Abs(Single) |
値の絶対を計算します。 |
Acos(Single) |
値のアークコサインを計算します。 |
Acosh(Single) |
値の双曲線アークコサインを計算します。 |
AcosPi(Single) |
値のアークコサインを計算し、結果を で |
Asin(Single) |
値のアークサインを計算します。 |
Asinh(Single) |
値の双曲線アークサインを計算します。 |
AsinPi(Single) |
値のアークサインを計算し、結果を で |
Atan(Single) |
値のアークタンジェントを計算します。 |
Atan2(Single, Single) |
2 つの値の商のアークタンジェントを計算します。 |
Atan2Pi(Single, Single) |
2 つの値の商のアークタンジェントを計算し、結果を で |
Atanh(Single) |
値の双曲線アークタンジェントを計算します。 |
AtanPi(Single) |
値のアークタンジェントを計算し、結果を pi で除算します。 |
BitDecrement(Single) |
指定された値より小さい値を比較する最小の値に値をデクリメントします。 |
BitIncrement(Single) |
指定した値より大きい値を比較する最小値に値をインクリメントします。 |
Cbrt(Single) |
値のキューブ ルートを計算します。 |
Ceiling(Single) |
値の上限を計算します。 |
Clamp(Single, Single, Single) |
値を包括的な最小値と最大値にクランプします。 |
CompareTo(Object) |
このインスタンスと指定したオブジェクトを比較し、このインスタンスの値が指定したオブジェクトの値よりも小さいか、同じか、または大きいかを示す整数を返します。 |
CompareTo(Single) |
このインスタンスと指定した単精度浮動小数点数を比較し、このインスタンスの値が指定した単精度浮動小数点数の値よりも小さいか、同じか、それとも大きいかを示す整数を返します。 |
CopySign(Single, Single) |
値の符号を別の値の符号にコピーします。 |
Cos(Single) |
値のコサインを計算します。 |
Cosh(Single) |
値の双曲線余弦を計算します。 |
CosPi(Single) |
によって |
CreateChecked<TOther>(TOther) |
現在の型のインスタンスを値から作成し、現在の型の表現可能な範囲外の値に対してオーバーフロー例外をスローします。 |
CreateSaturating<TOther>(TOther) |
現在の型のインスタンスを値から作成し、現在の型の表現可能な範囲外の値を飽和させます。 |
CreateTruncating<TOther>(TOther) |
現在の型のインスタンスを値から作成し、現在の型の表現可能な範囲外の値を切り捨てます。 |
DegreesToRadians(Single) |
指定した値を度からラジアンに変換します。 |
Equals(Object) |
このインスタンスが指定されたオブジェクトに等しいかどうかを示す値を返します。 |
Equals(Single) |
このインスタンスと指定した Single オブジェクトが同じ値を表しているかどうかを示す値を返します。 |
Exp(Single) |
特定の |
Exp10(Single) |
特定の |
Exp10M1(Single) |
特定の |
Exp2(Single) |
特定の |
Exp2M1(Single) |
特定の |
ExpM1(Single) |
特定の |
Floor(Single) |
値のフロアを計算します。 |
FusedMultiplyAdd(Single, Single, Single) |
3 つの値の乗算加算を計算します。 |
GetHashCode() |
このインスタンスのハッシュ コードを返します。 |
GetTypeCode() | |
Hypot(Single, Single) |
直角三角形の短辺の長さを表す 2 つの値を指定して、偽表を計算します。 |
Ieee754Remainder(Single, Single) |
IEEE 754 で指定された 2 つの値の残りの部分を計算します。 |
ILogB(Single) |
値の整数対数を計算します。 |
IsEvenInteger(Single) |
値が偶数の整数を表すかどうかを判断します。 |
IsFinite(Single) |
指定した値が有限 (ゼロ、非正規、または正規) かどうかを判断します。 |
IsInfinity(Single) |
指定した数値が負または正の無限大と評価されるかどうかを示す値を返します。 |
IsInteger(Single) |
値が整数値を表すかどうかを判断します。 |
IsNaN(Single) |
指定した値が非数値 (NaN) かどうかを示す値を返します。 |
IsNegative(Single) |
指定した値が負かどうかを判断します。 |
IsNegativeInfinity(Single) |
指定した数値が負の無限大と評価されるかどうかを示す値を返します。 |
IsNormal(Single) |
指定した値が正規かどうかを判断します。 |
IsOddInteger(Single) |
値が奇数の整数を表すかどうかを判断します。 |
IsPositive(Single) |
値が正かどうかを判断します。 |
IsPositiveInfinity(Single) |
指定した数値が正の無限大と評価されるかどうかを示す値を返します。 |
IsPow2(Single) |
値が 2 の累乗であるかどうかを判断します。 |
IsRealNumber(Single) |
値が実数を表すかどうかを判断します。 |
IsSubnormal(Single) |
指定した値が非正規かどうかを判断します。 |
Lerp(Single, Single, Single) |
指定された重み付けに基づいて、2 つの値間の線形補間を実行します。 |
Log(Single) |
自然 ( |
Log(Single, Single) |
指定した底の値の対数を計算します。 |
Log10(Single) |
値の底 10 の対数を計算します。 |
Log10P1(Single) |
値に 1 を加えた底 10 の対数を計算します。 |
Log2(Single) |
値の log2 を計算します。 |
Log2P1(Single) |
値に 1 を加えた底 2 の対数を計算します。 |
LogP1(Single) |
値に 1 を加えた自然対数 ( |
Max(Single, Single) |
2 つの値を、より大きいコンピューティングと比較します。 |
MaxMagnitude(Single, Single) |
2 つの値を、より大きいコンピューティングと比較します。 |
MaxMagnitudeNumber(Single, Single) |
2 つの値を比較して、大きさが大きい計算を行い、入力が の場合は |
MaxNumber(Single, Single) |
2 つの値を比較して、より大きいを計算し、入力が の場合は |
Min(Single, Single) |
2 つの値を比較して計算します。この値は小さくなります。 |
MinMagnitude(Single, Single) |
2 つの値を比較して計算します。この値は小さくなります。 |
MinMagnitudeNumber(Single, Single) |
2 つの値を比較して、大きさが小さい計算を行い、入力が の場合は |
MinNumber(Single, Single) |
2 つの値を比較して、より小さい計算を行い、入力が の場合は |
Parse(ReadOnlySpan<Byte>, IFormatProvider) |
UTF-8 文字のスパンを値に解析します。 |
Parse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider) |
UTF-8 文字のスパンを値に解析します。 |
Parse(ReadOnlySpan<Char>, IFormatProvider) |
文字のスパンを値に解析します。 |
Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider) |
指定したスタイルおよびカルチャに固有の書式による数値の文字列表現を含む文字スパンを、それと等価な単精度浮動小数点数に変換します。 |
Parse(String) |
数値の文字列形式を、それと等価な単精度浮動小数点数に変換します。 |
Parse(String, IFormatProvider) |
指定したカルチャに固有の書式による数値の文字列形式を、それと等価な単精度浮動小数点数に変換します。 |
Parse(String, NumberStyles) |
指定したスタイルでの数値の文字列形式を、それと等価な単精度浮動小数点数に変換します。 |
Parse(String, NumberStyles, IFormatProvider) |
指定したスタイルおよびカルチャに固有の書式による数値の文字列形式を、それと等価な単精度浮動小数点数に変換します。 |
Pow(Single, Single) |
特定の累乗に引き上げられた値を計算します。 |
RadiansToDegrees(Single) |
指定した値をラジアンから度に変換します。 |
ReciprocalEstimate(Single) |
値の逆数の推定値を計算します。 |
ReciprocalSqrtEstimate(Single) |
値の逆平方根の推定値を計算します。 |
RootN(Single, Int32) |
値の n 番目のルートを計算します。 |
Round(Single) |
既定の丸めモード (ToEven) を使用して、値を最も近い整数に丸めます。 |
Round(Single, Int32) |
既定の丸めモード (ToEven) を使用して、指定した桁数の小数部に値を丸めます。 |
Round(Single, Int32, MidpointRounding) |
既定の丸めモード (ToEven) を使用して、指定した桁数の小数部に値を丸めます。 |
Round(Single, MidpointRounding) |
指定した丸めモードを使用して、値を最も近い整数に丸めます。 |
ScaleB(Single, Int32) |
指定した累乗に引き上げられた値とその基数の積を計算します。 |
Sign(Single) |
値の符号を計算します。 |
Sin(Single) |
値のサインを計算します。 |
SinCos(Single) |
値のサインとコサインを計算します。 |
SinCosPi(Single) |
値のサインとコサインを計算します。 |
Sinh(Single) |
値の双曲線サインを計算します。 |
SinPi(Single) |
が乗算 |
Sqrt(Single) |
値の平方根を計算します。 |
Tan(Single) |
値のタンジェントを計算します。 |
Tanh(Single) |
値の双曲線正接を計算します。 |
TanPi(Single) |
によって多重化された値のタンジェントを |
ToString() |
このインスタンスの数値を、それと等価な文字列形式に変換します。 |
ToString(IFormatProvider) |
このインスタンスの数値を、指定したカルチャ固有の書式情報を使用して、それと等価な文字列形式に変換します。 |
ToString(String) |
指定した書式を使用して、このインスタンスの数値を、それと等価な文字列形式に変換します。 |
ToString(String, IFormatProvider) |
このインスタンスの数値を、指定した書式およびカルチャ固有の書式情報を使用して、それと等価な文字列形式に変換します。 |
Truncate(Single) |
値を切り捨てます。 |
TryFormat(Span<Byte>, Int32, ReadOnlySpan<Char>, IFormatProvider) |
現在のインスタンスの値を UTF-8 として指定されたバイトスパンに書式設定しようとします。 |
TryFormat(Span<Char>, Int32, ReadOnlySpan<Char>, IFormatProvider) |
現在の浮動小数点数インスタンスの値の、指定した文字スパンへの書式設定を試みます。 |
TryParse(ReadOnlySpan<Byte>, IFormatProvider, Single) |
UTF-8 文字のスパンを値に解析しようとします。 |
TryParse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider, Single) |
UTF-8 文字のスパンを値に解析しようとします。 |
TryParse(ReadOnlySpan<Byte>, Single) |
数値の文字列表現を含む UTF-8 文字範囲を、等価の単精度浮動小数点数に変換しようとします。 |
TryParse(ReadOnlySpan<Char>, IFormatProvider, Single) |
文字のスパンを値に解析しようとします。 |
TryParse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider, Single) |
指定したスタイルおよびカルチャに固有の書式による数値のスパン表現を、それと等価な単精度浮動小数点数に変換します。 戻り値は変換が成功したか失敗したかを示します。 |
TryParse(ReadOnlySpan<Char>, Single) |
文字スパン内の数値の文字列表現を、それと等価の単精度浮動小数点数に変換します。 戻り値は変換が成功したか失敗したかを示します。 |
TryParse(String, IFormatProvider, Single) |
文字列を値に解析しようとします。 |
TryParse(String, NumberStyles, IFormatProvider, Single) |
指定したスタイルおよびカルチャに固有の書式による数値の文字列形式を、それと等価な単精度浮動小数点数に変換します。 戻り値は変換が成功したか失敗したかを示します。 |
TryParse(String, Single) |
数値の文字列形式を、それと等価な単精度浮動小数点数に変換します。 戻り値は変換が成功したか失敗したかを示します。 |
演算子
Equality(Single, Single) |
指定した 2 つの Single 値が等しいかどうかを示す値を返します。 |
GreaterThan(Single, Single) | |
GreaterThanOrEqual(Single, Single) | |
Inequality(Single, Single) |
指定した 2 つの Single 値が等しくないかどうかを示す値を返します。 |
LessThan(Single, Single) | |
LessThanOrEqual(Single, Single) |
明示的なインターフェイスの実装
適用対象
スレッド セーフ
この型のすべてのメンバーはスレッド セーフです。 インスタンスの状態を変更するように見えるメンバーは、実際には新しい値で初期化された新しいインスタンスを返します。 他の型と同様に、この型のインスタンスを含む共有変数の読み取りと書き込みは、スレッドセーフを保証するためにロックによって保護する必要があります。