.NET 中的数字Numerics in .NET

.NET 提供一系列数值整型和浮点型基元、System.Numerics.BigInteger(没有理论上限或下限的整型类型)、System.Numerics.Complex(表示复杂数字)和 System.Numerics 命名空间中一组启用了 SIMD 的类型。.NET provides a range of numeric integer and floating-point primitives, as well as System.Numerics.BigInteger, which is an integral type with no theoretical upper or lower bound, System.Numerics.Complex, which represents complex numbers, and a set of SIMD-enabled types in the System.Numerics namespace.

整数类型Integer types

.NET 支持带有符号和无符号的 8 位、16 位、32 位和 64 位整数类型,如下表所示:.NET supports both signed and unsigned 8-, 16-, 32-, and 64-bit integer types, which are listed in the following table:

类型Type 有符号/无符号Signed/Unsigned 大小(以字节为单位)Size (in bytes) 最小值Minimum value 最大值Maximum value
System.Byte 无符号Unsigned 11 00 255255
System.Int16 有符号Signed 22 -32,768-32,768 32,76732,767
System.Int32 有符号Signed 44 -2,147,483,648-2,147,483,648 2,147,483,6472,147,483,647
System.Int64 有符号Signed 88 -9,223,372,036,854,775,808-9,223,372,036,854,775,808 9,223,372,036,854,775,8079,223,372,036,854,775,807
System.SByte 有符号Signed 11 -128-128 127127
System.UInt16 无符号Unsigned 22 00 65,53565,535
System.UInt32 无符号Unsigned 44 00 4,294,967,2954,294,967,295
System.UInt64 无符号Unsigned 88 00 18,446,744,073,709,551,61518,446,744,073,709,551,615

每个整数类型都支持一组标准算术运算符。Each integer type supports a set of standard arithmetic operators. System.Math 类为更广泛的数学函数集提供方法。The System.Math class provides methods for a broader set of mathematical functions.

还可以使用 System.BitConverter 类对整数值中的单个位进行运算。You can also work with the individual bits in an integer value by using the System.BitConverter class.

备注

无符号整数类型不符合 CLS。The unsigned integer types are not CLS-compliant. 有关详细信息,请参阅 Language Independence and Language-Independent ComponentsFor more information, see Language Independence and Language-Independent Components.

BigIntegerBigInteger

System.Numerics.BigInteger 结构是不可变类型,表示其值没有理论上限或下限的任意大型整数。The System.Numerics.BigInteger structure is an immutable type that represents an arbitrarily large integer whose value in theory has no upper or lower bounds. BigInteger 类型的方法几乎与其他整数类型的方法一致。The methods of the BigInteger type closely parallel those of the other integral types.

浮点类型Floating-point types

.NET 包括三个基元浮点类型,如下表所列:.NET includes three primitive floating-point types, which are listed in the following table:

类型Type 大小(以字节为单位)Size (in bytes) 大致范围Approximate range 精度Precision
System.Single 44 ±1.5 x 10−45 至 ±3.4 x 1038±1.5 x 10−45 to ±3.4 x 1038 大约 6-9 位数字~6-9 digits
System.Double 88 ±5.0 × 10−324 到 ±1.7 × 10308±5.0 × 10−324 to ±1.7 × 10308 大约 15-17 位数字~15-17 digits
System.Decimal 1616 ±1.0 x 10-28 至 ±7.9228 x 1028±1.0 x 10-28 to ±7.9228 x 1028 28-29 位28-29 digits

SingleDouble 类型都支持表示非数字和无穷大的特殊值。Both Single and Double types support special values that represent not-a-number and infinity. 例如,Double 类型提供以下值:Double.NaNDouble.NegativeInfinityDouble.PositiveInfinityFor example, the Double type provides the following values: Double.NaN, Double.NegativeInfinity, and Double.PositiveInfinity. 可以使用 Double.IsNaNDouble.IsInfinityDouble.IsPositiveInfinityDouble.IsNegativeInfinity 方法来测试这些特殊值。You use the Double.IsNaN, Double.IsInfinity, Double.IsPositiveInfinity, and Double.IsNegativeInfinity methods to test for these special values.

每个浮点类型都支持一组标准的算术运算符。Each floating-point type supports a set of standard arithmetic operators. System.Math 类为更广泛的数学函数集提供方法。The System.Math class provides methods for a broader set of mathematical functions. .NET Core 2.0 及更高版本包含 System.MathF 类,该类提供接受 Single 类型的参数的方法。.NET Core 2.0 and later includes the System.MathF class that provides methods which accept arguments of the Single type.

还可以使用 System.BitConverter 类对 DoubleSingle 值中的单个位进行运算。You can also work with the individual bits in Double and Single values by using the System.BitConverter class. System.Decimal 结构具有自己处理十进制值单个位的方法(Decimal.GetBitsDecimal.Decimal(Int32[]))以及一套执行其他数学运算的方法。The System.Decimal structure has its own methods, Decimal.GetBits and Decimal.Decimal(Int32[]), for working with a decimal value's individual bits, as well as its own set of methods for performing some additional mathematical operations.

DoubleSingle 类型旨在用于本质上不精确的值(例如,两颗行星之间的距离)和无需高度精确和舍入误差小的应用程序。The Double and Single types are intended to be used for values that by their nature are imprecise (for example, the distance between two stars) and for applications in which a high degree of precision and small rounding error is not required. 在需要较高准确度和尽量减小舍入误差的情况下,应使用 System.Decimal 类型。You should use the System.Decimal type for cases in which greater precision is required and rounding errors should be minimized.

备注

Decimal 类型不会消除对舍入的要求。The Decimal type doesn't eliminate the need for rounding. 相反,它最大限度地减少了因舍入而导致的错误。Rather, it minimizes errors due to rounding.

ComplexComplex

System.Numerics.Complex 结构表示复数,即带实数部分和虚数部分的数字。The System.Numerics.Complex structure represents a complex number, that is, a number with a real number part and an imaginary number part. 此类型支持一套标准的算术、比较、相等、显式和隐式转换运算符,以及数学、代数和三角方法。It supports a standard set of arithmetic, comparison, equality, explicit and implicit conversion operators, as well as mathematical, algebraic, and trigonometric methods.

启用了 SIMD 的类型SIMD-enabled types

System.Numerics 命名空间包含一组启用了 .NET SIMD 的类型。The System.Numerics namespace includes a set of .NET SIMD-enabled types. SIMD (Single Instruction Multiple Data) 操作可以在硬件级别并行化。SIMD (Single Instruction Multiple Data) operations can be parallelized at the hardware level. 这可以增加向量化计算的吞吐量,这在数学、科学和图形应用中很常见。That increases the throughput of the vectorized computations, which are common in mathematical, scientific, and graphics apps.

启用了 .NET SIMD 的类型如下:The .NET SIMD-enabled types include the following:

  • Vector2Vector3Vector4 类型,用于表示具有 2、3 和 4 Single 值的向量。The Vector2, Vector3, and Vector4 types, which represent vectors with 2, 3, and 4 Single values.

  • 两个矩阵类型:Matrix3x2(表示 3x2 矩阵)和 Matrix4x4(表示 4x4 矩阵)。Two matrix types, Matrix3x2, which represents a 3x2 matrix, and Matrix4x4, which represents a 4x4 matrix.

  • Plane 类型,表示三维空间中的一个平面。The Plane type, which represents a plane in three-dimensional space.

  • Quaternion 类型,表示一个用于对三维物理旋转进行编码的向量。The Quaternion type, which represents a vector that is used to encode three-dimensional physical rotations.

  • Vector<T> 类型,表示指定数字类型的向量,并提供受益于 SIMD 支持的一组广泛的运算符。The Vector<T> type, which represents a vector of a specified numeric type and provides a broad set of operators that benefit from SIMD support. Vector<T> 实例的计数是固定的,但其值 Vector<T>.Count 取决于执行代码的计算机的 CPU。The count of a Vector<T> instance is fixed, but its value Vector<T>.Count depends on the CPU of the machine, on which code is executed.

    备注

    Vector<T> 类型未包含在 .NET Framework 中。The Vector<T> type is not included into the .NET Framework. 必须安装 System.Numerics.Vectors NuGet 包才能访问此类型。You must install the System.Numerics.Vectors NuGet package to get access to this type.

启用了 SIMD 的类型以这样一种方式实现:即它们可以与未启用 SIMD 的硬件或 JIT 编译器一起使用。The SIMD-enabled types are implemented in such a way that they can be used with non-SIMD-enabled hardware or JIT compilers. 要利用 SIMD 指令,你的 64 位应用必须由使用 RyuJIT 编译器的运行时运行,该编译器包含在 .NET Core 和 .NET Framework 4.6 及更高版本中。To take advantage of SIMD instructions, your 64-bit apps must be run by the runtime that uses the RyuJIT compiler, which is included in .NET Core and in the .NET Framework 4.6 and later versions. 它针对 64 位处理器增加了 SIMD 支持。It adds SIMD support when targeting 64-bit processors.

请参阅See also