Numerics in the .NET Framework

The .NET Framework supports the standard numeric integral and floating-point primitives, as well as BigInteger, an integral type with no theoretical upper or lower bound, Complex, a type that represents complex numbers, and a set of SIMD-enabled vector types in the System.Numerics namespace.

In addition, System.Numerics.Vectors, the SIMD-enabled library of vectory types, was released as a NuGet package.

Integral types

The .NET Framework supports both signed and unsigned integers ranging from one byte to eight bytes in length. The following table lists the integral types and their size, indicates whether they are signed or unsigned, and documents their range. All integers are value types.

Type Signed/Unsigned Size (bytes) Minimum value Maximum Value
System.Byte Unsigned 1 0 255
System.Int16 Signed 2 -32,768 32,767
System.Int32 Signed 4 -2,147,483,648 2,147,483,647
System.Int64 Signed 8 -9,223,372,036,854,775,808 9,223,372,036,854,775,807
System.SByte Signed 1 -128 127
System.UInt16 Unsigned 2 0 65,535
System.UInt32 Unsigned 4 0 4,294,967,295
System.UInt64 Unsigned 8 0 18,446,744,073,709,551,615

Each integral type supports a standard set of arithmetic, comparison, equality, explicit conversion, and implicit conversion operators. Each integer also includes methods to perform equality comparisons and relative comparisons, to convert the string representation of a number to that integer, and to convert an integer to its string representation. Some additional mathematical operations beyond those handled by the standard operators, such as rounding and identifying the smaller or larger value of two integers, are available from the Math class. You can also work with the individual bits in an integer value by using the BitConverter class.

Note that the unsigned integral types are not CLS-compliant. For more information, see Language Independence and Language-Independent Components.

Floating-point types

The .NET Framework includes three primitive floating point types, which are listed in the following table.

Type Size (in bytes) Minimum Maximum
System.Double 8 -1.79769313486232e308 1.79769313486232e308
System.Single 4 -3.402823e38 3.402823e38
System.Decimal 16 -79,228,162,514,264,337,593,543,950,335 79,228,162,514,264,337,593,543,950,335

Each floating-point type supports a standard set of arithmetic, comparison, equality, explicit conversion, and implicit conversion operators. Each also includes methods to perform equality comparisons and relative comparisons, to convert the string representation of a floating-point number, and to convert a floating-point number to its string representation. Some additional mathematical, algebraic, and trigonometric operations are available from the Math class. You can also work with the individual bits in Double and Single values by using the BitConverter class. 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.

The Double and Single types are intended to be used for values that by their nature are imprecise (such as the distance between two stars in the solar system) and for applications in which a high degree of precision and small rounding error is not required. You should use the System.Decimal type for cases in which greater precision is required and rounding error is undesirable,

BigInteger

System.Numerics.BigInteger is an immutable type that represents an arbitrarily large integer whose value in theory has no upper or lower bounds. The methods of the BigInteger type closely parallel those of the other integral types.

Complex

The Complex type 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 conversion, and implicit conversion operators, as well as mathematical, algebraic, and trigonometric methods.

SIMD-enabled vector types

The System.Numerics namespace includes a set of SIMD-enabled vector types for the .NET Framework. SIMD (Single Instruction Multiple Data operations) allows some operations to be parallelized at the hardware level, which results in huge performance improvements in mathematical, scientific, and graphics apps that perform computations over vectors.

The SIMD-enabled vector types in the .NET Framework include the following: . In addition, System.Numerics.Vectors includes a Plane type and a Quaternion type.

The SimD-enabled vector types are implemented in IL, which allows them to be used on non-SimD-enabled hardware and JIT compilers. To take advantage of SIMD instructions, your 64-bit apps must be compiled by the new 64-bit JIT Compiler for managed code, which is included with the .NET Framework 4.6; it adds SIMD support when targeting x64 processors.

SIMD can also be downloaded as a NuGet package. The NuGET package also includes a generic Vector<T> structure that allows you to create a vector of any primitive numeric type. (The primitive numeric types include all numeric types in the System namespace except for Decimal.) In addition, the Vector<T> structure provides a library of convenience methods that you can call when working with vectors.

See Also

Application Essentials