Double 構造体
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
倍精度浮動小数点数を表します。
public value class double : IComparable, IComparable<double>, IConvertible, IEquatable<double>, IFormattable
public value class double : IComparable<double>, IConvertible, IEquatable<double>, IParsable<double>, ISpanParsable<double>, IUtf8SpanParsable<double>, System::Numerics::IAdditionOperators<double, double, double>, System::Numerics::IAdditiveIdentity<double, double>, System::Numerics::IBinaryFloatingPointIeee754<double>, System::Numerics::IBinaryNumber<double>, System::Numerics::IBitwiseOperators<double, double, double>, System::Numerics::IComparisonOperators<double, double, bool>, System::Numerics::IDecrementOperators<double>, System::Numerics::IDivisionOperators<double, double, double>, System::Numerics::IEqualityOperators<double, double, bool>, System::Numerics::IExponentialFunctions<double>, System::Numerics::IFloatingPoint<double>, System::Numerics::IFloatingPointConstants<double>, System::Numerics::IFloatingPointIeee754<double>, System::Numerics::IHyperbolicFunctions<double>, System::Numerics::IIncrementOperators<double>, System::Numerics::ILogarithmicFunctions<double>, System::Numerics::IMinMaxValue<double>, System::Numerics::IModulusOperators<double, double, double>, System::Numerics::IMultiplicativeIdentity<double, double>, System::Numerics::IMultiplyOperators<double, double, double>, System::Numerics::INumber<double>, System::Numerics::INumberBase<double>, System::Numerics::IPowerFunctions<double>, System::Numerics::IRootFunctions<double>, System::Numerics::ISignedNumber<double>, System::Numerics::ISubtractionOperators<double, double, double>, System::Numerics::ITrigonometricFunctions<double>, System::Numerics::IUnaryNegationOperators<double, double>, System::Numerics::IUnaryPlusOperators<double, double>
public value class double : IComparable, IComparable<double>, IConvertible, IEquatable<double>, ISpanFormattable
public value class double : IComparable<double>, IConvertible, IEquatable<double>, IParsable<double>, ISpanParsable<double>, System::Numerics::IAdditionOperators<double, double, double>, System::Numerics::IAdditiveIdentity<double, double>, System::Numerics::IBinaryFloatingPointIeee754<double>, System::Numerics::IBinaryNumber<double>, System::Numerics::IBitwiseOperators<double, double, double>, System::Numerics::IComparisonOperators<double, double, bool>, System::Numerics::IDecrementOperators<double>, System::Numerics::IDivisionOperators<double, double, double>, System::Numerics::IEqualityOperators<double, double, bool>, System::Numerics::IExponentialFunctions<double>, System::Numerics::IFloatingPoint<double>, System::Numerics::IFloatingPointConstants<double>, System::Numerics::IFloatingPointIeee754<double>, System::Numerics::IHyperbolicFunctions<double>, System::Numerics::IIncrementOperators<double>, System::Numerics::ILogarithmicFunctions<double>, System::Numerics::IMinMaxValue<double>, System::Numerics::IModulusOperators<double, double, double>, System::Numerics::IMultiplicativeIdentity<double, double>, System::Numerics::IMultiplyOperators<double, double, double>, System::Numerics::INumber<double>, System::Numerics::INumberBase<double>, System::Numerics::IPowerFunctions<double>, System::Numerics::IRootFunctions<double>, System::Numerics::ISignedNumber<double>, System::Numerics::ISubtractionOperators<double, double, double>, System::Numerics::ITrigonometricFunctions<double>, System::Numerics::IUnaryNegationOperators<double, double>, System::Numerics::IUnaryPlusOperators<double, double>
public value class double : IComparable, IConvertible, IFormattable
public value class double : IComparable, IComparable<double>, IEquatable<double>, IFormattable
public struct Double : IComparable, IComparable<double>, IConvertible, IEquatable<double>, IFormattable
public readonly struct Double : IComparable<double>, IConvertible, IEquatable<double>, IParsable<double>, ISpanParsable<double>, IUtf8SpanParsable<double>, System.Numerics.IAdditionOperators<double,double,double>, System.Numerics.IAdditiveIdentity<double,double>, System.Numerics.IBinaryFloatingPointIeee754<double>, System.Numerics.IBinaryNumber<double>, System.Numerics.IBitwiseOperators<double,double,double>, System.Numerics.IComparisonOperators<double,double,bool>, System.Numerics.IDecrementOperators<double>, System.Numerics.IDivisionOperators<double,double,double>, System.Numerics.IEqualityOperators<double,double,bool>, System.Numerics.IExponentialFunctions<double>, System.Numerics.IFloatingPoint<double>, System.Numerics.IFloatingPointConstants<double>, System.Numerics.IFloatingPointIeee754<double>, System.Numerics.IHyperbolicFunctions<double>, System.Numerics.IIncrementOperators<double>, System.Numerics.ILogarithmicFunctions<double>, System.Numerics.IMinMaxValue<double>, System.Numerics.IModulusOperators<double,double,double>, System.Numerics.IMultiplicativeIdentity<double,double>, System.Numerics.IMultiplyOperators<double,double,double>, System.Numerics.INumber<double>, System.Numerics.INumberBase<double>, System.Numerics.IPowerFunctions<double>, System.Numerics.IRootFunctions<double>, System.Numerics.ISignedNumber<double>, System.Numerics.ISubtractionOperators<double,double,double>, System.Numerics.ITrigonometricFunctions<double>, System.Numerics.IUnaryNegationOperators<double,double>, System.Numerics.IUnaryPlusOperators<double,double>
public readonly struct Double : IComparable, IComparable<double>, IConvertible, IEquatable<double>, IFormattable
public readonly struct Double : IComparable, IComparable<double>, IConvertible, IEquatable<double>, ISpanFormattable
public readonly struct Double : IComparable<double>, IConvertible, IEquatable<double>, IParsable<double>, ISpanParsable<double>, System.Numerics.IAdditionOperators<double,double,double>, System.Numerics.IAdditiveIdentity<double,double>, System.Numerics.IBinaryFloatingPointIeee754<double>, System.Numerics.IBinaryNumber<double>, System.Numerics.IBitwiseOperators<double,double,double>, System.Numerics.IComparisonOperators<double,double,bool>, System.Numerics.IDecrementOperators<double>, System.Numerics.IDivisionOperators<double,double,double>, System.Numerics.IEqualityOperators<double,double,bool>, System.Numerics.IExponentialFunctions<double>, System.Numerics.IFloatingPoint<double>, System.Numerics.IFloatingPointConstants<double>, System.Numerics.IFloatingPointIeee754<double>, System.Numerics.IHyperbolicFunctions<double>, System.Numerics.IIncrementOperators<double>, System.Numerics.ILogarithmicFunctions<double>, System.Numerics.IMinMaxValue<double>, System.Numerics.IModulusOperators<double,double,double>, System.Numerics.IMultiplicativeIdentity<double,double>, System.Numerics.IMultiplyOperators<double,double,double>, System.Numerics.INumber<double>, System.Numerics.INumberBase<double>, System.Numerics.IPowerFunctions<double>, System.Numerics.IRootFunctions<double>, System.Numerics.ISignedNumber<double>, System.Numerics.ISubtractionOperators<double,double,double>, System.Numerics.ITrigonometricFunctions<double>, System.Numerics.IUnaryNegationOperators<double,double>, System.Numerics.IUnaryPlusOperators<double,double>
[System.Serializable]
public struct Double : IComparable, IConvertible, IFormattable
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public struct Double : IComparable, IComparable<double>, IConvertible, IEquatable<double>, IFormattable
public struct Double : IComparable, IComparable<double>, IEquatable<double>, IFormattable
type double = struct
interface IConvertible
interface IFormattable
type double = struct
interface IConvertible
interface IFormattable
interface IParsable<double>
interface ISpanFormattable
interface ISpanParsable<double>
interface IUtf8SpanFormattable
interface IUtf8SpanParsable<double>
interface IAdditionOperators<double, double, double>
interface IAdditiveIdentity<double, double>
interface IBinaryFloatingPointIeee754<double>
interface IBinaryNumber<double>
interface IBitwiseOperators<double, double, double>
interface IComparisonOperators<double, double, bool>
interface IEqualityOperators<double, double, bool>
interface IDecrementOperators<double>
interface IDivisionOperators<double, double, double>
interface IIncrementOperators<double>
interface IModulusOperators<double, double, double>
interface IMultiplicativeIdentity<double, double>
interface IMultiplyOperators<double, double, double>
interface INumber<double>
interface INumberBase<double>
interface ISubtractionOperators<double, double, double>
interface IUnaryNegationOperators<double, double>
interface IUnaryPlusOperators<double, double>
interface IExponentialFunctions<double>
interface IFloatingPointConstants<double>
interface IFloatingPoint<double>
interface ISignedNumber<double>
interface IFloatingPointIeee754<double>
interface IHyperbolicFunctions<double>
interface ILogarithmicFunctions<double>
interface IPowerFunctions<double>
interface IRootFunctions<double>
interface ITrigonometricFunctions<double>
interface IMinMaxValue<double>
type double = struct
interface IConvertible
interface ISpanFormattable
interface IFormattable
type double = struct
interface IConvertible
interface IFormattable
interface IParsable<double>
interface ISpanFormattable
interface ISpanParsable<double>
interface IAdditionOperators<double, double, double>
interface IAdditiveIdentity<double, double>
interface IBinaryFloatingPointIeee754<double>
interface IBinaryNumber<double>
interface IBitwiseOperators<double, double, double>
interface IComparisonOperators<double, double, bool>
interface IEqualityOperators<double, double, bool>
interface IDecrementOperators<double>
interface IDivisionOperators<double, double, double>
interface IIncrementOperators<double>
interface IModulusOperators<double, double, double>
interface IMultiplicativeIdentity<double, double>
interface IMultiplyOperators<double, double, double>
interface INumber<double>
interface INumberBase<double>
interface ISubtractionOperators<double, double, double>
interface IUnaryNegationOperators<double, double>
interface IUnaryPlusOperators<double, double>
interface IExponentialFunctions<double>
interface IFloatingPointConstants<double>
interface IFloatingPoint<double>
interface ISignedNumber<double>
interface IFloatingPointIeee754<double>
interface IHyperbolicFunctions<double>
interface ILogarithmicFunctions<double>
interface IPowerFunctions<double>
interface IRootFunctions<double>
interface ITrigonometricFunctions<double>
interface IMinMaxValue<double>
type double = struct
interface IConvertible
interface IFormattable
interface IParsable<double>
interface ISpanFormattable
interface ISpanParsable<double>
interface IAdditionOperators<double, double, double>
interface IAdditiveIdentity<double, double>
interface IBinaryFloatingPointIeee754<double>
interface IBinaryNumber<double>
interface IBitwiseOperators<double, double, double>
interface IComparisonOperators<double, double, bool>
interface IEqualityOperators<double, double, bool>
interface IDecrementOperators<double>
interface IDivisionOperators<double, double, double>
interface IIncrementOperators<double>
interface IModulusOperators<double, double, double>
interface IMultiplicativeIdentity<double, double>
interface IMultiplyOperators<double, double, double>
interface INumber<double>
interface INumberBase<double>
interface ISubtractionOperators<double, double, double>
interface IUnaryNegationOperators<double, double>
interface IUnaryPlusOperators<double, double>
interface IUtf8SpanFormattable
interface IUtf8SpanParsable<double>
interface IExponentialFunctions<double>
interface IFloatingPointConstants<double>
interface IFloatingPoint<double>
interface ISignedNumber<double>
interface IFloatingPointIeee754<double>
interface IHyperbolicFunctions<double>
interface ILogarithmicFunctions<double>
interface IPowerFunctions<double>
interface IRootFunctions<double>
interface ITrigonometricFunctions<double>
interface IMinMaxValue<double>
[<System.Serializable>]
type double = struct
interface IFormattable
interface IConvertible
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type double = struct
interface IFormattable
interface IConvertible
type double = struct
interface IFormattable
Public Structure Double
Implements IComparable, IComparable(Of Double), IConvertible, IEquatable(Of Double), IFormattable
Public Structure Double
Implements IAdditionOperators(Of Double, Double, Double), IAdditiveIdentity(Of Double, Double), IBinaryFloatingPointIeee754(Of Double), IBinaryNumber(Of Double), IBitwiseOperators(Of Double, Double, Double), IComparable(Of Double), IComparisonOperators(Of Double, Double, Boolean), IConvertible, IDecrementOperators(Of Double), IDivisionOperators(Of Double, Double, Double), IEqualityOperators(Of Double, Double, Boolean), IEquatable(Of Double), IExponentialFunctions(Of Double), IFloatingPoint(Of Double), IFloatingPointConstants(Of Double), IFloatingPointIeee754(Of Double), IHyperbolicFunctions(Of Double), IIncrementOperators(Of Double), ILogarithmicFunctions(Of Double), IMinMaxValue(Of Double), IModulusOperators(Of Double, Double, Double), IMultiplicativeIdentity(Of Double, Double), IMultiplyOperators(Of Double, Double, Double), INumber(Of Double), INumberBase(Of Double), IParsable(Of Double), IPowerFunctions(Of Double), IRootFunctions(Of Double), ISignedNumber(Of Double), ISpanParsable(Of Double), ISubtractionOperators(Of Double, Double, Double), ITrigonometricFunctions(Of Double), IUnaryNegationOperators(Of Double, Double), IUnaryPlusOperators(Of Double, Double), IUtf8SpanParsable(Of Double)
Public Structure Double
Implements IComparable, IComparable(Of Double), IConvertible, IEquatable(Of Double), ISpanFormattable
Public Structure Double
Implements IAdditionOperators(Of Double, Double, Double), IAdditiveIdentity(Of Double, Double), IBinaryFloatingPointIeee754(Of Double), IBinaryNumber(Of Double), IBitwiseOperators(Of Double, Double, Double), IComparable(Of Double), IComparisonOperators(Of Double, Double, Boolean), IConvertible, IDecrementOperators(Of Double), IDivisionOperators(Of Double, Double, Double), IEqualityOperators(Of Double, Double, Boolean), IEquatable(Of Double), IExponentialFunctions(Of Double), IFloatingPoint(Of Double), IFloatingPointConstants(Of Double), IFloatingPointIeee754(Of Double), IHyperbolicFunctions(Of Double), IIncrementOperators(Of Double), ILogarithmicFunctions(Of Double), IMinMaxValue(Of Double), IModulusOperators(Of Double, Double, Double), IMultiplicativeIdentity(Of Double, Double), IMultiplyOperators(Of Double, Double, Double), INumber(Of Double), INumberBase(Of Double), IParsable(Of Double), IPowerFunctions(Of Double), IRootFunctions(Of Double), ISignedNumber(Of Double), ISpanParsable(Of Double), ISubtractionOperators(Of Double, Double, Double), ITrigonometricFunctions(Of Double), IUnaryNegationOperators(Of Double, Double), IUnaryPlusOperators(Of Double, Double)
Public Structure Double
Implements IComparable, IConvertible, IFormattable
Public Structure Double
Implements IComparable, IComparable(Of Double), IEquatable(Of Double), IFormattable
- 継承
- 属性
- 実装
-
IComparable IComparable<Double> IConvertible IEquatable<Double> IFormattable IComparable<TSelf> IEquatable<TSelf> IParsable<Double> IParsable<TSelf> ISpanFormattable ISpanParsable<Double> ISpanParsable<TSelf> IUtf8SpanFormattable IUtf8SpanParsable<Double> IUtf8SpanParsable<TSelf> IAdditionOperators<Double,Double,Double> IAdditionOperators<TSelf,TSelf,TSelf> IAdditiveIdentity<Double,Double> IAdditiveIdentity<TSelf,TSelf> IBinaryFloatingPointIeee754<Double> IBinaryNumber<Double> IBinaryNumber<TSelf> IBitwiseOperators<Double,Double,Double> IBitwiseOperators<TSelf,TSelf,TSelf> IComparisonOperators<Double,Double,Boolean> IComparisonOperators<TSelf,TSelf,Boolean> IDecrementOperators<Double> IDecrementOperators<TSelf> IDivisionOperators<Double,Double,Double> IDivisionOperators<TSelf,TSelf,TSelf> IEqualityOperators<Double,Double,Boolean> IEqualityOperators<TSelf,TOther,TResult> IEqualityOperators<TSelf,TSelf,Boolean> IExponentialFunctions<Double> IExponentialFunctions<TSelf> IFloatingPoint<Double> IFloatingPoint<TSelf> IFloatingPointConstants<Double> IFloatingPointConstants<TSelf> IFloatingPointIeee754<Double> IFloatingPointIeee754<TSelf> IHyperbolicFunctions<Double> IHyperbolicFunctions<TSelf> IIncrementOperators<Double> IIncrementOperators<TSelf> ILogarithmicFunctions<Double> ILogarithmicFunctions<TSelf> IMinMaxValue<Double> IModulusOperators<Double,Double,Double> IModulusOperators<TSelf,TSelf,TSelf> IMultiplicativeIdentity<Double,Double> IMultiplicativeIdentity<TSelf,TSelf> IMultiplyOperators<Double,Double,Double> IMultiplyOperators<TSelf,TSelf,TSelf> INumber<Double> INumber<TSelf> INumberBase<Double> INumberBase<TSelf> IPowerFunctions<Double> IPowerFunctions<TSelf> IRootFunctions<Double> IRootFunctions<TSelf> ISignedNumber<Double> ISignedNumber<TSelf> ISubtractionOperators<Double,Double,Double> ISubtractionOperators<TSelf,TSelf,TSelf> ITrigonometricFunctions<Double> ITrigonometricFunctions<TSelf> IUnaryNegationOperators<Double,Double> IUnaryNegationOperators<TSelf,TSelf> IUnaryPlusOperators<Double,Double> IUnaryPlusOperators<TSelf,TSelf>
例
次のコード例に Double の使用方法を示します。
// The Temperature class stores the temperature as a Double
// and delegates most of the functionality to the Double
// implementation.
public class Temperature : IComparable, IFormattable
{
// IComparable.CompareTo implementation.
public int CompareTo(object obj) {
if (obj == null) return 1;
Temperature temp = obj as Temperature;
if (obj != null)
return m_value.CompareTo(temp.m_value);
else
throw new ArgumentException("object is not a Temperature");
}
// IFormattable.ToString implementation.
public string ToString(string format, IFormatProvider provider) {
if( format != null ) {
if( format.Equals("F") ) {
return String.Format("{0}'F", this.Value.ToString());
}
if( format.Equals("C") ) {
return String.Format("{0}'C", this.Celsius.ToString());
}
}
return m_value.ToString(format, provider);
}
// Parses the temperature from a string in the form
// [ws][sign]digits['F|'C][ws]
public static Temperature Parse(string s, NumberStyles styles, IFormatProvider provider) {
Temperature temp = new Temperature();
if( s.TrimEnd(null).EndsWith("'F") ) {
temp.Value = Double.Parse( s.Remove(s.LastIndexOf('\''), 2), styles, provider);
}
else if( s.TrimEnd(null).EndsWith("'C") ) {
temp.Celsius = Double.Parse( s.Remove(s.LastIndexOf('\''), 2), styles, provider);
}
else {
temp.Value = Double.Parse(s, styles, provider);
}
return temp;
}
// The value holder
protected double m_value;
public double Value {
get {
return m_value;
}
set {
m_value = value;
}
}
public double Celsius {
get {
return (m_value-32.0)/1.8;
}
set {
m_value = 1.8*value+32.0;
}
}
}
// The Temperature class stores the temperature as a Double
// and delegates most of the functionality to the Double
// implementation.
type Temperature() =
member val Value = 0. with get, set
member this.Celsius
with get () = (this.Value - 32.) / 1.8
and set (value) =
this.Value <- 1.8 * value + 32.
// Parses the temperature from a string in the form
// [ws][sign]digits['F|'C][ws]
static member Parse(s: string, styles: NumberStyles, provider: IFormatProvider) =
let temp = Temperature()
if s.TrimEnd(null).EndsWith "'F" then
temp.Value <- Double.Parse(s.Remove(s.LastIndexOf '\'', 2), styles, provider)
elif s.TrimEnd(null).EndsWith "'C" then
temp.Celsius <- Double.Parse(s.Remove(s.LastIndexOf '\'', 2), styles, provider)
else
temp.Value <- Double.Parse(s, styles, provider)
temp
interface IComparable with
// IComparable.CompareTo implementation.
member this.CompareTo(obj: obj) =
match obj with
| null -> 1
| :? Temperature as temp ->
this.Value.CompareTo temp.Value
| _ ->
invalidArg "obj" "object is not a Temperature"
interface IFormattable with
// IFormattable.ToString implementation.
member this.ToString(format: string, provider: IFormatProvider) =
match format with
| "F" ->
$"{this.Value}'F"
| "C" ->
$"{this.Celsius}'C"
| _ ->
this.Value.ToString(format, provider)
' Temperature class stores the value as Double
' and delegates most of the functionality
' to the Double implementation.
Public Class Temperature
Implements IComparable, IFormattable
Public Overloads Function CompareTo(ByVal obj As Object) As Integer _
Implements IComparable.CompareTo
If TypeOf obj Is Temperature Then
Dim temp As Temperature = CType(obj, Temperature)
Return m_value.CompareTo(temp.m_value)
End If
Throw New ArgumentException("object is not a Temperature")
End Function
Public Overloads Function ToString(ByVal format As String, ByVal provider As IFormatProvider) As String _
Implements IFormattable.ToString
If Not (format Is Nothing) Then
If format.Equals("F") Then
Return [String].Format("{0}'F", Me.Value.ToString())
End If
If format.Equals("C") Then
Return [String].Format("{0}'C", Me.Celsius.ToString())
End If
End If
Return m_value.ToString(format, provider)
End Function
' Parses the temperature from a string in form
' [ws][sign]digits['F|'C][ws]
Public Shared Function Parse(ByVal s As String, ByVal styles As NumberStyles, ByVal provider As IFormatProvider) As Temperature
Dim temp As New Temperature()
If s.TrimEnd().EndsWith("'F") Then
temp.Value = Double.Parse(s.Remove(s.LastIndexOf("'"c), 2), styles, provider)
Else
If s.TrimEnd().EndsWith("'C") Then
temp.Celsius = Double.Parse(s.Remove(s.LastIndexOf("'"c), 2), styles, provider)
Else
temp.Value = Double.Parse(s, styles, provider)
End If
End If
Return temp
End Function
' The value holder
Protected m_value As Double
Public Property Value() As Double
Get
Return m_value
End Get
Set(ByVal Value As Double)
m_value = Value
End Set
End Property
Public Property Celsius() As Double
Get
Return (m_value - 32) / 1.8
End Get
Set(ByVal Value As Double)
m_value = Value * 1.8 + 32
End Set
End Property
End Class
注釈
Double 値型は、負の 1.79769313486232e308 から正の 1.79769313486232e308 までの値、正または負のゼロ、PositiveInfinity、NegativeInfinity、非数 (NaN) を表す 64 ビットの倍精度数です。 これは、非常に大きい値 (惑星や銀河間の距離など) または非常に小さい値 (1 キロ単位の物質の分子質量など) を表すことを目的としており、多くの場合、(地球から別の太陽系までの距離など) 不正確です。 Double型は、二項浮動小数点演算の IEC 60559:1989 (IEEE 754) 標準に準拠しています。
浮動小数点表現と精度
Double データ型は、次の表に示すように、倍精度浮動小数点値を 64 ビットの 2 進形式で格納します。
| 要素 | ビット |
|---|---|
| 仮数 (Significand/mantissa) | 0-51 |
| Exponent | 52-62 |
| 符号 (0 = 正、1 = 負) | 63 |
小数部が一部の小数部の値 (1/3 や Math.PIなど) を正確に表すことができないのと同様に、二項分数は一部の小数部を表すことができません。 たとえば、1/10 は 10 進小数では .1 で正確に表現されますが、2 進小数では .001100110011... となり、「0011」のパターンが無限に繰り返されます。 この場合、浮動小数点値は、それが表す数値の不正確な表現を提供します。 元の浮動小数点値に対してさらに算術演算を行うと、精度損失が増す傾向があります。 たとえば、.1 を 10 で乗算し、.1 を .1 に 9 回加算した結果を比較すると、さらに 8 つの演算が関係しているため、精度の低い結果が生成されていることがわかります。 (.NET 10 より前では、2 つの Double 値を "R" 標準の数値書式指定文字列 を使用して表示した場合にのみ、この差異が明らかになります。これは、Double 型でサポートされている有効桁数の最大 17 桁をすべて表示します)。
using System;
public class Example13
{
public static void Main()
{
Double value = .1;
Double result1 = value * 10;
Double result2 = 0;
for (int ctr = 1; ctr <= 10; ctr++)
result2 += value;
Console.WriteLine($".1 * 10: {result1:R}");
Console.WriteLine($".1 Added 10 times: {result2:R}");
}
}
// The example displays the following output:
// .1 * 10: 1
// .1 Added 10 times: 0.99999999999999989
let value = 0.1
let result1 = value * 10.
let mutable result2 = 0.
for i = 1 to 10 do
result2 <- result2 + value
printfn $".1 * 10: {result1:R}"
printfn $".1 Added 10 times: {result2:R}"
// The example displays the following output:
// .1 * 10: 1
// .1 Added 10 times: 0.99999999999999989
Module Example14
Public Sub Run()
Dim value As Double = 0.1
Dim result1 As Double = value * 10
Dim result2 As Double
For ctr As Integer = 1 To 10
result2 += value
Next
Console.WriteLine(".1 * 10: {0:R}", result1)
Console.WriteLine(".1 Added 10 times: {0:R}", result2)
End Sub
End Module
' The example displays the following output:
' .1 * 10: 1
' .1 Added 10 times: 0.99999999999999989
一部の数値は小数部のバイナリ値として正確に表現できないため、浮動小数点数は実数のみを近似できます。
すべての浮動小数点数には有効桁数の制限もあり、これも浮動小数点値が実数をどれだけ正確に近似できるかを左右します。 Double 値は最大 15 桁の 10 進精度を持ちますが、内部的には最大 17 桁が保持されています。 つまり、一部の浮動小数点演算では、浮動小数点値を変更する精度が不足している可能性があります。 具体的な例を次に示します。 ここでは、非常に大きな浮動小数点値を定義し、それに Double.Epsilon と 1,000 兆 (10 の15 乗) の積を足しています。 しかし、その積は元の浮動小数点値を変更するには小さすぎます。 その最下位桁は 10 の -3 乗 (1,000 分の 1) ですが、積の最上位桁は 10 の -309 乗です。
using System;
public class Example14
{
public static void Main()
{
Double value = 123456789012.34567;
Double additional = Double.Epsilon * 1e15;
Console.WriteLine($"{value} + {additional} = {value + additional}");
}
}
// The example displays the following output:
// 123456789012.346 + 4.94065645841247E-309 = 123456789012.346
open System
let value = 123456789012.34567
let additional = Double.Epsilon * 1e15
printfn $"{value} + {additional} = {value + additional}"
// The example displays the following output:
// 123456789012.346 + 4.94065645841247E-309 = 123456789012.346
Module Example15
Public Sub Run()
Dim value As Double = 123456789012.34567
Dim additional As Double = Double.Epsilon * 1.0E+15
Console.WriteLine("{0} + {1} = {2}", value, additional,
value + additional)
End Sub
End Module
' The example displays the following output:
' 123456789012.346 + 4.94065645841247E-309 = 123456789012.346
浮動小数点数の有効桁数が制限されている場合、次のような結果が生じることがあります。
特定の有効桁数に対して同じように見えても、2つの浮動小数点数は、最下位の桁が異なるため、実際には等しくないことがあります。 次の例では、一連の数値が一緒に追加され、その合計が予想される合計と比較されます。
using System; public class Example10 { public static void Main() { Double[] values = { 10.0, 2.88, 2.88, 2.88, 9.0 }; Double result = 27.64; Double total = 0; 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 ({total}) does not equal the total ({result})."); } } // The example displays the following output: // The sum of the values (36.64) does not equal the total (36.64). // // 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).let values = [ 10.0; 2.88; 2.88; 2.88; 9.0 ] let result = 27.64 let total = List.sum values 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 (36.64) does not equal the total (36.64). // // 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).Module Example11 Public Sub Run() Dim values() As Double = {10.0, 2.88, 2.88, 2.88, 9.0} Dim result As Double = 27.64 Dim total As Double 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 (36.64) does not equal the total (36.64). ' ' 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).加算操作中に精度が失われるため、2 つの値は等しくありません。 この場合、比較を実行する前に、 Math.Round(Double, Int32) メソッドを呼び出して Double 値を目的の精度に丸めることで問題を解決できます。
浮動小数点数を使用する数学演算または比較演算では、10 進数を使用した場合、2 進浮動小数点数が 10 進数と等しくない可能性があるため、同じ結果が得られない可能性があります。 前の例では、0.1 に 10 を掛けた結果と 0.1 を複数回足した結果を表示することで、この点を示しました。
小数部の値を持つ数値演算の精度が重要な場合は、Decimal型ではなくDouble型を使用できます。 Int128 や UInt128 型の範囲から外れる整数演算で精度が重要な場合は、BigInteger 型を使用してください。
Single 値の精度は、 Double 値よりも小さくなります。 一見同等のSingleに変換されるDouble値は、多くの場合、精度の違いのためにDouble値と等しくありません。 次の例では、同じ除算演算の結果を Double 値と Single 値にそれぞれ代入します。 Single値がDoubleにキャストされた後、2 つの値を比較すると、それらが等しくないことが示されます。
using System; public class Example9 { public static void Run() { double value1 = 1 / 3.0; float sValue2 = 1 / 3.0f; double value2 = (double)sValue2; Console.WriteLine($"{value1:R} = {value2:R}: {value1.Equals(value2)}"); } } // The example displays the following output: // 0.33333333333333331 = 0.3333333432674408: Falseopen 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: FalseModule Example10 Public Sub Run() 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 メソッドを使用します。
さらに、 Double 値を持つ算術演算と代入演算の結果は、 Double 型の精度が失われるため、プラットフォームによって若干異なる場合があります。 たとえば、リテラル Double 値を割り当てた結果は、.NETの 32 ビット バージョンと 64 ビット バージョンで異なる場合があります。 次の例は、リテラル値 -4.42330604244772E-305 と、値が -4.42330604244772E-305 である変数を Double 変数に代入した場合の違いを示しています。 この場合、Parse(String) メソッドの結果は精度損失の影響を受けません。
double value = -4.42330604244772E-305;
double fromLiteral = -4.42330604244772E-305;
double fromVariable = value;
double fromParse = double.Parse("-4.42330604244772E-305");
Console.WriteLine("Double value from literal: {0,29:R}", fromLiteral);
Console.WriteLine("Double value from variable: {0,28:R}", fromVariable);
Console.WriteLine("Double value from Parse method: {0,24:R}", fromParse);
// The output is:
// Double value from literal: -4.42330604244772E-305
// Double value from variable: -4.42330604244772E-305
// Double value from Parse method: -4.42330604244772E-305
let value = -4.42330604244772E-305
let fromLiteral = -4.42330604244772E-305
let fromVariable = value
let fromParse = Double.Parse "-4.42330604244772E-305"
printfn $"Double value from literal: {fromLiteral,29:R}"
printfn $"Double value from variable: {fromVariable,28:R}"
printfn $"Double value from Parse method: {fromParse,24:R}"
// On 32-bit versions of the .NET Framework, the output is:
// Double value from literal: -4.42330604244772E-305
// Double value from variable: -4.42330604244772E-305
// Double value from Parse method: -4.42330604244772E-305
//
// On other versions of the .NET Framework, the output is:
// Double value from literal: -4.4233060424477198E-305
// Double value from variable: -4.4233060424477198E-305
// Double value from Parse method: -4.42330604244772E-305
Dim value As Double = -4.4233060424477198E-305
Dim fromLiteral As Double = -4.4233060424477198E-305
Dim fromVariable As Double = value
Dim fromParse As Double = Double.Parse("-4.42330604244772E-305")
Console.WriteLine("Double value from literal: {0,29:R}", fromLiteral)
Console.WriteLine("Double value from variable: {0,28:R}", fromVariable)
Console.WriteLine("Double value from Parse method: {0,24:R}", fromParse)
' The output is:
' Double value from literal: -4.42330604244772E-305
' Double value from variable: -4.42330604244772E-305
' Double value from Parse method: -4.42330604244772E-305
等しいかどうかをテストする
等しいと見なすには、2 つの Double 値が同じ値を表す必要があります。 しかし、値間の精度の違い、一方または両方の値の精度損失により、最下位桁が異なることになり、等しいと想定される浮動小数点値が、多くの場合、実際には等しくありません。 その結果、 Equals メソッドを呼び出して 2 つの値が等しいかどうかを判断したり、 CompareTo メソッドを呼び出して 2 つの Double 値間の関係を判断したりすると、多くの場合、予期しない結果が生じます。 次の例では、一見等しい 2 つの Double 値が実際には等しくないことを示しています。これは、最初の値の精度が 15 桁であるのに対し、2 番目の値の精度が 17 桁であるためです。
using System;
public class Example
{
public static void Main()
{
double value1 = .333333333333333;
double value2 = 1.0/3;
Console.WriteLine($"{value1} = {value2}: {value1.Equals(value2)}");
}
}
// The example displays the following output:
// 0.333333333333333 = 0.33333333333333331: False
open System
let value1 = 0.333333333333333
let value2 = 1. / 3.
printfn $"{value1:R} = {value2:R}: {value1.Equals value2}"
// The example displays the following output:
// 0.333333333333333 = 0.33333333333333331: False
Module Example1
Public Sub Run()
Dim value1 As Double = 0.333333333333333
Dim value2 As Double = 1 / 3
Console.WriteLine("{0} = {1}: {2}", value1, value2, value1.Equals(value2))
End Sub
End Module
' The example displays the following output:
' 0.333333333333333 = 0.33333333333333331: False
精度損失が比較結果に影響する可能性がある場合、Equals メソッドや CompareTo メソッドを呼び出す代わりに、次のいずれかの方法を採用できます。
Math.Round メソッドを呼び出して、両方の値の有効桁数が同じであることを確認します。 次の例では、2 つの小数部の値が同等になるように、この方法を使用するように前の例を変更します。
double value1 = .333333333333333; double value2 = 1.0 / 3; int precision = 7; value1 = Math.Round(value1, precision); value2 = Math.Round(value2, precision); Console.WriteLine($"{value1} = {value2}: {value1.Equals(value2)}"); // The example displays the following output: // 0.3333333 = 0.3333333: Trueopen System let v1 = 0.333333333333333 let v2 = 1. / 3. let precision = 7 let value1 = Math.Round(v1, precision) let value2 = Math.Round(v2, precision) printfn $"{value1:R} = {value2:R}: {value1.Equals value2}" // The example displays the following output: // 0.3333333 = 0.3333333: TrueModule Example3 Public Sub Run() Dim value1 As Double = 0.333333333333333 Dim value2 As Double = 1 / 3 Dim precision As Integer = 7 value1 = Math.Round(value1, precision) value2 = Math.Round(value2, precision) Console.WriteLine("{0} = {1}: {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 つの値が異なっていても等しいとみなせる絶対的な差の許容量を定義するか、または小さい方の値が大きい方の値からどれだけ離れていてもよいかを示す相対的な差の許容量を定義する必要があります。
Warning
Double.Epsilon は、等しいかどうかをテストするときに、2 つの Double 値間の距離の絶対メジャーとして使用される場合があります。 ただし、 Double.Epsilon は、値が 0 の Double に加算または減算できる、可能な限り小さい値を測定します。 ほとんどの正と負の Double 値では、 Double.Epsilon の値が小さすぎて検出できません。 したがって、0 の値を除き、等しいかどうかをテストで使用することはお勧めしません。
次の例では、後者のアプローチを使用して、2 つの値の相対差をテストする
IsApproximatelyEqualメソッドを定義します。 このメソッドはMath.Max(value1, value2)で除算されるため、比較は 2 つの値の大きい方 (大きさ) に対して相対的であり、結果は正しい桁になります。Math.Maxが 0 を返す場合 (1 つの値が 0 で、もう一方が負の値の場合に発生します)、このメソッドは 0 以外の値を除数として使用するためにMath.Min(value1, value2)にフォールバックします。 また、IsApproximatelyEqualメソッドと Equals(Double) メソッドの呼び出しの結果も比較します。using System; public class Example3 { public static void Main() { double one1 = .1 * 10; double one2 = 0; for (int ctr = 1; ctr <= 10; ctr++) one2 += .1; Console.WriteLine($"{one1} = {one2}: {one1.Equals(one2)}"); Console.WriteLine($"{one1} is approximately equal to {one2}: {IsApproximatelyEqual(one1, one2, .000000001)}"); } static bool IsApproximatelyEqual(double value1, double value2, double 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 = 0.99999999999999989: False // 1 is approximately equal to 0.99999999999999989: Trueopen System let isApproximatelyEqual (value1: double) (value2: double) (epsilon: double) = // If they are equal anyway, just return True. if value1.Equals value2 then true else // Handle NaN, Infinity. if Double.IsInfinity value1 || Double.IsNaN value1 then value1.Equals value2 elif Double.IsInfinity value2 || Double.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.1 * 10. let mutable one2 = 0. for _ = 1 to 10 do one2 <- one2 + 0.1 printfn $"{one1:R} = {one2:R}: {one1.Equals one2}" printfn $"{one1:R} is approximately equal to {one2:R}: {isApproximatelyEqual one1 one2 0.000000001}" // The example displays the following output: // 1 = 0.99999999999999989: False // 1 is approximately equal to 0.99999999999999989: TrueModule Example4 Public Sub Run() Dim one1 As Double = 0.1 * 10 Dim one2 As Double = 0 For ctr As Integer = 1 To 10 one2 += 0.1 Next Console.WriteLine("{0} = {1}: {2}", one1, one2, one1.Equals(one2)) Console.WriteLine("{0} is approximately equal to {1}: {2}", one1, one2, IsApproximatelyEqual(one1, one2, 0.000000001)) End Sub Function IsApproximatelyEqual(value1 As Double, value2 As Double, epsilon As Double) As Boolean ' If they are equal anyway, just return True. If value1.Equals(value2) Then Return True ' Handle NaN, Infinity. If Double.IsInfinity(value1) Or Double.IsNaN(value1) Then Return value1.Equals(value2) ElseIf Double.IsInfinity(value2) Or Double.IsNaN(value2) Then Return value1.Equals(value2) End If ' Handle zero to avoid division by zero Dim divisor As Double = 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 = 0.99999999999999989: False ' 1 is approximately equal to 0.99999999999999989: True
浮動小数点値と例外
整数型を使用する操作とは異なり、ゼロ除算のDivideByZeroExceptionをスローするか、OverflowExceptionでオーバーフローのをスローしますが、浮動小数点値を持つ操作では例外はスローされません。 代わりに、例外的な状況では、浮動小数点演算の結果はゼロ、正の無限大、負の無限大、または非数 (NaN) になります。
浮動小数点演算の結果が変換先の形式に対して小さすぎる場合、結果は 0 になります。 このような結果になる可能性があるのは、次の例に示すように、非常に小さい 2 つの数値を乗算した場合などです。
using System; public class Example6 { public static void Main() { Double value1 = 1.1632875981534209e-225; Double value2 = 9.1642346778e-175; Double result = value1 * value2; Console.WriteLine($"{value1} * {value2} = {result}"); Console.WriteLine($"{result} = 0: {result.Equals(0.0)}"); } } // The example displays the following output: // 1.16328759815342E-225 * 9.1642346778E-175 = 0 // 0 = 0: Truelet value1 = 1.1632875981534209e-225 let value2 = 9.1642346778e-175 let result = value1 * value2 printfn $"{value1} * {value2} = {result}" printfn $"{result} = 0: {result.Equals 0.0}" // The example displays the following output: // 1.16328759815342E-225 * 9.1642346778E-175 = 0 // 0 = 0: TrueModule Example7 Public Sub Run() Dim value1 As Double = 1.1632875981534209E-225 Dim value2 As Double = 9.1642346778E-175 Dim result As Double = value1 * value2 Console.WriteLine("{0} * {1} = {2}", value1, value2, result) Console.WriteLine("{0} = 0: {1}", result, result.Equals(0.0)) End Sub End Module ' The example displays the following output: ' 1.16328759815342E-225 * 9.1642346778E-175 = 0 ' 0 = 0: True浮動小数点演算の結果の大きさが変換先の形式の範囲を超えた場合、結果の符号に応じて、操作の結果は PositiveInfinity または NegativeInfinityされます。 次の例に示すように、 Double.MaxValue オーバーフローする操作の結果が PositiveInfinityされ、 Double.MinValue オーバーフローした操作の結果が NegativeInfinityされます。
using System; public class Example7 { public static void Main() { Double value1 = 4.565e153; Double value2 = 6.9375e172; Double result = value1 * value2; Console.WriteLine($"PositiveInfinity: {Double.IsPositiveInfinity(result)}"); Console.WriteLine($"NegativeInfinity: {Double.IsNegativeInfinity(result)}{Environment.NewLine}"); value1 = -value1; result = value1 * value2; Console.WriteLine($"PositiveInfinity: {Double.IsPositiveInfinity(result)}"); Console.WriteLine($"NegativeInfinity: {Double.IsNegativeInfinity(result)}"); } } // The example displays the following output: // PositiveInfinity: True // NegativeInfinity: False // // PositiveInfinity: False // NegativeInfinity: Trueopen System let value1 = 4.565e153 let value2 = 6.9375e172 let result = value1 * value2 printfn $"PositiveInfinity: {Double.IsPositiveInfinity result}" printfn $"NegativeInfinity: {Double.IsNegativeInfinity result}\n" let value3 = - value1 let result2 = value2 * value3 printfn $"PositiveInfinity: {Double.IsPositiveInfinity result2}" printfn $"NegativeInfinity: {Double.IsNegativeInfinity result2}" // The example displays the following output: // PositiveInfinity: True // NegativeInfinity: False // // PositiveInfinity: False // NegativeInfinity: TrueModule Example8 Public Sub Run() Dim value1 As Double = 4.565E+153 Dim value2 As Double = 6.9375E+172 Dim result As Double = value1 * value2 Console.WriteLine("PositiveInfinity: {0}", Double.IsPositiveInfinity(result)) Console.WriteLine("NegativeInfinity: {0}", Double.IsNegativeInfinity(result)) Console.WriteLine() value1 = -value1 result = value1 * value2 Console.WriteLine("PositiveInfinity: {0}", Double.IsPositiveInfinity(result)) Console.WriteLine("NegativeInfinity: {0}", Double.IsNegativeInfinity(result)) End Sub End Module ' The example displays the following output: ' PositiveInfinity: True ' NegativeInfinity: False ' ' PositiveInfinity: False ' NegativeInfinity: TruePositiveInfinity は正の被除数でゼロ除算を行った結果を示し、NegativeInfinity は負の被除数でゼロ除算を行った結果を示します。
浮動小数点演算が無効な場合、操作の結果は NaN。 たとえば、次の操作の結果 NaN します。
0 で除算し、被除数を 0 にします。 ゼロ除算の他のケースでは、 PositiveInfinity または NegativeInfinityが発生します。
無効な入力値に対する浮動小数点演算。 たとえば、負の値に対して Math.Sqrt メソッドを呼び出した場合や、1 より大きい値または -1 より小さい値に対して NaN メソッドを呼び出した場合には、Math.Acos が返されます。
値が Double.NaN引数を持つすべての操作。
型の変換
Double構造体では、明示的または暗黙的な変換演算子は定義されません。代わりに、変換はコンパイラによって実装されます。
任意のプリミティブ数値型の値を Double に変換するのは拡大変換であり、コンパイラが明示的に要求する場合を除き、明示的なキャスト演算子や変換メソッドの呼び出しは不要です。 たとえば、C# コンパイラでは、Decimal から Double への変換にキャスト演算子が必要ですが、Visual Basic コンパイラには必要ありません。 次の例では、他のプリミティブ数値型の最小値または最大値を Double に変換しています。
dynamic[] values = { Byte.MinValue, Byte.MaxValue, Decimal.MinValue,
Decimal.MaxValue, Int16.MinValue, Int16.MaxValue,
Int32.MinValue, Int32.MaxValue, Int64.MinValue,
Int64.MaxValue, SByte.MinValue, SByte.MaxValue,
Single.MinValue, Single.MaxValue, UInt16.MinValue,
UInt16.MaxValue, UInt32.MinValue, UInt32.MaxValue,
UInt64.MinValue, UInt64.MaxValue };
double dblValue;
foreach (dynamic value in values)
{
if (value.GetType() == typeof(decimal))
dblValue = (double)value;
else
dblValue = value;
Console.WriteLine($"{value} ({value.GetType().Name}) --> " +
$"{dblValue:R} ({dblValue.GetType().Name})");
}
// The example displays the following output:
// 0 (Byte) --> 0 (Double)
// 255 (Byte) --> 255 (Double)
// -79228162514264337593543950335 (Decimal) --> -7.9228162514264338E+28 (Double)
// 79228162514264337593543950335 (Decimal) --> 7.9228162514264338E+28 (Double)
// -32768 (Int16) --> -32768 (Double)
// 32767 (Int16) --> 32767 (Double)
// -2147483648 (Int32) --> -2147483648 (Double)
// 2147483647 (Int32) --> 2147483647 (Double)
// -9223372036854775808 (Int64) --> -9.2233720368547758E+18 (Double)
// 9223372036854775807 (Int64) --> 9.2233720368547758E+18 (Double)
// -128 (SByte) --> -128 (Double)
// 127 (SByte) --> 127 (Double)
// -3.402823E+38 (Single) --> -3.4028234663852886E+38 (Double)
// 3.402823E+38 (Single) --> 3.4028234663852886E+38 (Double)
// 0 (UInt16) --> 0 (Double)
// 65535 (UInt16) --> 65535 (Double)
// 0 (UInt32) --> 0 (Double)
// 4294967295 (UInt32) --> 4294967295 (Double)
// 0 (UInt64) --> 0 (Double)
// 18446744073709551615 (UInt64) --> 1.8446744073709552E+19 (Double)
open System
let values: obj[] =
[| Byte.MinValue; Byte.MaxValue; Decimal.MinValue
Decimal.MaxValue; Int16.MinValue; Int16.MaxValue
Int32.MinValue; Int32.MaxValue; Int64.MinValue
Int64.MaxValue; SByte.MinValue; SByte.MaxValue
Single.MinValue; Single.MaxValue; UInt16.MinValue
UInt16.MaxValue; UInt32.MinValue, UInt32.MaxValue
UInt64.MinValue; UInt64.MaxValue |]
for value in values do
let dblValue = value :?> double
printfn $"{value} ({value.GetType().Name}) --> {dblValue:R} ({dblValue.GetType().Name})"
// The example displays the following output:
// 0 (Byte) --> 0 (Double)
// 255 (Byte) --> 255 (Double)
// -79228162514264337593543950335 (Decimal) --> -7.9228162514264338E+28 (Double)
// 79228162514264337593543950335 (Decimal) --> 7.9228162514264338E+28 (Double)
// -32768 (Int16) --> -32768 (Double)
// 32767 (Int16) --> 32767 (Double)
// -2147483648 (Int32) --> -2147483648 (Double)
// 2147483647 (Int32) --> 2147483647 (Double)
// -9223372036854775808 (Int64) --> -9.2233720368547758E+18 (Double)
// 9223372036854775807 (Int64) --> 9.2233720368547758E+18 (Double)
// -128 (SByte) --> -128 (Double)
// 127 (SByte) --> 127 (Double)
// -3.402823E+38 (Single) --> -3.4028234663852886E+38 (Double)
// 3.402823E+38 (Single) --> 3.4028234663852886E+38 (Double)
// 0 (UInt16) --> 0 (Double)
// 65535 (UInt16) --> 65535 (Double)
// 0 (UInt32) --> 0 (Double)
// 4294967295 (UInt32) --> 4294967295 (Double)
// 0 (UInt64) --> 0 (Double)
// 18446744073709551615 (UInt64) --> 1.8446744073709552E+19 (Double)
Module Example5
Public Sub Run()
Dim values() As Object = {Byte.MinValue, Byte.MaxValue, Decimal.MinValue,
Decimal.MaxValue, Int16.MinValue, Int16.MaxValue,
Int32.MinValue, Int32.MaxValue, Int64.MinValue,
Int64.MaxValue, SByte.MinValue, SByte.MaxValue,
Single.MinValue, Single.MaxValue, UInt16.MinValue,
UInt16.MaxValue, UInt32.MinValue, UInt32.MaxValue,
UInt64.MinValue, UInt64.MaxValue}
Dim dblValue As Double
For Each value In values
dblValue = value
Console.WriteLine("{0} ({1}) --> {2:R} ({3})",
value, value.GetType().Name,
dblValue, dblValue.GetType().Name)
Next
End Sub
End Module
' The example displays the following output:
' 0 (Byte) --> 0 (Double)
' 255 (Byte) --> 255 (Double)
' -79228162514264337593543950335 (Decimal) --> -7.9228162514264338E+28 (Double)
' 79228162514264337593543950335 (Decimal) --> 7.9228162514264338E+28 (Double)
' -32768 (Int16) --> -32768 (Double)
' 32767 (Int16) --> 32767 (Double)
' -2147483648 (Int32) --> -2147483648 (Double)
' 2147483647 (Int32) --> 2147483647 (Double)
' -9223372036854775808 (Int64) --> -9.2233720368547758E+18 (Double)
' 9223372036854775807 (Int64) --> 9.2233720368547758E+18 (Double)
' -128 (SByte) --> -128 (Double)
' 127 (SByte) --> 127 (Double)
' -3.402823E+38 (Single) --> -3.4028234663852886E+38 (Double)
' 3.402823E+38 (Single) --> 3.4028234663852886E+38 (Double)
' 0 (UInt16) --> 0 (Double)
' 65535 (UInt16) --> 65535 (Double)
' 0 (UInt32) --> 0 (Double)
' 4294967295 (UInt32) --> 4294967295 (Double)
' 0 (UInt64) --> 0 (Double)
' 18446744073709551615 (UInt64) --> 1.8446744073709552E+19 (Double)
さらに、Single値はそれぞれSingle.NaN、Single.PositiveInfinity、およびSingle.NegativeInfinityDouble.NaN、Double.PositiveInfinity、およびDouble.NegativeInfinityに変換されます。
一部の数値型の値を Double 値に変換すると、精度が失われる可能性があることに注意してください。 この例が示すように、Decimal、Int64、UInt64 の値を Double 値に変換すると、精度が失われる可能性があります。
Double値から他のプリミティブ数値データ型の値への変換は縮小変換であり、キャスト演算子 (C#)、変換メソッド (Visual Basic)、または Convert メソッドの呼び出しが必要です。 ターゲット型の MinValue プロパティと MaxValue プロパティによって定義されるターゲット データ型の範囲外の値は、次の表に示すように動作します。
| ターゲットの種類 | Result |
|---|---|
| 任意の整数型 | チェックされたコンテキストで変換が発生した場合の OverflowException 例外。 オフのコンテキスト (C# の既定値) で変換が発生した場合、変換操作は成功しますが、値はオーバーフローします。 |
| Decimal | OverflowException例外。 |
| Single | 負の値の場合は Single.NegativeInfinity。 正の値の場合は Single.PositiveInfinity。 |
さらに、 Double.NaN、 Double.PositiveInfinity、および Double.NegativeInfinity は、チェック されたコンテキスト内の整数への変換の OverflowException をスローしますが、チェックされていないコンテキストで整数に変換すると、これらの値がオーバーフローします。 Decimal への変換では、常に OverflowExceptionがスローされます。 Singleへの変換では、それぞれSingle.NaN、Single.PositiveInfinity、およびSingle.NegativeInfinityに変換されます。
精度が低下すると、 Double 値が別の数値型に変換される可能性があります。 整数型のいずれかに変換する場合、例の出力に示すように、Double 値が丸められた場合 (Visual Basicのように) または切り捨てられると、小数部分が失われます (C# のように)。 Decimal値とSingle値への変換では、Double値がターゲット データ型で正確に表現されない場合があります。
次の例では、複数の Double 値を他のいくつかの数値型に変換します。 変換は、Visual Basic (既定)、C# (checked キーワードのため)、F# (Checked モジュールのため) のチェックされたコンテキストで発生します。 この例の出力は、チェックされていないコンテキストの両方で変換の結果を示しています。
/removeintchecks+ コンパイラ スイッチを使用してコンパイルし、C# で checked ステートメントをコメントアウトし、F# で open Checked ステートメントをコメントアウトすることで、Visual Basic のチェックされていないコンテキストで変換を実行できます。
using System;
public class Example5
{
public static void Main()
{
Double[] values = { Double.MinValue, -67890.1234, -12345.6789,
12345.6789, 67890.1234, Double.MaxValue,
Double.NaN, Double.PositiveInfinity,
Double.NegativeInfinity };
checked
{
foreach (var value in values)
{
try
{
Int64 lValue = (long)value;
Console.WriteLine($"{value} ({value.GetType().Name}) --> {lValue} (0x{lValue:X16}) ({lValue.GetType().Name})");
}
catch (OverflowException)
{
Console.WriteLine($"Unable to convert {value} to Int64.");
}
try
{
UInt64 ulValue = (ulong)value;
Console.WriteLine($"{value} ({value.GetType().Name}) --> {ulValue} (0x{ulValue:X16}) ({ulValue.GetType().Name})");
}
catch (OverflowException)
{
Console.WriteLine($"Unable to convert {value} to UInt64.");
}
try
{
Decimal dValue = (decimal)value;
Console.WriteLine($"{value} ({value.GetType().Name}) --> {dValue} ({dValue.GetType().Name})");
}
catch (OverflowException)
{
Console.WriteLine($"Unable to convert {value} to Decimal.");
}
try
{
Single sValue = (float)value;
Console.WriteLine($"{value} ({value.GetType().Name}) --> {sValue} ({sValue.GetType().Name})");
}
catch (OverflowException)
{
Console.WriteLine($"Unable to convert {value} to Single.");
}
Console.WriteLine();
}
}
}
}
// The example displays the following output for conversions performed
// in a checked context:
// Unable to convert -1.79769313486232E+308 to Int64.
// Unable to convert -1.79769313486232E+308 to UInt64.
// Unable to convert -1.79769313486232E+308 to Decimal.
// -1.79769313486232E+308 (Double) --> -Infinity (Single)
//
// -67890.1234 (Double) --> -67890 (0xFFFFFFFFFFFEF6CE) (Int64)
// Unable to convert -67890.1234 to UInt64.
// -67890.1234 (Double) --> -67890.1234 (Decimal)
// -67890.1234 (Double) --> -67890.13 (Single)
//
// -12345.6789 (Double) --> -12345 (0xFFFFFFFFFFFFCFC7) (Int64)
// Unable to convert -12345.6789 to UInt64.
// -12345.6789 (Double) --> -12345.6789 (Decimal)
// -12345.6789 (Double) --> -12345.68 (Single)
//
// 12345.6789 (Double) --> 12345 (0x0000000000003039) (Int64)
// 12345.6789 (Double) --> 12345 (0x0000000000003039) (UInt64)
// 12345.6789 (Double) --> 12345.6789 (Decimal)
// 12345.6789 (Double) --> 12345.68 (Single)
//
// 67890.1234 (Double) --> 67890 (0x0000000000010932) (Int64)
// 67890.1234 (Double) --> 67890 (0x0000000000010932) (UInt64)
// 67890.1234 (Double) --> 67890.1234 (Decimal)
// 67890.1234 (Double) --> 67890.13 (Single)
//
// Unable to convert 1.79769313486232E+308 to Int64.
// Unable to convert 1.79769313486232E+308 to UInt64.
// Unable to convert 1.79769313486232E+308 to Decimal.
// 1.79769313486232E+308 (Double) --> Infinity (Single)
//
// Unable to convert NaN to Int64.
// Unable to convert NaN to UInt64.
// Unable to convert NaN to Decimal.
// NaN (Double) --> NaN (Single)
//
// Unable to convert Infinity to Int64.
// Unable to convert Infinity to UInt64.
// Unable to convert Infinity to Decimal.
// Infinity (Double) --> Infinity (Single)
//
// Unable to convert -Infinity to Int64.
// Unable to convert -Infinity to UInt64.
// Unable to convert -Infinity to Decimal.
// -Infinity (Double) --> -Infinity (Single)
// The example displays the following output for conversions performed
// in an unchecked context:
// -1.79769313486232E+308 (Double) --> -9223372036854775808 (0x8000000000000000) (Int64)
// -1.79769313486232E+308 (Double) --> 9223372036854775808 (0x8000000000000000) (UInt64)
// Unable to convert -1.79769313486232E+308 to Decimal.
// -1.79769313486232E+308 (Double) --> -Infinity (Single)
//
// -67890.1234 (Double) --> -67890 (0xFFFFFFFFFFFEF6CE) (Int64)
// -67890.1234 (Double) --> 18446744073709483726 (0xFFFFFFFFFFFEF6CE) (UInt64)
// -67890.1234 (Double) --> -67890.1234 (Decimal)
// -67890.1234 (Double) --> -67890.13 (Single)
//
// -12345.6789 (Double) --> -12345 (0xFFFFFFFFFFFFCFC7) (Int64)
// -12345.6789 (Double) --> 18446744073709539271 (0xFFFFFFFFFFFFCFC7) (UInt64)
// -12345.6789 (Double) --> -12345.6789 (Decimal)
// -12345.6789 (Double) --> -12345.68 (Single)
//
// 12345.6789 (Double) --> 12345 (0x0000000000003039) (Int64)
// 12345.6789 (Double) --> 12345 (0x0000000000003039) (UInt64)
// 12345.6789 (Double) --> 12345.6789 (Decimal)
// 12345.6789 (Double) --> 12345.68 (Single)
//
// 67890.1234 (Double) --> 67890 (0x0000000000010932) (Int64)
// 67890.1234 (Double) --> 67890 (0x0000000000010932) (UInt64)
// 67890.1234 (Double) --> 67890.1234 (Decimal)
// 67890.1234 (Double) --> 67890.13 (Single)
//
// 1.79769313486232E+308 (Double) --> -9223372036854775808 (0x8000000000000000) (Int64)
// 1.79769313486232E+308 (Double) --> 0 (0x0000000000000000) (UInt64)
// Unable to convert 1.79769313486232E+308 to Decimal.
// 1.79769313486232E+308 (Double) --> Infinity (Single)
//
// NaN (Double) --> -9223372036854775808 (0x8000000000000000) (Int64)
// NaN (Double) --> 0 (0x0000000000000000) (UInt64)
// Unable to convert NaN to Decimal.
// NaN (Double) --> NaN (Single)
//
// Infinity (Double) --> -9223372036854775808 (0x8000000000000000) (Int64)
// Infinity (Double) --> 0 (0x0000000000000000) (UInt64)
// Unable to convert Infinity to Decimal.
// Infinity (Double) --> Infinity (Single)
//
// -Infinity (Double) --> -9223372036854775808 (0x8000000000000000) (Int64)
// -Infinity (Double) --> 9223372036854775808 (0x8000000000000000) (UInt64)
// Unable to convert -Infinity to Decimal.
// -Infinity (Double) --> -Infinity (Single)
open System
open Checked
let values =
[| Double.MinValue; -67890.1234; -12345.6789
12345.6789; 67890.1234; Double.MaxValue
Double.NaN; Double.PositiveInfinity;
Double.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."
try
let sValue = float32 value
printfn $"{value} ({value.GetType().Name}) --> {sValue} ({sValue.GetType().Name})"
with :? OverflowException ->
printfn $"Unable to convert {value} to Single."
printfn ""
// The example displays the following output for conversions performed
// in a checked context:
// Unable to convert -1.79769313486232E+308 to Int64.
// Unable to convert -1.79769313486232E+308 to UInt64.
// Unable to convert -1.79769313486232E+308 to Decimal.
// -1.79769313486232E+308 (Double) --> -Infinity (Single)
//
// -67890.1234 (Double) --> -67890 (0xFFFFFFFFFFFEF6CE) (Int64)
// Unable to convert -67890.1234 to UInt64.
// -67890.1234 (Double) --> -67890.1234 (Decimal)
// -67890.1234 (Double) --> -67890.13 (Single)
//
// -12345.6789 (Double) --> -12345 (0xFFFFFFFFFFFFCFC7) (Int64)
// Unable to convert -12345.6789 to UInt64.
// -12345.6789 (Double) --> -12345.6789 (Decimal)
// -12345.6789 (Double) --> -12345.68 (Single)
//
// 12345.6789 (Double) --> 12345 (0x0000000000003039) (Int64)
// 12345.6789 (Double) --> 12345 (0x0000000000003039) (UInt64)
// 12345.6789 (Double) --> 12345.6789 (Decimal)
// 12345.6789 (Double) --> 12345.68 (Single)
//
// 67890.1234 (Double) --> 67890 (0x0000000000010932) (Int64)
// 67890.1234 (Double) --> 67890 (0x0000000000010932) (UInt64)
// 67890.1234 (Double) --> 67890.1234 (Decimal)
// 67890.1234 (Double) --> 67890.13 (Single)
//
// Unable to convert 1.79769313486232E+308 to Int64.
// Unable to convert 1.79769313486232E+308 to UInt64.
// Unable to convert 1.79769313486232E+308 to Decimal.
// 1.79769313486232E+308 (Double) --> Infinity (Single)
//
// Unable to convert NaN to Int64.
// Unable to convert NaN to UInt64.
// Unable to convert NaN to Decimal.
// NaN (Double) --> NaN (Single)
//
// Unable to convert Infinity to Int64.
// Unable to convert Infinity to UInt64.
// Unable to convert Infinity to Decimal.
// Infinity (Double) --> Infinity (Single)
//
// Unable to convert -Infinity to Int64.
// Unable to convert -Infinity to UInt64.
// Unable to convert -Infinity to Decimal.
// -Infinity (Double) --> -Infinity (Single)
// The example displays the following output for conversions performed
// in an unchecked context:
// -1.79769313486232E+308 (Double) --> -9223372036854775808 (0x8000000000000000) (Int64)
// -1.79769313486232E+308 (Double) --> 9223372036854775808 (0x8000000000000000) (UInt64)
// Unable to convert -1.79769313486232E+308 to Decimal.
// -1.79769313486232E+308 (Double) --> -Infinity (Single)
//
// -67890.1234 (Double) --> -67890 (0xFFFFFFFFFFFEF6CE) (Int64)
// -67890.1234 (Double) --> 18446744073709483726 (0xFFFFFFFFFFFEF6CE) (UInt64)
// -67890.1234 (Double) --> -67890.1234 (Decimal)
// -67890.1234 (Double) --> -67890.13 (Single)
//
// -12345.6789 (Double) --> -12345 (0xFFFFFFFFFFFFCFC7) (Int64)
// -12345.6789 (Double) --> 18446744073709539271 (0xFFFFFFFFFFFFCFC7) (UInt64)
// -12345.6789 (Double) --> -12345.6789 (Decimal)
// -12345.6789 (Double) --> -12345.68 (Single)
//
// 12345.6789 (Double) --> 12345 (0x0000000000003039) (Int64)
// 12345.6789 (Double) --> 12345 (0x0000000000003039) (UInt64)
// 12345.6789 (Double) --> 12345.6789 (Decimal)
// 12345.6789 (Double) --> 12345.68 (Single)
//
// 67890.1234 (Double) --> 67890 (0x0000000000010932) (Int64)
// 67890.1234 (Double) --> 67890 (0x0000000000010932) (UInt64)
// 67890.1234 (Double) --> 67890.1234 (Decimal)
// 67890.1234 (Double) --> 67890.13 (Single)
//
// 1.79769313486232E+308 (Double) --> -9223372036854775808 (0x8000000000000000) (Int64)
// 1.79769313486232E+308 (Double) --> 0 (0x0000000000000000) (UInt64)
// Unable to convert 1.79769313486232E+308 to Decimal.
// 1.79769313486232E+308 (Double) --> Infinity (Single)
//
// NaN (Double) --> -9223372036854775808 (0x8000000000000000) (Int64)
// NaN (Double) --> 0 (0x0000000000000000) (UInt64)
// Unable to convert NaN to Decimal.
// NaN (Double) --> NaN (Single)
//
// Infinity (Double) --> -9223372036854775808 (0x8000000000000000) (Int64)
// Infinity (Double) --> 0 (0x0000000000000000) (UInt64)
// Unable to convert Infinity to Decimal.
// Infinity (Double) --> Infinity (Single)
//
// -Infinity (Double) --> -9223372036854775808 (0x8000000000000000) (Int64)
// -Infinity (Double) --> 9223372036854775808 (0x8000000000000000) (UInt64)
// Unable to convert -Infinity to Decimal.
// -Infinity (Double) --> -Infinity (Single)
Module Example6
Public Sub Run()
Dim values() As Double = {Double.MinValue, -67890.1234, -12345.6789,
12345.6789, 67890.1234, Double.MaxValue,
Double.NaN, Double.PositiveInfinity,
Double.NegativeInfinity}
For Each value In values
Try
Dim lValue As Int64 = 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
Try
Dim sValue As Single = CSng(value)
Console.WriteLine("{0} ({1}) --> {2} ({3})",
value, value.GetType().Name,
sValue, sValue.GetType().Name)
Catch e As OverflowException
Console.WriteLine("Unable to convert {0} to Single.", value)
End Try
Console.WriteLine()
Next
End Sub
End Module
' The example displays the following output for conversions performed
' in a checked context:
' Unable to convert -1.79769313486232E+308 to Int64.
' Unable to convert -1.79769313486232E+308 to UInt64.
' Unable to convert -1.79769313486232E+308 to Decimal.
' -1.79769313486232E+308 (Double) --> -Infinity (Single)
'
' -67890.1234 (Double) --> -67890 (0xFFFFFFFFFFFEF6CE) (Int64)
' Unable to convert -67890.1234 to UInt64.
' -67890.1234 (Double) --> -67890.1234 (Decimal)
' -67890.1234 (Double) --> -67890.13 (Single)
'
' -12345.6789 (Double) --> -12346 (0xFFFFFFFFFFFFCFC6) (Int64)
' Unable to convert -12345.6789 to UInt64.
' -12345.6789 (Double) --> -12345.6789 (Decimal)
' -12345.6789 (Double) --> -12345.68 (Single)
'
' 12345.6789 (Double) --> 12346 (0x000000000000303A) (Int64)
' 12345.6789 (Double) --> 12346 (0x000000000000303A) (UInt64)
' 12345.6789 (Double) --> 12345.6789 (Decimal)
' 12345.6789 (Double) --> 12345.68 (Single)
'
' 67890.1234 (Double) --> 67890 (0x0000000000010932) (Int64)
' 67890.1234 (Double) --> 67890 (0x0000000000010932) (UInt64)
' 67890.1234 (Double) --> 67890.1234 (Decimal)
' 67890.1234 (Double) --> 67890.13 (Single)
'
' Unable to convert 1.79769313486232E+308 to Int64.
' Unable to convert 1.79769313486232E+308 to UInt64.
' Unable to convert 1.79769313486232E+308 to Decimal.
' 1.79769313486232E+308 (Double) --> Infinity (Single)
'
' Unable to convert NaN to Int64.
' Unable to convert NaN to UInt64.
' Unable to convert NaN to Decimal.
' NaN (Double) --> NaN (Single)
'
' Unable to convert Infinity to Int64.
' Unable to convert Infinity to UInt64.
' Unable to convert Infinity to Decimal.
' Infinity (Double) --> Infinity (Single)
'
' Unable to convert -Infinity to Int64.
' Unable to convert -Infinity to UInt64.
' Unable to convert -Infinity to Decimal.
' -Infinity (Double) --> -Infinity (Single)
' The example displays the following output for conversions performed
' in an unchecked context:
' -1.79769313486232E+308 (Double) --> -9223372036854775808 (0x8000000000000000) (Int64)
' -1.79769313486232E+308 (Double) --> 9223372036854775808 (0x8000000000000000) (UInt64)
' Unable to convert -1.79769313486232E+308 to Decimal.
' -1.79769313486232E+308 (Double) --> -Infinity (Single)
'
' -67890.1234 (Double) --> -67890 (0xFFFFFFFFFFFEF6CE) (Int64)
' -67890.1234 (Double) --> 18446744073709483726 (0xFFFFFFFFFFFEF6CE) (UInt64)
' -67890.1234 (Double) --> -67890.1234 (Decimal)
' -67890.1234 (Double) --> -67890.13 (Single)
'
' -12345.6789 (Double) --> -12346 (0xFFFFFFFFFFFFCFC6) (Int64)
' -12345.6789 (Double) --> 18446744073709539270 (0xFFFFFFFFFFFFCFC6) (UInt64)
' -12345.6789 (Double) --> -12345.6789 (Decimal)
' -12345.6789 (Double) --> -12345.68 (Single)
'
' 12345.6789 (Double) --> 12346 (0x000000000000303A) (Int64)
' 12345.6789 (Double) --> 12346 (0x000000000000303A) (UInt64)
' 12345.6789 (Double) --> 12345.6789 (Decimal)
' 12345.6789 (Double) --> 12345.68 (Single)
'
' 67890.1234 (Double) --> 67890 (0x0000000000010932) (Int64)
' 67890.1234 (Double) --> 67890 (0x0000000000010932) (UInt64)
' 67890.1234 (Double) --> 67890.1234 (Decimal)
' 67890.1234 (Double) --> 67890.13 (Single)
'
' 1.79769313486232E+308 (Double) --> -9223372036854775808 (0x8000000000000000) (Int64)
' 1.79769313486232E+308 (Double) --> 0 (0x0000000000000000) (UInt64)
' Unable to convert 1.79769313486232E+308 to Decimal.
' 1.79769313486232E+308 (Double) --> Infinity (Single)
'
' NaN (Double) --> -9223372036854775808 (0x8000000000000000) (Int64)
' NaN (Double) --> 0 (0x0000000000000000) (UInt64)
' Unable to convert NaN to Decimal.
' NaN (Double) --> NaN (Single)
'
' Infinity (Double) --> -9223372036854775808 (0x8000000000000000) (Int64)
' Infinity (Double) --> 0 (0x0000000000000000) (UInt64)
' Unable to convert Infinity to Decimal.
' Infinity (Double) --> Infinity (Single)
'
' -Infinity (Double) --> -9223372036854775808 (0x8000000000000000) (Int64)
' -Infinity (Double) --> 9223372036854775808 (0x8000000000000000) (UInt64)
' Unable to convert -Infinity to Decimal.
' -Infinity (Double) --> -Infinity (Single)
数値型の変換の詳細については、「 .NET での型変換 」および「 型変換テーブル」を参照してください。
浮動小数点機能
Double 構造体および関連する型には、次の領域での操作を実行するメソッドが用意されています。
値の比較。 Equals メソッドを呼び出して、2 つのDouble値が等しいかどうかを判断するか、CompareToメソッドを呼び出して 2 つの値間の関係を判断できます。
Double構造体は、比較演算子の完全なセットもサポートしています。 たとえば、等価性または非等価性をテストしたり、ある値が別の値より大きいか等しいかを判断したりすることができます。 オペランドのいずれかが Double 以外の数値型である場合、比較前に Double に変換されます。
Warning
精度の違いにより、等しいと予想される 2 つの Double 値が等しくない場合があり、比較の結果に影響します。 2 つの Double 値を比較する方法については、「 等しいかどうかをテスト する」セクションを参照してください。
IsNaN、IsInfinity、IsPositiveInfinity、およびIsNegativeInfinityメソッドを呼び出して、これらの特別な値をテストすることもできます。
数学演算。 加算、減算、乗算、除算などの一般的な算術演算は、Double のメソッドではなく、言語コンパイラおよび共通中間言語 (CIL) の命令によって実装されます。 数学演算のオペランドの 1 つが Double以外の数値型の場合、演算を実行する前に Double に変換されます。 演算の結果も Double 値になります。
その他の算術演算は、
staticクラスでShared(Visual Basic のSystem.Math) メソッドを呼び出すことによって実行できます。 これには、算術 (Math.Abs、Math.Sign、Math.Sqrt など)、幾何学 (Math.Cos、Math.Sin など)、および微積分 (Math.Log など) で一般的に使用される追加のメソッドが含まれています。Double値内の個々のビットを操作することもできます。 BitConverter.DoubleToInt64Bits メソッドは Double 値のビット パターンを 64 ビット整数として保持します。 BitConverter.GetBytes(Double) メソッドは、バイト配列内のビット パターンを返します。
丸め。 丸めは、浮動小数点表現と精度の問題によって生じる値間の差の影響を軽減するための手法としてよく使用されます。 Double メソッドを呼び出すと、Math.Round値を丸めることができます。
書式設定。 Double メソッドを呼び出すか、複合書式指定機能を使用して、ToString 値をその文字列表現に変換できます。 書式指定文字列が浮動小数点値の文字列表現をどのように制御するかの詳細については、「標準の数値書式指定文字列」および「カスタム数値書式指定文字列」を参照してください。
文字列の解析。 Double メソッドまたは Parse メソッドのいずれかを呼び出すことで、浮動小数点値の文字列表現を TryParse 値に変換できます。 解析操作が失敗した場合、 Parse メソッドは例外をスローしますが、 TryParse メソッドは
falseを返します。型変換。 Double 構造体は、IConvertible インターフェイスの明示的なインターフェイス実装を提供します。これは、任意の 2 つの標準.NET データ型間の変換をサポートします。 言語コンパイラは、他のすべての標準数値型の値から Double 値への暗黙的な変換もサポートしています。 任意の標準数値型の値を Double に変換するのは拡大変換であり、ユーザーがキャスト演算子や変換メソッドを使用する必要はありません。
ただし、Int64 および Single の値を変換すると、精度が失われる可能性があります。 次の表に、これらの型ごとの精度の違いを示します。
タイプ 最大精度 内部精度 Double 15 17 Int64 19 桁の 10 進精度 19 桁の 10 進精度 Single 7 桁の 10 進精度 9 桁の 10 進精度 精度の問題は、Single値に変換されるDouble値に最も頻繁に影響します。 次の例では、同じ除算演算によって生成された 2 つの値が等しくありません。これは、一方の値が Double に変換された単精度浮動小数点値であるためです。
using System; public class Example13 { public static void Main() { Double value = .1; Double result1 = value * 10; Double result2 = 0; for (int ctr = 1; ctr <= 10; ctr++) result2 += value; Console.WriteLine($".1 * 10: {result1:R}"); Console.WriteLine($".1 Added 10 times: {result2:R}"); } } // The example displays the following output: // .1 * 10: 1 // .1 Added 10 times: 0.99999999999999989let value = 0.1 let result1 = value * 10. let mutable result2 = 0. for i = 1 to 10 do result2 <- result2 + value printfn $".1 * 10: {result1:R}" printfn $".1 Added 10 times: {result2:R}" // The example displays the following output: // .1 * 10: 1 // .1 Added 10 times: 0.99999999999999989Module Example14 Public Sub Run() Dim value As Double = 0.1 Dim result1 As Double = value * 10 Dim result2 As Double For ctr As Integer = 1 To 10 result2 += value Next Console.WriteLine(".1 * 10: {0:R}", result1) Console.WriteLine(".1 Added 10 times: {0:R}", result2) End Sub End Module ' The example displays the following output: ' .1 * 10: 1 ' .1 Added 10 times: 0.99999999999999989
フィールド
| 名前 | 説明 |
|---|---|
| E |
定数 e で指定された自然対数底を表します。 |
| Epsilon |
0 より大きい最小の正 Double 値を表します。 このフィールドは定数です。 |
| MaxValue |
Doubleの可能な最大値を表します。 このフィールドは定数です。 |
| MinValue |
Doubleの可能な限り小さい値を表します。 このフィールドは定数です。 |
| NaN |
数値 ( |
| NegativeInfinity |
負の無限大を表します。 このフィールドは定数です。 |
| NegativeZero |
負の 0 (-0) の数値を表します。 |
| Pi |
円の円周の直径に対する比率を表します。定数πで指定します。 |
| PositiveInfinity |
正の無限大を表します。 このフィールドは定数です。 |
| Tau |
定数 π で指定された、1 ターンのラジアンの数を表します。 |
メソッド
| 名前 | 説明 |
|---|---|
| Abs(Double) |
値の絶対を計算します。 |
| Acos(Double) |
値のアークコサインを計算します。 |
| Acosh(Double) |
値の双曲線アークコサインを計算します。 |
| AcosPi(Double) |
値のアークコサインを計算し、結果を |
| Asin(Double) |
値のアークサインを計算します。 |
| Asinh(Double) |
値の双曲線アークサインを計算します。 |
| AsinPi(Double) |
値のアークサインを計算し、結果を |
| Atan(Double) |
値のアークタンジェントを計算します。 |
| Atan2(Double, Double) |
2 つの値の商のアークタンジェントを計算します。 |
| Atan2Pi(Double, Double) |
2 つの値の商のアークタンジェントを計算し、結果を |
| Atanh(Double) |
値の双曲線アークタンジェントを計算します。 |
| AtanPi(Double) |
値のアークタンジェントを計算し、結果を pi で除算します。 |
| BitDecrement(Double) |
指定した値より小さい値を比較する最大値を返します。 |
| BitIncrement(Double) |
指定した値より大きい値を比較する最小の値を返します。 |
| Cbrt(Double) |
値のキューブ ルートを計算します。 |
| Ceiling(Double) |
値の上限を計算します。 |
| Clamp(Double, Double, Double) |
値を包括的な最小値と最大値にクランプします。 |
| ClampNative(Double, Double, Double) |
|
| CompareTo(Double) |
このインスタンスを指定した倍精度浮動小数点数と比較し、このインスタンスの値が指定された倍精度浮動小数点数の値より小さいか、等しいか、または大きいかを示す整数を返します。 |
| CompareTo(Object) |
このインスタンスを指定したオブジェクトと比較し、このインスタンスの値が指定したオブジェクトの値より小さいか、等しいか、または大きいかを示す整数を返します。 |
| ConvertToInteger<TInteger>(Double) |
オーバーフロー時の彩度を使用して、値を指定した整数型に変換します。 |
| ConvertToIntegerNative<TInteger>(Double) |
オーバーフロー時のプラットフォーム固有の動作を使用して、値を指定された整数型に変換します。 |
| CopySign(Double, Double) |
値の符号を別の値の符号にコピーします。 |
| Cos(Double) |
値のコサインを計算します。 |
| Cosh(Double) |
値の双曲線余弦を計算します。 |
| CosPi(Double) |
|
| CreateChecked<TOther>(TOther) |
現在の型のインスタンスを値から作成し、現在の型の表現可能範囲外の値に対してオーバーフロー例外をスローします。 |
| CreateSaturating<TOther>(TOther) |
現在の型のインスタンスを値から作成し、現在の型の表現可能な範囲外の値を飽和させます。 |
| CreateTruncating<TOther>(TOther) |
現在の型のインスタンスを値から作成し、現在の型の表現可能範囲外の値を切り捨てます。 |
| DegreesToRadians(Double) |
指定した値を度からラジアンに変換します。 |
| Equals(Double) |
このインスタンスと指定した Double オブジェクトが同じ値を表すかどうかを示す値を返します。 |
| Equals(Object) |
このインスタンスが指定したオブジェクトと等しいかどうかを示す値を返します。 |
| Exp(Double) |
コンピューティング |
| Exp10(Double) |
コンピューティング |
| Exp10M1(Double) |
|
| Exp2(Double) |
コンピューティング |
| Exp2M1(Double) |
|
| ExpM1(Double) |
|
| Floor(Double) |
値の床を計算します。 |
| FusedMultiplyAdd(Double, Double, Double) |
3 つの値の融合乗算加算を計算します。 |
| GetHashCode() |
このインスタンスのハッシュ コードを返します。 |
| GetTypeCode() | |
| Hypot(Double, Double) |
直角三角形の短辺の長さを表す 2 つの値を指定して、低血圧を計算します。 |
| Ieee754Remainder(Double, Double) |
IEEE 754 で指定された 2 つの値の剰余を計算します。 |
| ILogB(Double) |
値の整数対数を計算します。 |
| IsEvenInteger(Double) |
値が偶数の整数を表すかどうかを判断します。 |
| IsFinite(Double) |
指定した値が有限 (ゼロ、非正規、または正規) かどうかを判断します。 |
| IsInfinity(Double) |
指定した数が負または正の無限大に評価されるかどうかを示す値を返します。 |
| IsInteger(Double) |
値が整数値を表すかどうかを判断します。 |
| IsNaN(Double) |
指定した値が数値 (NaN) でないかどうかを示す値を返します。 |
| IsNegative(Double) |
指定した値が負の値かどうかを判断します。 |
| IsNegativeInfinity(Double) |
指定した数が負の無限大に評価されるかどうかを示す値を返します。 |
| IsNormal(Double) |
指定した値が正規かどうかを判断します。 |
| IsOddInteger(Double) |
値が奇数の整数を表すかどうかを判断します。 |
| IsPositive(Double) |
値が正かどうかを判断します。 |
| IsPositiveInfinity(Double) |
指定した数が正の無限大に評価されるかどうかを示す値を返します。 |
| IsPow2(Double) |
値が 2 の累乗であるかどうかを判断します。 |
| IsRealNumber(Double) |
値が実数を表すかどうかを判断します。 |
| IsSubnormal(Double) |
指定した値が非正規かどうかを判断します。 |
| Lerp(Double, Double, Double) |
指定された重みに基づいて、2 つの値の間で線形補間を実行します。 |
| Log(Double, Double) |
指定した底の値の対数を計算します。 |
| Log(Double) |
値の自然対数 ( |
| Log10(Double) |
値の底 10 対数を計算します。 |
| Log10P1(Double) |
値に 1 を加えた底 10 の対数を計算します。 |
| Log2(Double) |
値の log2 を計算します。 |
| Log2P1(Double) |
値に 1 を加えた底 2 の対数を計算します。 |
| LogP1(Double) |
値に 1 を加えた自然対数 ( |
| Max(Double, Double) |
2 つの値を、より大きいコンピューティングと比較します。 |
| MaxMagnitude(Double, Double) |
2 つの値を、より大きいコンピューティングと比較します。 |
| MaxMagnitudeNumber(Double, Double) |
2 つの値を比較して、大きさが大きい計算を行い、入力が |
| MaxNative(Double, Double) |
|
| MaxNumber(Double, Double) |
2 つの値を比較して、より大きい値を計算し、入力が |
| Min(Double, Double) |
2 つの値を比較して計算します。計算の方が少なくなります。 |
| MinMagnitude(Double, Double) |
2 つの値を比較して計算します。計算の方が少なくなります。 |
| MinMagnitudeNumber(Double, Double) |
2 つの値を比較して、大きさが小さい計算を行い、入力が |
| MinNative(Double, Double) |
2 つの値を比較し、 |
| MinNumber(Double, Double) |
2 つの値を比較して計算します。小さい方が計算され、入力が |
| MultiplyAddEstimate(Double, Double, Double) |
( |
| Parse(ReadOnlySpan<Byte>, IFormatProvider) |
UTF-8 文字のスパンを値に解析します。 |
| Parse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider) |
UTF-8 文字のスパンを値に解析します。 |
| Parse(ReadOnlySpan<Char>, IFormatProvider) |
文字のスパンを値に解析します。 |
| Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider) |
指定したスタイルおよびカルチャ固有の形式の数値の文字列形式を含む文字スパンを、等価の倍精度浮動小数点数に変換します。 |
| Parse(String, IFormatProvider) |
指定したカルチャ固有の形式の数値の文字列形式を、等価の倍精度浮動小数点数に変換します。 |
| Parse(String, NumberStyles, IFormatProvider) |
指定したスタイルおよびカルチャ固有の形式の数値の文字列形式を、等価の倍精度浮動小数点数に変換します。 |
| Parse(String, NumberStyles) |
指定したスタイルの数値の文字列形式を、等価の倍精度浮動小数点数に変換します。 |
| Parse(String) |
数値の文字列形式を、等価の倍精度浮動小数点数に変換します。 |
| Pow(Double, Double) |
特定の累乗に引き上げられた値を計算します。 |
| RadiansToDegrees(Double) |
指定した値をラジアンから度に変換します。 |
| ReciprocalEstimate(Double) |
値の逆数の推定値を計算します。 |
| ReciprocalSqrtEstimate(Double) |
値の逆平方根の推定値を計算します。 |
| RootN(Double, Int32) |
値の n 番目のルートを計算します。 |
| Round(Double, Int32, MidpointRounding) |
既定の丸めモード (ToEven) を使用して、指定した桁数の小数部に値を丸めます。 |
| Round(Double, Int32) |
既定の丸めモード (ToEven) を使用して、指定した桁数の小数部に値を丸めます。 |
| Round(Double, MidpointRounding) |
指定した丸めモードを使用して、値を最も近い整数に丸めます。 |
| Round(Double) |
既定の丸めモード (ToEven) を使用して、値を最も近い整数に丸めます。 |
| ScaleB(Double, Int32) |
指定した累乗に引き上げられた値とその基数の積を計算します。 |
| Sign(Double) |
値の符号を計算します。 |
| Sin(Double) |
値のサインを計算します。 |
| SinCos(Double) |
値のサインとコサインを計算します。 |
| SinCosPi(Double) |
値のサインとコサインを計算します。 |
| Sinh(Double) |
値の双曲線サインを計算します。 |
| SinPi(Double) |
|
| Sqrt(Double) |
値の平方根を計算します。 |
| Tan(Double) |
値のタンジェントを計算します。 |
| Tanh(Double) |
値の双曲線正接を計算します。 |
| TanPi(Double) |
|
| ToString() |
このインスタンスの数値を等価の文字列形式に変換します。 |
| ToString(IFormatProvider) |
指定したカルチャ固有の書式情報を使用して、このインスタンスの数値を等価の文字列形式に変換します。 |
| ToString(String, IFormatProvider) |
指定した書式とカルチャ固有の書式情報を使用して、このインスタンスの数値を等価の文字列形式に変換します。 |
| ToString(String) |
指定した形式を使用して、このインスタンスの数値を等価の文字列形式に変換します。 |
| Truncate(Double) |
値を切り捨てます。 |
| TryFormat(Span<Byte>, Int32, ReadOnlySpan<Char>, IFormatProvider) |
現在のインスタンスの値を UTF-8 として指定されたバイトスパンに書式設定しようとします。 |
| TryFormat(Span<Char>, Int32, ReadOnlySpan<Char>, IFormatProvider) |
現在の double インスタンスの値を、指定された文字スパンに書式設定しようとします。 |
| TryParse(ReadOnlySpan<Byte>, Double) |
数値の文字列形式を含む UTF-8 文字スパンを、等価の倍精度浮動小数点数に変換しようとします。 |
| TryParse(ReadOnlySpan<Byte>, IFormatProvider, Double) |
UTF-8 文字のスパンを値に解析しようとします。 |
| TryParse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider, Double) |
UTF-8 文字のスパンを値に解析しようとします。 |
| TryParse(ReadOnlySpan<Char>, Double) |
指定したスタイルおよびカルチャ固有の形式の数値のスパン表現を、等価の倍精度浮動小数点数に変換します。 戻り値は、変換が成功したか失敗したかを示します。 |
| TryParse(ReadOnlySpan<Char>, IFormatProvider, Double) |
文字のスパンを値に解析しようとします。 |
| TryParse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider, Double) |
指定したスタイルおよびカルチャ固有の形式の数値の文字列形式を含む文字スパンを、等価の倍精度浮動小数点数に変換します。 戻り値は、変換が成功したか失敗したかを示します。 |
| TryParse(String, Double) |
数値の文字列形式を、等価の倍精度浮動小数点数に変換します。 戻り値は、変換が成功したか失敗したかを示します。 |
| TryParse(String, IFormatProvider, Double) |
文字列を値に解析しようとします。 |
| TryParse(String, NumberStyles, IFormatProvider, Double) |
指定したスタイルおよびカルチャ固有の形式の数値の文字列形式を、等価の倍精度浮動小数点数に変換します。 戻り値は、変換が成功したか失敗したかを示します。 |
演算子
| 名前 | 説明 |
|---|---|
| Equality(Double, Double) |
指定した 2 つの Double 値が等しいかどうかを示す値を返します。 |
| GreaterThan(Double, Double) | |
| GreaterThanOrEqual(Double, Double) | |
| Inequality(Double, Double) |
指定した 2 つの Double 値が等しくないかどうかを示す値を返します。 |
| LessThan(Double, Double) | |
| LessThanOrEqual(Double, Double) |
明示的なインターフェイスの実装
適用対象
スレッド セーフ
この型のすべてのメンバーはスレッド セーフです。 インスタンスの状態を変更するように見えるメンバーは、実際には新しい値で初期化された新しいインスタンスを返します。 他の型と同様に、この型のインスタンスを含む共有変数の読み取りと書き込みは、スレッド セーフを保証するためにロックによって保護する必要があります。