Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/numeric"

From cppreference.com
< cpp
(+constants as a sub-page)
m (Bit manipulation: Add anchor to section name without "since C++20" tag.)
 
(18 intermediate revisions by 9 users not shown)
Line 7: Line 7:
  
 
===[[cpp/numeric/math|Common mathematical functions]]===
 
===[[cpp/numeric/math|Common mathematical functions]]===
 
 
The header {{header|cmath}} provides [[cpp/numeric/math|standard C library mathematical functions]] such as {{lc|std::fabs}}, {{lc|std::sqrt}}, and {{lc|std::sin}}.
 
The header {{header|cmath}} provides [[cpp/numeric/math|standard C library mathematical functions]] such as {{lc|std::fabs}}, {{lc|std::sqrt}}, and {{lc|std::sin}}.
  
===[[cpp/numeric/special_math|Mathematical special functions]] {{mark since c++17}}===
+
===[[cpp/numeric/special_functions|Mathematical special functions]] {{mark since c++17}}===
 
+
 
The header {{header|cmath}} also provides several mathematical special functions such as {{lc|std::beta}}, {{lc|std::hermite}}, and {{lc|std::cyl_bessel_i}}.
 
The header {{header|cmath}} also provides several mathematical special functions such as {{lc|std::beta}}, {{lc|std::hermite}}, and {{lc|std::cyl_bessel_i}}.
  
 
===[[cpp/numeric/constants|Mathematical constants]] {{mark since c++20}}===
 
===[[cpp/numeric/constants|Mathematical constants]] {{mark since c++20}}===
The header {{header|numbers}} provides several mathematical constants, such as {{lc|std::pi}} or {{lc|std::sqrt2}}
+
The header {{header|numbers}} provides several mathematical constants, such as {{lc|std::numbers::pi}} or {{lc|std::numbers::sqrt2}}
  
===Complex number arithmetic===
+
===[[cpp/numeric/linalg|Basic linear algebra algorithms]] {{mark since c++26}}===
 +
The header {{header|linalg}} provides basic linear algebra algorithms which are based on BLAS.
  
 +
===Complex number arithmetic===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc header | complex}}
+
{{dsc header|complex}}
{{dsc tclass | cpp/numeric/complex | a complex number type }}
+
{{dsc tclass|cpp/numeric/complex|a complex number type}}
 
{{dsc end}}
 
{{dsc end}}
  
 
===Numeric arrays===
 
===Numeric arrays===
 
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc header | valarray}}
+
{{dsc header|valarray}}
{{dsc tclass | cpp/numeric/valarray | numeric arrays, array masks and array slices }}
+
{{dsc tclass|cpp/numeric/valarray|numeric arrays, array masks and array slices}}
 
{{dsc end}}
 
{{dsc end}}
  
Line 36: Line 35:
  
 
===Factor operations===
 
===Factor operations===
 
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc header | numeric}}
+
{{dsc header|numeric}}
{{dsc inc | cpp/numeric/dsc gcd}}
+
{{dsc inc|cpp/numeric/dsc gcd}}
{{dsc inc | cpp/numeric/dsc lcm}}
+
{{dsc inc|cpp/numeric/dsc lcm}}
 
{{dsc end}}
 
{{dsc end}}
  
 
===Interpolation operations===
 
===Interpolation operations===
 +
{{dsc begin}}
 +
{{dsc header|numeric}}
 +
{{dsc inc|cpp/numeric/dsc midpoint}}
 +
{{dsc header|cmath}}
 +
{{dsc inc|cpp/numeric/dsc lerp}}
 +
{{dsc end}}
  
 +
===Saturation arithmetic {{mark since c++26}}===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc header | numeric}}
+
{{dsc header|numeric}}
{{dsc inc | cpp/numeric/dsc midpoint}}
+
{{dsc inc|cpp/numeric/dsc add_sat}}
{{dsc header | cmath}}
+
{{dsc inc|cpp/numeric/dsc sub_sat}}
{{dsc inc | cpp/numeric/dsc lerp}}
+
{{dsc inc|cpp/numeric/dsc mul_sat}}
 +
{{dsc inc|cpp/numeric/dsc div_sat}}
 +
{{dsc inc|cpp/numeric/dsc saturate_cast}}
 
{{dsc end}}
 
{{dsc end}}
  
 
===Numeric operations===
 
===Numeric operations===
 
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc header | numeric}}
+
{{dsc header|numeric}}
{{dsc inc | cpp/algorithm/dsc iota}}
+
{{dsc inc|cpp/algorithm/dsc iota}}
{{dsc inc | cpp/algorithm/dsc accumulate}}
+
{{dsc inc|cpp/algorithm/ranges/dsc iota}}
{{dsc inc | cpp/algorithm/dsc reduce}}
+
{{dsc inc|cpp/algorithm/dsc accumulate}}
{{dsc inc | cpp/algorithm/dsc transform_reduce}}
+
<!--{{dsc inc|cpp/algorithm/ranges/dsc accumulate}}-->
{{dsc inc | cpp/algorithm/dsc inner_product}}
+
{{dsc inc|cpp/algorithm/dsc reduce}}
{{dsc inc | cpp/algorithm/dsc adjacent_difference}}
+
<!--{{dsc inc|cpp/algorithm/ranges/dsc reduce}}-->
{{dsc inc | cpp/algorithm/dsc partial_sum}}
+
{{dsc inc|cpp/algorithm/dsc transform_reduce}}
{{dsc inc | cpp/algorithm/dsc inclusive_scan}}
+
<!--{{dsc inc|cpp/algorithm/ranges/dsc transform_reduce}}-->
{{dsc inc | cpp/algorithm/dsc exclusive_scan}}
+
{{dsc inc|cpp/algorithm/dsc inner_product}}
{{dsc inc | cpp/algorithm/dsc transform_inclusive_scan}}
+
<!--{{dsc inc|cpp/algorithm/ranges/dsc inner_product}}-->
{{dsc inc | cpp/algorithm/dsc transform_exclusive_scan}}
+
{{dsc inc|cpp/algorithm/dsc adjacent_difference}}
 +
<!--{{dsc inc|cpp/algorithm/ranges/dsc adjacent_difference}}-->
 +
{{dsc inc|cpp/algorithm/dsc partial_sum}}
 +
<!--{{dsc inc|cpp/algorithm/ranges/dsc partial_sum}}-->
 +
{{dsc inc|cpp/algorithm/dsc inclusive_scan}}
 +
<!--{{dsc inc|cpp/algorithm/ranges/dsc inclusive_scan}}-->
 +
{{dsc inc|cpp/algorithm/dsc exclusive_scan}}
 +
<!--{{dsc inc|cpp/algorithm/ranges/dsc exclusive_scan}}-->
 +
{{dsc inc|cpp/algorithm/dsc transform_inclusive_scan}}
 +
<!--{{dsc inc|cpp/algorithm/ranges/dsc transform_inclusive_scan}}-->
 +
{{dsc inc|cpp/algorithm/dsc transform_exclusive_scan}}
 +
<!--{{dsc inc|cpp/algorithm/ranges/dsc transform_exclusive_scan}}-->
 
{{dsc end}}
 
{{dsc end}}
  
Line 72: Line 89:
  
 
===[[cpp/numeric/random|Pseudo-random number generation]]===
 
===[[cpp/numeric/random|Pseudo-random number generation]]===
 
 
The header {{header|random}} defines [[cpp/numeric/random|pseudo-random number generators and numerical distributions]]. The header {{header|cstdlib}} also includes C-style random number generation via {{lc|std::srand}} and {{lc|std::rand}}.
 
The header {{header|random}} defines [[cpp/numeric/random|pseudo-random number generators and numerical distributions]]. The header {{header|cstdlib}} also includes C-style random number generation via {{lc|std::srand}} and {{lc|std::rand}}.
 
===[[cpp/numeric/ratio|Compile time rational arithmetic]] {{mark since c++11}}===
 
 
The header {{header|ratio}} provides [[cpp/numeric/ratio|types and functions for manipulating and storing compile-time ratios]].
 
  
 
===[[cpp/numeric/fenv|Floating-point environment]] {{mark since c++11}}===
 
===[[cpp/numeric/fenv|Floating-point environment]] {{mark since c++11}}===
 
 
The header {{header|cfenv}} defines [[cpp/numeric/fenv|flags and functions related to exceptional floating-point state]], such as overflow and division by zero.
 
The header {{header|cfenv}} defines [[cpp/numeric/fenv|flags and functions related to exceptional floating-point state]], such as overflow and division by zero.
  
=== Bit manipulation {{mark since c++20}} ===
+
{{anchor|Bit manipulation}}
 +
===Bit manipulation {{mark since c++20}}===
 
The header {{header|bit}} provides several function templates to access, manipulate, and process individual bits and bit sequences.
 
The header {{header|bit}} provides several function templates to access, manipulate, and process individual bits and bit sequences.
  
 
{{dsc begin}}
 
{{dsc begin}}
 
{{dsc header|bit}}
 
{{dsc header|bit}}
{{dsc namespace | std }}
+
{{dsc namespace|std}}
{{dsc inc | cpp/numeric/dsc bit_cast}}
+
{{dsc inc|cpp/numeric/dsc bit_cast}}
{{dsc inc | cpp/numeric/dsc ispow2}}
+
{{dsc inc|cpp/numeric/dsc byteswap}}
{{dsc inc | cpp/numeric/dsc ceil2}}
+
{{dsc inc|cpp/numeric/dsc has_single_bit}}
{{dsc inc | cpp/numeric/dsc floor2}}
+
{{dsc inc|cpp/numeric/dsc bit_ceil}}
{{dsc inc | cpp/numeric/dsc log2p1}}
+
{{dsc inc|cpp/numeric/dsc bit_floor}}
{{dsc inc | cpp/numeric/dsc rotl}}
+
{{dsc inc|cpp/numeric/dsc bit_width}}
{{dsc inc | cpp/numeric/dsc rotr}}
+
{{dsc inc|cpp/numeric/dsc rotl}}
{{dsc inc | cpp/numeric/dsc countl_zero}}
+
{{dsc inc|cpp/numeric/dsc rotr}}
{{dsc inc | cpp/numeric/dsc countl_one}}
+
{{dsc inc|cpp/numeric/dsc countl_zero}}
{{dsc inc | cpp/numeric/dsc countr_zero}}
+
{{dsc inc|cpp/numeric/dsc countl_one}}
{{dsc inc | cpp/numeric/dsc countr_one}}
+
{{dsc inc|cpp/numeric/dsc countr_zero}}
{{dsc inc | cpp/numeric/dsc popcount}}
+
{{dsc inc|cpp/numeric/dsc countr_one}}
 +
{{dsc inc|cpp/numeric/dsc popcount}}
  
{{dsc inc | cpp/types/dsc endian}}
+
{{dsc inc|cpp/types/dsc endian}}
 
{{dsc end}}
 
{{dsc end}}
  
 
===See also===
 
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc see c | c/numeric | Numerics}}
+
{{dsc see c|c/numeric|Numerics|nomono=true}}
 
{{dsc end}}
 
{{dsc end}}
  
 
{{langlinks|ar|de|es|fr|it|ja|pt|ru|zh}}
 
{{langlinks|ar|de|es|fr|it|ja|pt|ru|zh}}

Latest revision as of 12:50, 1 January 2024

 
 
 

The C++ numerics library includes common mathematical functions and types, as well as optimized numeric arrays and support for random number generation.

Contents

[edit] Mathematical functions and types

[edit] Common mathematical functions

The header <cmath> provides standard C library mathematical functions such as std::fabs, std::sqrt, and std::sin.

[edit] Mathematical special functions (since C++17)

The header <cmath> also provides several mathematical special functions such as std::beta, std::hermite, and std::cyl_bessel_i.

[edit] Mathematical constants (since C++20)

The header <numbers> provides several mathematical constants, such as std::numbers::pi or std::numbers::sqrt2

[edit] Basic linear algebra algorithms (since C++26)

The header <linalg> provides basic linear algebra algorithms which are based on BLAS.

[edit] Complex number arithmetic

Defined in header <complex>
a complex number type
(class template)

[edit] Numeric arrays

Defined in header <valarray>
numeric arrays, array masks and array slices
(class template)

[edit] Numeric algorithms

The header <numeric> provides numeric algorithms below:

[edit] Factor operations

Defined in header <numeric>
(C++17)
computes the greatest common divisor of two integers
(function template) [edit]
(C++17)
computes the least common multiple of two integers
(function template) [edit]

[edit] Interpolation operations

Defined in header <numeric>
(C++20)
midpoint between two numbers or pointers
(function template) [edit]
Defined in header <cmath>
(C++20)
linear interpolation function
(function) [edit]

[edit] Saturation arithmetic (since C++26)

Defined in header <numeric>
(C++26)
saturating addition operation on two integers
(function template) [edit]
(C++26)
saturating subtraction operation on two integers
(function template) [edit]
(C++26)
saturating multiplication operation on two integers
(function template) [edit]
(C++26)
saturating division operation on two integers
(function template) [edit]
returns an integer value clamped to the range of a another integer type
(function template) [edit]

[edit] Numeric operations

Defined in header <numeric>
(C++11)
fills a range with successive increments of the starting value
(function template) [edit]
fills a range with successive increments of the starting value
(niebloid)[edit]
sums up or folds a range of elements
(function template) [edit]
(C++17)
similar to std::accumulate, except out of order
(function template) [edit]
applies an invocable, then reduces out of order
(function template) [edit]
computes the inner product of two ranges of elements
(function template) [edit]
computes the differences between adjacent elements in a range
(function template) [edit]
computes the partial sum of a range of elements
(function template) [edit]
similar to std::partial_sum, includes the ith input element in the ith sum
(function template) [edit]
similar to std::partial_sum, excludes the ith input element from the ith sum
(function template) [edit]
applies an invocable, then calculates inclusive scan
(function template) [edit]
applies an invocable, then calculates exclusive scan
(function template) [edit]

[edit] Miscellanous

[edit] Pseudo-random number generation

The header <random> defines pseudo-random number generators and numerical distributions. The header <cstdlib> also includes C-style random number generation via std::srand and std::rand.

[edit] Floating-point environment (since C++11)

The header <cfenv> defines flags and functions related to exceptional floating-point state, such as overflow and division by zero.

[edit] Bit manipulation (since C++20)

The header <bit> provides several function templates to access, manipulate, and process individual bits and bit sequences.

Defined in header <bit>
Defined in namespace std
(C++20)
reinterpret the object representation of one type as that of another
(function template) [edit]
(C++23)
reverses the bytes in the given integer value
(function template) [edit]
checks if a number is an integral power of 2
(function template) [edit]
(C++20)
finds the smallest integral power of two not less than the given value
(function template) [edit]
(C++20)
finds the largest integral power of two not greater than the given value
(function template) [edit]
(C++20)
finds the smallest number of bits needed to represent the given value
(function template) [edit]
(C++20)
computes the result of bitwise left-rotation
(function template) [edit]
(C++20)
computes the result of bitwise right-rotation
(function template) [edit]
counts the number of consecutive 0 bits, starting from the most significant bit
(function template) [edit]
counts the number of consecutive 1 bits, starting from the most significant bit
(function template) [edit]
counts the number of consecutive 0 bits, starting from the least significant bit
(function template) [edit]
counts the number of consecutive 1 bits, starting from the least significant bit
(function template) [edit]
(C++20)
counts the number of 1 bits in an unsigned integer
(function template) [edit]
(C++20)
indicates the endianness of scalar types
(enum) [edit]

[edit] See also

C documentation for Numerics