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, IConvertible, IFormattable
public value class float : IComparable, IComparable<float>, IEquatable<float>, IFormattable
public struct Single : IComparable, IComparable<float>, IConvertible, IEquatable<float>, IFormattable
public struct Single : IComparable, IComparable<float>, IConvertible, IEquatable<float>, ISpanFormattable
[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
[<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 IComparable, IConvertible, IFormattable
Public Structure Single
Implements IComparable, IComparable(Of Single), IEquatable(Of Single), IFormattable
- 继承
- 属性
- 实现
注解
Single值类型表示单精度32位数字,其值范围从负 3.402823 e 38 到正 3.402823 e 38,以及正或负零、 PositiveInfinity 、 NegativeInfinity ,而非数字 (NaN) 。 它用于表示极大型 (的值,例如行星或 galaxies 之间的距离) 或极小的 (,例如以) 千克为间隔的物质的分子质量,这通常是不精确的 ((如从地球到另一阳历系统) 的距离。 Single类型符合二元浮点算法的 IEC 60559:1989 (IEEE 754) 标准。
本文包含以下各节:
System.Single 提供一些方法,用于比较此类型的实例,将实例的值转换为其字符串表示形式,并将数字的字符串表示形式转换为此类型的实例。 有关格式规范代码如何控制值类型的字符串表示形式的信息,请参阅 格式设置类型、 标准数字格式字符串和 自定义数字格式字符串。
浮点表示形式和精度
Single数据类型以32位二进制格式存储单精度浮点值,如下表所示:
组成部分 | Bits |
---|---|
有效位数或尾数 | 0-22 |
指数 | 23-30 |
Sign (0 = 正,1 = 负值) | 31 |
正如小数部分无法精确表示某些小数值 (例如1/3 或 Math.PI) ),二进制小数无法表示某些小数值。 例如,2/10 以 .2 的形式精确表示为小数部分,以二进制小数0011111001001100表示,其模式为 "1100" 重复到无限大。 在这种情况下,浮点值提供它所表示的数字的不精确表示形式。 对原始浮点值执行其他数学运算通常会增加其精度。 例如,如果比较 .3 乘以10的结果并将 .3 添加到 3 9 次,则您将看到加法产生的结果不太精确,因为它涉及到与乘法的8个以上的操作。 请注意,仅当 Single 使用 "R" 标准数字格式字符串显示这两个值时,此差异才明显,如有必要,将显示该类型支持的所有9位精度 Single 。
using System;
public class Example
{
public static void Main()
{
Single value = .2f;
Single result1 = value * 10f;
Single result2 = 0f;
for (int ctr = 1; ctr <= 10; ctr++)
result2 += value;
Console.WriteLine(".2 * 10: {0:R}", result1);
Console.WriteLine(".2 Added 10 times: {0:R}", result2);
}
}
// The example displays the following output:
// .2 * 10: 2
// .2 Added 10 times: 2.00000024
Module Example
Public Sub Main()
Dim value As Single = .2
Dim result1 As Single = value * 10
Dim result2 As Single
For ctr As Integer = 1 To 10
result2 += value
Next
Console.WriteLine(".2 * 10: {0:R}", result1)
Console.WriteLine(".2 Added 10 times: {0:R}", result2)
End Sub
End Module
' The example displays the following output:
' .2 * 10: 2
' .2 Added 10 times: 2.00000024
由于某些数字不能精确表示为小数部分的二进制值,因此浮点数只能近似于实数。
所有浮点数都具有有限数量的有效数字,还决定了浮点值接近于实数的准确程度。 Single值最多可有7个小数位数的精度,尽管它在内部维护最多9位数字。 这意味着,某些浮点运算可能缺乏更改浮点值的精度。 下面的示例定义了一个大的单精度浮点值,然后在其中添加了的积 Single.Epsilon 和一个千万亿。 但是,该产品太小,无法修改原始浮点值。 它的最小有效位数为千分之几,而产品中最重要的位是 10-30。
using System;
public class Example
{
public static void Main()
{
Single value = 123.456f;
Single additional = Single.Epsilon * 1e15f;
Console.WriteLine($"{value} + {additional} = {value + additional}");
}
}
// The example displays the following output:
// 123.456 + 1.401298E-30 = 123.456
Module Example
Public Sub Main()
Dim value As Single = 123.456
Dim additional As Single = Single.Epsilon * 1e15
Console.WriteLine($"{value} + {additional} = {value + additional}")
End Sub
End Module
' The example displays the following output:
' 123.456 + 1.401298E-30 = 123.456
浮点数的有限精度有几个后果:
对于特定精度,看起来相等的两个浮点数在进行比较时可能不相等,因为其最小有效位不同。 在下面的示例中,将一系列数字添加在一起,并将其总计与预期的总数进行比较。 尽管这两个值看起来是相同的,但对方法的调用
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).
Module Example Public Sub Main() Dim values() As Single = { 10.01, 2.88, 2.88, 2.88, 9.0 } Dim result As Single = 27.65 Dim total As Single For Each value In values total += value Next If total.Equals(result) Then Console.WriteLine("The sum of the values equals the total.") Else Console.WriteLine("The sum of the values ({0}) does not equal the total ({1}).", total, result) End If End Sub End Module ' The example displays the following output: ' The sum of the values (27.65) does not equal the total (27.65). ' ' If the index items in the Console.WriteLine statement are changed to {0:R}, ' the example displays the following output: ' The sum of the values (27.639999999999997) does not equal the total (27.64).
如果将语句中的格式项 Console.WriteLine(String, Object, Object) 从和更改
{0}
{1}
为{0:R}
,并{1:R}
显示这两个值的所有有效位,则可以 Single 清楚地表明这两个值不相等,因为在加法运算期间会丢失精度。 在这种情况下,可以通过调用方法来解决该问题, Math.Round(Double, Int32) 以便在 Single 执行比较之前将值舍入到所需的精度。如果使用的是十进制数,则使用浮点数的算术或比较运算可能不会产生相同的结果,因为二进制浮点数可能不等于十进制数。 前面的示例通过显示乘以 .3 乘以10的结果并将 .3 添加到 3 9 次,阐释了这一点。
当包含小数值的数值操作的准确性非常重要时,请使用 Decimal 类型而不是 Single 类型。 当具有整数值超出或类型范围的数值操作的准确性 Int64 UInt64 很重要时,请使用 BigInteger 类型。
如果涉及浮点数,值可能不会往返。 如果某个操作将原始浮点数转换为另一种格式,则会将值转换为舍入,而反向运算会将转换后的窗体转换回浮点数,最终浮点数等于原始浮点数。 往返过程可能会失败,因为在转换过程中一个或多个最小有效位会丢失或更改。 在下面的示例中, 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
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
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 因为精度存在差异。 在下面的示例中,将相同除法运算的结果分配给一个 Double 值和一个 Single 值。 在将 Single 值强制转换为之后 Double ,这两个值的比较表明它们不相等。
using System; public class Example { public static void Main() { Double value1 = 1/3.0; Single sValue2 = 1/3.0f; Double value2 = (Double) sValue2; Console.WriteLine("{0:R} = {1:R}: {2}", value1, value2, value1.Equals(value2)); } } // The example displays the following output: // 0.33333333333333331 = 0.3333333432674408: False
Module Example Public Sub Main() Dim value1 As Double = 1/3 Dim sValue2 As Single = 1/3 Dim value2 As Double = CDbl(sValue2) Console.WriteLine("{0} = {1}: {2}", value1, value2, value1.Equals(value2)) End Sub End Module ' The example displays the following output: ' 0.33333333333333331 = 0.3333333432674408: False
若要避免此问题,请使用 Double 数据类型来代替 Single 数据类型,或使用 Round 方法,使这两个值具有相同的精度。
测试是否相等
若要将视为相等,两个 Single 值必须表示相同的值。 不过,由于值之间的精度差异,或由于一个或两个值的精度损失,应相同的浮点值通常会因其最小有效位之间的差异而不相等。 因此,调用 Equals 方法来确定两个值是否相等,或调用 CompareTo 方法来确定两个值之间的关系 Single ,通常会产生意外结果。 这在下面的示例中很明显,其中两个明显相等 Single 的值是不相等的,因为第一个值的精度为7位,而第二个值为9。
using System;
public class Example
{
public static void Main()
{
float value1 = .3333333f;
float value2 = 1.0f/3;
Console.WriteLine("{0:R} = {1:R}: {2}", value1, value2, value1.Equals(value2));
}
}
// The example displays the following output:
// 0.3333333 = 0.333333343: False
Module Example
Public Sub Main()
Dim value1 As Single = .3333333
Dim value2 As Single = 1/3
Console.WriteLine("{0:R} = {1:R}: {2}", value1, value2, value1.Equals(value2))
End Sub
End Module
' The example displays the following output:
' 0.3333333 = 0.333333343: False
采用不同的代码路径并以不同的方式操作的计算值通常证明不相等。 在下面的示例中,一个 Single 值为平方,然后计算平方根以还原原始值。 第二个 Single 比3.51 和 squared 相乘,然后将结果的平方根除以3.51 来还原原始值。 尽管两个值看起来相同,但对方法的调用 Equals(Single) 指示它们不相等。 使用 "G9" 标准格式字符串返回显示每个值的所有有效位的结果字符串表明, Single 第二个值 .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
Module Example
Public Sub Main()
Dim value1 As Single = 10.201438
value1 = CSng(Math.Sqrt(CSng(Math.Pow(value1, 2))))
Dim value2 As Single = CSng(Math.Pow(value1 * CSng(3.51), 2))
value2 = CSng(Math.Sqrt(value2) / CSng(3.51))
Console.WriteLine("{0} = {1}: {2}",
value1, value2, value1.Equals(value2))
Console.WriteLine()
Console.WriteLine("{0:G9} = {1:G9}", value1, value2)
End Sub
End Module
' The example displays the following output:
' 10.20144 = 10.20144: False
'
' 10.201438 = 10.2014389
如果精度损失可能会影响比较结果,则可以使用以下技术,而不是调用 Equals 或 CompareTo 方法:
调用 Math.Round 方法以确保这两个值具有相同的精度。 下面的示例修改了上一个示例,以使用此方法,以使两个小数值相等。
using System; public class Example { public static void Main() { float value1 = .3333333f; float value2 = 1.0f/3; int precision = 7; value1 = (float) Math.Round(value1, precision); value2 = (float) Math.Round(value2, precision); Console.WriteLine("{0:R} = {1:R}: {2}", value1, value2, value1.Equals(value2)); } } // The example displays the following output: // 0.3333333 = 0.3333333: True
Module Example Public Sub Main() Dim value1 As Single = .3333333 Dim value2 As Single = 1/3 Dim precision As Integer = 7 value1 = CSng(Math.Round(value1, precision)) value2 = CSng(Math.Round(value2, precision)) Console.WriteLine("{0:R} = {1:R}: {2}", value1, value2, value1.Equals(value2)) End Sub End Module ' The example displays the following output: ' 0.3333333 = 0.3333333: True
精度问题仍适用于中点值的舍入。 有关更多信息,请参见 Math.Round(Double, Int32, MidpointRounding) 方法。
测试近似相等性,而不是相等。 此方法要求您定义一个绝对量,而这两个值可能会不同,但仍相等,或者您定义较小值与较大值之间的差异。
警告
Single.Epsilon 在测试相等性时,有时会将其用作两个值之间的距离的绝对度量值 Single 。 但 Single.Epsilon 度量值为零的可添加到或从中减去的最小可能值 Single 。 对于大多数正值和负值 Single ,值 Single.Epsilon 太小,无法检测到。 因此,除了零值以外,不建议在测试中使用它是否相等。
下面的示例使用后一种方法来定义
IsApproximatelyEqual
测试两个值之间的相对差异的方法。 它还比较了对方法的调用和方法的调用结果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
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
浮点值和异常
与整数类型的操作不同,具有浮点值的操作不会引发异常,这种操作在非法操作(如被零除或溢出)的情况下会引发异常。 相反,在这些情况下,浮点运算的结果为零、正无穷、负无穷或不是 (NaN) 的数字:
如果浮点运算的结果对于目标格式来说太小,则结果为零。 当两个非常小的浮点数相乘时,可能会出现这种情况,如下面的示例所示。
using System; public class Example { public static void Main() { float value1 = 1.163287e-36f; float value2 = 9.164234e-25f; float result = value1 * value2; Console.WriteLine("{0} * {1} = {2}", value1, value2, result); Console.WriteLine("{0} = 0: {1}", result, result.Equals(0.0f)); } } // The example displays the following output: // 1.163287E-36 * 9.164234E-25 = 0 // 0 = 0: True
Module Example Public Sub Main() Dim value1 As Single = 1.163287e-36 Dim value2 As Single = 9.164234e-25 Dim result As Single = value1 * value2 Console.WriteLine("{0} * {1} = {2:R}", value1, value2, result) Console.WriteLine("{0} = 0: {1}", result, result.Equals(0)) End Sub End Module ' The example displays the following output: ' 1.163287E-36 * 9.164234E-25 = 0 ' 0 = 0: True
如果浮点运算结果的大小超出目标格式的范围,则操作的结果为 PositiveInfinity 或 NegativeInfinity (适用于结果的符号)。 溢出的操作的结果为 Single.MaxValue PositiveInfinity ,而溢出的操作的结果为 Single.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
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 以下操作的结果:
除数为零的除以零。 请注意,除数为零的其他情况会导致 PositiveInfinity 或 NegativeInfinity 。
具有无效输入的任何浮点运算。 例如,如果尝试查找负值的平方根,则返回 NaN 。
具有值为的参数的任何操作 Single.NaN 。
类型转换和单结构
该 Single 结构不定义任何显式或隐式转换运算符; 相反,转换是由编译器实现的。
下表列出了其他基元数值类型的值可能转换为值的情况 Single ,它还指示转换是扩大还是收缩转换,以及所生成的精度是否低于 Single 原始值。
从 | 扩大/收缩 | 可能丢失精度 |
---|---|---|
Byte | Widening | 否 |
Decimal | Widening 请注意,C# 需要强制转换运算符。 |
是的。 Decimal 支持 29 位十进制精度数字; Single 支持 9。 |
Double | 收缩;范围外的值将转换为 Double.NegativeInfinity 或 Double.PositiveInfinity 。 | 是的。 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)
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.NaN 、 和 Single.PositiveInfinity Single.NegativeInfinity 。
请注意,将某些数值类型的值转换为 Single 值可能涉及精度损失。 如示例所示,将 、 和 值转换为 值时,可能会 Decimal Double Int32 Int64 UInt32 UInt64 丢失 Single 精度。
值到 Single 的转换 Double 是扩大转换。 如果类型没有值的精确表示形式,则转换可能会导致精度 Double Single 损失。
将值转换为除 外的任何基元数值数据类型的值是收缩转换,并且需要在 Single C#) 中转换运算符 (或 Visual Basic) 中的转换 (Double 方法。 超出目标数据类型范围的值(由目标类型的 和 属性定义)的行为 MinValue
MaxValue
如下表所示。
目标类型 | 结果 |
---|---|
任何整型类型 | 如果在 OverflowException 选中的上下文中发生转换,则会出现异常。 如果在未选中的上下文中进行转换 (C#) 中的默认值,则转换操作成功,但值溢出。 |
Decimal | 异常 OverflowException : |
此外, 、 和 引发 ,以在选中的上下文中转换为整数,但在未选中的上下文中转换为整数时, Single.NaN Single.PositiveInfinity Single.NegativeInfinity OverflowException 这些值溢出。 对于到 的 Decimal 转换,它们始终引发 OverflowException 。 对于到 的 Double 转换,它们分别转换为 Double.NaN 、 Double.PositiveInfinity 和 Double.NegativeInfinity 。
请注意,将值转换为另一种数值类型可能会导致精度 Single 损失。 在转换非整型值的情况下,如示例中的输出所示,当值舍入为 ((如 Visual Basic) 中)或截断 ((如 C#) 中)时,小数 Single Single 部分将丢失。 对于转换为 Decimal 值, Single 该值在目标数据类型中可能没有精确的表示形式。
下面的示例将多个值 Single 转换为其他几种数值类型。 由于使用 checked 关键字,转换Visual Basic (默认) C# (中的已选中上下文中) 。 该示例的输出显示检查了未检查上下文的 中转换的结果。 可以通过使用编译器开关进行编译Visual Basic C# 中通过注释掉 语句,在未选中的上下文中执行 /removeintchecks+
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)
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 方法来确定两个值是否相等,也可以调用 方法 Single CompareTo 来确定两个值之间的关系。
结构 Single 还支持一组完整的比较运算符。 例如,可以测试相等性或不相等性,或确定一个值是否大于或等于另一个值。 如果其中一个操作数是 Double ,则 Single 值将转换为 , Double 然后再执行比较。 如果其中一个操作数是整型类型,则在执行比较 Single 之前将其转换为 。 尽管这些转换是扩大转换,但可能涉及精度损失。
还可以调用 IsNaN IsInfinity 、、 IsPositiveInfinity 和 IsNegativeInfinity 方法来测试这些特殊值。
数学运算。 常见算术运算(如加法、减法、乘法和除法)由语言编译器和公共中间语言 (CIL) 指令而不是方法 Single 实现。 如果数学运算中的另一个操作数是 ,则 在执行该操作之前, 将转换为 ,并且操作结果 Double Single Double 也是 Double 一个值。 如果另一个操作数是整型类型,则在执行该操作之前将其转换为 ,并且操作结果 Single 也是 Single 一个值。
可以通过在 类的 (方法Visual Basic)
static
Shared
其他数学 System.Math 运算。 其中包括通常用于算术运算的其他方法 (如 、 和) 、geometry ((如 和) )以及微 ((如 Math.Abs Math.Sign Math.Sqrt Math.Cos Math.Sin Math.Log) )。 在所有情况下, Single 该值都转换为 Double 。还可以操作值中的单个 Single 位。 方法 BitConverter.GetBytes(Single) 在字节数组中返回其位模式。 通过向 方法传递该字节数组,还可以在 32 位整数中保留 BitConverter.ToInt32 Single 值的位模式。
舍入。 舍入通常用作一种技术,用于减少浮点表示和精度问题导致的值之间的差异的影响。 可以通过调用 Single 方法舍入 Math.Round 值。 但是,请注意,在调用 方法之前,该值将转换为 , Single Double 并且转换可能涉及精度损失。
设置 格式。 可以通过调用 方法或复合格式设置功能将值转换为 Single ToString 其 字符串表示 形式。 有关格式字符串如何控制浮点值的字符串表示形式的信息,请参阅标准 数字 格式字符串和 自定义数字格式字符串 主题。
分析字符串。 可以通过调用 或 方法,将浮点值的字符串表示形式 Single 转换为 Parse TryParse 值。 如果分析操作失败, Parse 该方法将引发异常,而 TryParse 方法返回
false
。类型转换。 Single结构为 接口提供显式接口实现,该接口支持在任意两个标准数据类型.NET Framework IConvertible 转换。 语言编译器还支持将 值隐式转换为所有其他标准数值类型,但 转换为 Double Single 值除外。 将 除 外的任何标准数值类型的值转换为 是扩大转换,不需要使用强制转换运算符 Double Single 或转换方法。
但是,32 位和 64 位整数值的转换可能涉及精度损失。 下表列出了 32 位、64 位和类型的精度 Double 差异:
类型 以十进制 (表示的最大精度) 内部精度 (小数位数) Double 15 17 Int32 和 UInt32 10 10 Int64 和 UInt64 19 19 Single 7 9 精确的问题会影响 Single 转换为值的值 Double 。 在下面的示例中,两个由相同除法运算生成的值是不相等的,因为其中一个值是转换为的单精度浮点值 Double 。
using System; public class Example { public static void Main() { Double value1 = 1/3.0; Single sValue2 = 1/3.0f; Double value2 = (Double) sValue2; Console.WriteLine("{0:R} = {1:R}: {2}", value1, value2, value1.Equals(value2)); } } // The example displays the following output: // 0.33333333333333331 = 0.3333333432674408: False
Module Example Public Sub Main() Dim value1 As Double = 1/3 Dim sValue2 As Single = 1/3 Dim value2 As Double = CDbl(sValue2) Console.WriteLine("{0} = {1}: {2}", value1, value2, value1.Equals(value2)) End Sub End Module ' The example displays the following output: ' 0.33333333333333331 = 0.3333333432674408: False
字段
Epsilon |
表示大于零的最小正 Single 值。 此字段为常数。 |
MaxValue |
表示 Single 的最大可能值。 此字段为常数。 |
MinValue |
表示 Single 的最小可能值。 此字段为常数。 |
NaN |
表示非数字 ( |
NegativeInfinity |
表示负无穷。 此字段为常数。 |
PositiveInfinity |
表示正无穷。 此字段为常数。 |
方法
CompareTo(Object) |
将此实例与指定对象进行比较,并返回一个整数,该整数指示此实例的值是小于、等于还是大于指定对象的值。 |
CompareTo(Single) |
将此实例与指定的单精度浮点数进行比较,并返回一个整数,该整数指示此实例的值是小于、等于还是大于指定单精度浮点数的值。 |
Equals(Object) |
返回一个值,该值指示此实例是否等于指定的对象。 |
Equals(Single) |
返回一个值,该值指示此实例和指定的 Single 对象是否表示相同的值。 |
GetHashCode() |
返回此实例的哈希代码。 |
GetTypeCode() | |
IsFinite(Single) |
确定指定值是否为有限(零、次正规数或正规数)。 |
IsInfinity(Single) |
返回一个值,该值指示指定数字是计算为负无穷大还是正无穷大。 |
IsNaN(Single) |
返回一个值,该值指示指定的值是否不为数字 (NaN)。 |
IsNegative(Single) |
确定指定值是否为负值。 |
IsNegativeInfinity(Single) |
返回一个值,通过该值指示指定数字是否计算为负无穷大。 |
IsNormal(Single) |
确定指定值是否为正规数。 |
IsPositiveInfinity(Single) |
返回一个值,通过该值指示指定数字是否计算为正无穷大。 |
IsSubnormal(Single) |
确定指定值是否是次正规数。 |
Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider) |
将字符范围(其中包含指定样式和区域性特定格式的数字的字符串表示形式)转换为它的等效单精度浮点数。 |
Parse(String) |
将数字的字符串表示形式转换为它的等效单精度浮点数。 |
Parse(String, IFormatProvider) |
将具有指定区域性特定格式的数字的字符串表示形式转换为它的等效单精度浮点数。 |
Parse(String, NumberStyles) |
将具有指定样式的数字的字符串表示形式转换为它的等效单精度浮点数。 |
Parse(String, NumberStyles, IFormatProvider) |
将具有指定样式和区域性特定格式的数字的字符串表示形式转换为它的等效单精度浮点数。 |
ToString() |
将此实例的数值转换为其等效的字符串表示形式。 |
ToString(IFormatProvider) |
使用指定的区域性特定格式信息,将此实例的数值转换为它的等效字符串表示形式。 |
ToString(String) |
使用指定的格式,将此实例的数值转换为它的等效字符串表示形式。 |
ToString(String, IFormatProvider) |
使用指定的格式和区域性特定格式信息,将此实例的数值转换为它的等效字符串表示形式。 |
TryFormat(Span<Char>, Int32, ReadOnlySpan<Char>, IFormatProvider) |
尝试将当前浮点数实例的值的格式设置为提供的字符范围。 |
TryParse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider, Single) |
将具有指定样式和区域性特定格式的数字的范围表示形式转换为它的等效单精度浮点数。 一个指示转换是否成功的返回值。 |
TryParse(ReadOnlySpan<Char>, Single) |
将字符范围中的数字的字符串表示形式转换为它的等效单精度浮点数。 一个指示转换是否成功的返回值。 |
TryParse(String, NumberStyles, IFormatProvider, Single) |
将具有指定样式和区域性特定格式的数字的字符串表示形式转换为它的等效单精度浮点数。 一个指示转换是否成功的返回值。 |
TryParse(String, Single) |
将数字的字符串表示形式转换为它的等效单精度浮点数。 一个指示转换是否成功的返回值。 |
运算符
Equality(Single, Single) |
返回一个值,该值指示两个指定的 Single 值是否相等。 |
GreaterThan(Single, Single) | |
GreaterThanOrEqual(Single, Single) | |
Inequality(Single, Single) |
返回一个值,该值指示两个指定的 Single 值是否不相等。 |
LessThan(Single, Single) | |
LessThanOrEqual(Single, Single) |
显式接口实现
适用于
线程安全性
此类型的所有成员都是线程安全的。 看似修改实例状态的成员实际上返回用新值初始化的新实例。 与任何其他类型一样,读取和写入包含此类型的实例的共享变量时,必须通过锁保护以保证线程安全。