Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/numeric/math"

From cppreference.com
< cpp‎ | numeric
(actually, FLT_EVAL_METHOD would work better inside the float_t/double_t page. This page is big enough already)
m (Notes: FTM fix: <complex> has its own `__cpp_lib_constexpr_complex` macro.)
 
(48 intermediate revisions by 15 users not shown)
Line 1: Line 1:
 
{{title|Common mathematical functions}}
 
{{title|Common mathematical functions}}
{{cpp/numeric/math/sidebar}}
+
{{cpp/numeric/math/navbar}}
  
 
===Functions===
 
===Functions===
{{dcl list begin}}
+
{{dsc begin}}
{{dcl list header | cmath}}
+
{{dsc header|cstdlib}}
{{dcl list h2 | Basic operations}}
+
{{dsc inc|cpp/numeric/math/dsc abs}}
{{dcl list template | cpp/numeric/math/dcl list abs}}
+
{{dsc inc|cpp/numeric/math/dsc div}}
{{dcl list template | cpp/numeric/math/dcl list fabs}}
+
{{dcl list template | cpp/numeric/math/dcl list div}}
+
{{dcl list template | cpp/numeric/math/dcl list fmod}}
+
{{dcl list template | cpp/numeric/math/dcl list remainder}}
+
{{dcl list template | cpp/numeric/math/dcl list remquo}}
+
{{dcl list template | cpp/numeric/math/dcl list fma}}
+
{{dcl list template | cpp/numeric/math/dcl list fmax}}
+
{{dcl list template | cpp/numeric/math/dcl list fmin}}
+
{{dcl list template | cpp/numeric/math/dcl list fdim}}
+
{{dcl list template | cpp/numeric/math/dcl list fnan}}
+
  
{{dcl list h2 | Exponential functions}}
+
{{dsc header|cinttypes}}
{{dcl list template | cpp/numeric/math/dcl list exp}}
+
{{dsc inc|cpp/numeric/math/dsc imaxabs}}
{{dcl list template | cpp/numeric/math/dcl list exp2}}
+
{{dsc inc|cpp/numeric/math/dsc imaxdiv}}
{{dcl list template | cpp/numeric/math/dcl list expm1}}
+
{{dcl list template | cpp/numeric/math/dcl list log}}
+
{{dcl list template | cpp/numeric/math/dcl list log10}}
+
{{dcl list template | cpp/numeric/math/dcl list log1p}}
+
{{dcl list template | cpp/numeric/math/dcl list log2}}
+
  
{{dcl list h2 | Power functions}}
+
{{dsc header|cmath}}
{{dcl list template | cpp/numeric/math/dcl list sqrt}}
+
{{dsc h2|Basic operations}}
{{dcl list template | cpp/numeric/math/dcl list cbrt}}
+
{{dsc inc|cpp/numeric/math/dsc fabs}}
{{dcl list template | cpp/numeric/math/dcl list hypot}}
+
{{dsc inc|cpp/numeric/math/dsc fmod}}
{{dcl list template | cpp/numeric/math/dcl list pow}}
+
{{dsc inc|cpp/numeric/math/dsc remainder}}
 +
{{dsc inc|cpp/numeric/math/dsc remquo}}
 +
{{dsc inc|cpp/numeric/math/dsc fma}}
 +
{{dsc inc|cpp/numeric/math/dsc fmax}}
 +
{{dsc inc|cpp/numeric/math/dsc fmin}}
 +
{{dsc inc|cpp/numeric/math/dsc fdim}}
 +
{{dsc inc|cpp/numeric/math/dsc fnan}}
  
{{dcl list h2 | Trigonometric functions}}
+
{{dsc h2|Exponential functions}}
{{dcl list template | cpp/numeric/math/dcl list sin}}
+
{{dsc inc|cpp/numeric/math/dsc exp}}
{{dcl list template | cpp/numeric/math/dcl list cos}}
+
{{dsc inc|cpp/numeric/math/dsc exp2}}
{{dcl list template | cpp/numeric/math/dcl list tan}}
+
{{dsc inc|cpp/numeric/math/dsc expm1}}
{{dcl list template | cpp/numeric/math/dcl list asin}}
+
{{dsc inc|cpp/numeric/math/dsc log}}
{{dcl list template | cpp/numeric/math/dcl list acos}}
+
{{dsc inc|cpp/numeric/math/dsc log10}}
{{dcl list template | cpp/numeric/math/dcl list atan}}
+
{{dsc inc|cpp/numeric/math/dsc log2}}
{{dcl list template | cpp/numeric/math/dcl list atan2}}
+
{{dsc inc|cpp/numeric/math/dsc log1p}}
  
{{dcl list h2 | Hyperbolic functions}}
+
{{dsc h2|Power functions}}
{{dcl list template | cpp/numeric/math/dcl list sinh}}
+
{{dsc inc|cpp/numeric/math/dsc pow}}
{{dcl list template | cpp/numeric/math/dcl list cosh}}
+
{{dsc inc|cpp/numeric/math/dsc sqrt}}
{{dcl list template | cpp/numeric/math/dcl list tanh}}
+
{{dsc inc|cpp/numeric/math/dsc cbrt}}
{{dcl list template | cpp/numeric/math/dcl list asinh}}
+
{{dsc inc|cpp/numeric/math/dsc hypot}}
{{dcl list template | cpp/numeric/math/dcl list acosh}}
+
{{dcl list template | cpp/numeric/math/dcl list atanh}}
+
  
{{dcl list h2 | Error and gamma functions}}
+
{{dsc h2|Trigonometric functions}}
{{dcl list template | cpp/numeric/math/dcl list erf}}
+
{{dsc inc|cpp/numeric/math/dsc sin}}
{{dcl list template | cpp/numeric/math/dcl list erfc}}
+
{{dsc inc|cpp/numeric/math/dsc cos}}
{{dcl list template | cpp/numeric/math/dcl list lgamma}}
+
{{dsc inc|cpp/numeric/math/dsc tan}}
{{dcl list template | cpp/numeric/math/dcl list tgamma}}
+
{{dsc inc|cpp/numeric/math/dsc asin}}
 +
{{dsc inc|cpp/numeric/math/dsc acos}}
 +
{{dsc inc|cpp/numeric/math/dsc atan}}
 +
{{dsc inc|cpp/numeric/math/dsc atan2}}
  
{{dcl list h2 | Nearest integer floating point operations}}
+
{{dsc h2|Hyperbolic functions}}
{{dcl list template | cpp/numeric/math/dcl list ceil}}
+
{{dsc inc|cpp/numeric/math/dsc sinh}}
{{dcl list template | cpp/numeric/math/dcl list floor}}
+
{{dsc inc|cpp/numeric/math/dsc cosh}}
{{dcl list template | cpp/numeric/math/dcl list trunc}}
+
{{dsc inc|cpp/numeric/math/dsc tanh}}
{{dcl list template | cpp/numeric/math/dcl list round}}
+
{{dsc inc|cpp/numeric/math/dsc asinh}}
{{dcl list template | cpp/numeric/math/dcl list nearbyint}}
+
{{dsc inc|cpp/numeric/math/dsc acosh}}
{{dcl list template | cpp/numeric/math/dcl list rint}}
+
{{dsc inc|cpp/numeric/math/dsc atanh}}
  
{{dcl list h2 | Floating point manipulation functions}}
+
{{dsc h2|Error and gamma functions}}
{{dcl list template | cpp/numeric/math/dcl list frexp}}
+
{{dsc inc|cpp/numeric/math/dsc erf}}
{{dcl list template | cpp/numeric/math/dcl list ldexp}}
+
{{dsc inc|cpp/numeric/math/dsc erfc}}
{{dcl list template | cpp/numeric/math/dcl list modf}}
+
{{dsc inc|cpp/numeric/math/dsc tgamma}}
{{dcl list template | cpp/numeric/math/dcl list scalbn}}
+
{{dsc inc|cpp/numeric/math/dsc lgamma}}
{{dcl list template | cpp/numeric/math/dcl list ilogb}}
+
{{dcl list template | cpp/numeric/math/dcl list logb}}
+
{{dcl list template | cpp/numeric/math/dcl list nextafter}}
+
{{dcl list template | cpp/numeric/math/dcl list copysign}}
+
  
{{dcl list h2 | Classification}}
+
{{dsc h2|Nearest integer floating point operations}}
{{dcl list template | cpp/numeric/math/dcl list fpclassify}}
+
{{dsc inc|cpp/numeric/math/dsc ceil}}
{{dcl list template | cpp/numeric/math/dcl list isfinite}}
+
{{dsc inc|cpp/numeric/math/dsc floor}}
{{dcl list template | cpp/numeric/math/dcl list isinf}}
+
{{dsc inc|cpp/numeric/math/dsc trunc}}
{{dcl list template | cpp/numeric/math/dcl list isnan}}
+
{{dsc inc|cpp/numeric/math/dsc round}}
{{dcl list template | cpp/numeric/math/dcl list isnormal}}
+
{{dsc inc|cpp/numeric/math/dsc nearbyint}}
{{dcl list template | cpp/numeric/math/dcl list signbit}}
+
{{dsc inc|cpp/numeric/math/dsc rint}}
{{dcl list end}}
+
  
===Macro constants===
+
{{dsc h2|Floating point manipulation functions}}
{{dcl list begin}}
+
{{dsc inc|cpp/numeric/math/dsc frexp}}
{{dcl list header | cmath}}
+
{{dsc inc|cpp/numeric/math/dsc ldexp}}
{{dcl list template | cpp/numeric/math/dcl list HUGE_VAL}}}
+
{{dsc inc|cpp/numeric/math/dsc modf}}
{{dcl list macro const | cpp/numeric/math/INFINITY | evaluates to positive infinity or the value guaranteed to overflow a {{c|float}}| notes={{mark c++11}} }}
+
{{dsc inc|cpp/numeric/math/dsc scalbn}}
{{dcl list macro const | cpp/numeric/math/NAN | evaluates to a quiet NaN of type {{c|float}} | notes={{mark c++11}}}}
+
{{dsc inc|cpp/numeric/math/dsc ilogb}}
{{dcl list macro const | cpp/numeric/math/math_errhandling | defines the error handling mechanism used by the common mathematical functions | notes={{mark c++11}} }} <!-- MATH_ERRNO and MATH_ERREXCEPT go inside this page -->
+
{{dsc inc|cpp/numeric/math/dsc logb}}
{{dcl list h2 | Classification}}
+
{{dsc inc|cpp/numeric/math/dsc nextafter}}
{{dcl list template | cpp/numeric/math/dcl list FP_categories}}
+
{{dsc inc|cpp/numeric/math/dsc copysign}}
{{dcl list end}}
+
 
 +
{{dsc h2|Classification and comparison}}
 +
{{dsc inc|cpp/numeric/math/dsc fpclassify}}
 +
{{dsc inc|cpp/numeric/math/dsc isfinite}}
 +
{{dsc inc|cpp/numeric/math/dsc isinf}}
 +
{{dsc inc|cpp/numeric/math/dsc isnan}}
 +
{{dsc inc|cpp/numeric/math/dsc isnormal}}
 +
{{dsc inc|cpp/numeric/math/dsc signbit}}
 +
 
 +
{{dsc inc|cpp/numeric/math/dsc isgreater}}
 +
{{dsc inc|cpp/numeric/math/dsc isgreaterequal}}
 +
{{dsc inc|cpp/numeric/math/dsc isless}}
 +
{{dsc inc|cpp/numeric/math/dsc islessequal}}
 +
{{dsc inc|cpp/numeric/math/dsc islessgreater}}
 +
{{dsc inc|cpp/numeric/math/dsc isunordered}}
 +
{{dsc end}}
  
 
===Types===
 
===Types===
{{dcl list begin}}
+
{{dsc begin}}
{{dcl list header | cmath }}
+
{{dsc header|cstdlib}}
{{dcl list typedef | cpp/numeric/math/float_t | title=float_t<br>double_t | most efficient floating-point type at least as wide as {{c|float}}<br>most efficient floating-point type at least as wide as {{c|double}}| notes={{mark c++11}}<br>{{mark c++11}}}} <!-- mention FLT_EVAL_METHOD inside -->
+
{{dsc inc|cpp/numeric/math/dsc div_t}}
{{dcl list end}}
+
{{dsc inc|cpp/numeric/math/dsc ldiv_t}}
 +
{{dsc inc|cpp/numeric/math/dsc lldiv_t}}
 +
{{dsc header|cinttypes}}
 +
{{dsc inc|cpp/numeric/math/dsc imaxdiv_t}}
 +
{{dsc header|cmath}}
 +
{{dsc inc|cpp/numeric/math/dsc float_t}}
 +
{{dsc inc|cpp/numeric/math/dsc double_t}}
 +
{{dsc end}}
 +
 
 +
===Macro constants===
 +
{{dsc begin}}
 +
{{dsc header|cmath}}
 +
{{dsc inc|cpp/numeric/math/dsc HUGE_VAL}}}
 +
{{dsc inc|cpp/numeric/math/dsc INFINITY}}}
 +
{{dsc inc|cpp/numeric/math/dsc NAN}}}
 +
{{dsc inc|cpp/numeric/math/dsc math_errhandling}}
 +
{{dsc h2|Classification}}
 +
{{dsc inc|cpp/numeric/math/dsc FP_categories}}
 +
{{dsc end}}
 +
 
 +
===Notes===
 +
{{ftm begin|std=yea|comment=yes}}
 +
{{ftm|__cpp_lib_constexpr_cmath|rowspan="2"|std=C++23|value=202202L|Constexpr for FP environment agnostic mathematical functions in {{header|cmath}} and {{header|cstdlib}}}}
 +
{{ftm|-|std=C++26|value=202306L|Constexpr for most mathematical functions in {{header|cmath}}}}
 +
{{ftm end}}
 +
 
 +
===See also===
 +
{{dsc begin}}
 +
{{dsc|[[cpp/numeric/special_functions|'''Mathematical special functions''']]}}
 +
{{dsc see c|c/numeric/math|Common mathematical functions|nomono=true}}
 +
{{dsc end}}
 +
 
 +
{{langlinks|cs|de|es|fr|it|ja|pl|pt|ru|tr|zh}}

Latest revision as of 14:20, 16 September 2023

 
 
 
Common mathematical functions
 

Contents

[edit] Functions

Defined in header <cstdlib>
computes absolute value of an integral value (|x|)
(function) [edit]
computes quotient and remainder of integer division
(function) [edit]
Defined in header <cinttypes>
computes absolute value of an integral value (|x|)
(function) [edit]
computes quotient and remainder of integer division
(function) [edit]
Defined in header <cmath>
Basic operations
absolute value of a floating point value (|x|)
(function) [edit]
(C++11)(C++11)
remainder of the floating point division operation
(function) [edit]
(C++11)(C++11)(C++11)
signed remainder of the division operation
(function) [edit]
(C++11)(C++11)(C++11)
signed remainder as well as the three last bits of the division operation
(function) [edit]
(C++11)(C++11)(C++11)
fused multiply-add operation
(function) [edit]
(C++11)(C++11)(C++11)
larger of two floating-point values
(function) [edit]
(C++11)(C++11)(C++11)
smaller of two floating point values
(function) [edit]
(C++11)(C++11)(C++11)
positive difference of two floating point values (max(0, x-y))
(function) [edit]
(C++11)(C++11)(C++11)
not-a-number (NaN)
(function) [edit]
Exponential functions
(C++11)(C++11)
returns e raised to the given power (ex)
(function) [edit]
(C++11)(C++11)(C++11)
returns 2 raised to the given power (2x)
(function) [edit]
(C++11)(C++11)(C++11)
returns e raised to the given power, minus one (ex-1)
(function) [edit]
(C++11)(C++11)
computes natural (base e) logarithm (ln(x))
(function) [edit]
(C++11)(C++11)
computes common (base 10) logarithm (log10(x))
(function) [edit]
(C++11)(C++11)(C++11)
base 2 logarithm of the given number (log2(x))
(function) [edit]
(C++11)(C++11)(C++11)
natural logarithm (to base e) of 1 plus the given number (ln(1+x))
(function) [edit]
Power functions
(C++11)(C++11)
raises a number to the given power (xy)
(function) [edit]
(C++11)(C++11)
computes square root (x)
(function) [edit]
(C++11)(C++11)(C++11)
computes cube root (3x)
(function) [edit]
(C++11)(C++11)(C++11)
computes square root of the sum of the squares of two or three(since C++17) given numbers (x2+y2), (x2+y2+z2)(since C++17)
(function) [edit]
Trigonometric functions
(C++11)(C++11)
computes sine (sin(x))
(function) [edit]
(C++11)(C++11)
computes cosine (cos(x))
(function) [edit]
(C++11)(C++11)
computes tangent (tan(x))
(function) [edit]
(C++11)(C++11)
computes arc sine (arcsin(x))
(function) [edit]
(C++11)(C++11)
computes arc cosine (arccos(x))
(function) [edit]
(C++11)(C++11)
computes arc tangent (arctan(x))
(function) [edit]
(C++11)(C++11)
arc tangent, using signs to determine quadrants
(function) [edit]
Hyperbolic functions
(C++11)(C++11)
computes hyperbolic sine (sinh(x))
(function) [edit]
(C++11)(C++11)
computes hyperbolic cosine (cosh(x))
(function) [edit]
(C++11)(C++11)
computes hyperbolic tangent (tanh(x))
(function) [edit]
(C++11)(C++11)(C++11)
computes the inverse hyperbolic sine (arsinh(x))
(function) [edit]
(C++11)(C++11)(C++11)
computes the inverse hyperbolic cosine (arcosh(x))
(function) [edit]
(C++11)(C++11)(C++11)
computes the inverse hyperbolic tangent (artanh(x))
(function) [edit]
Error and gamma functions
(C++11)(C++11)(C++11)
error function
(function) [edit]
(C++11)(C++11)(C++11)
complementary error function
(function) [edit]
(C++11)(C++11)(C++11)
gamma function
(function) [edit]
(C++11)(C++11)(C++11)
natural logarithm of the gamma function
(function) [edit]
Nearest integer floating point operations
(C++11)(C++11)
nearest integer not less than the given value
(function) [edit]
(C++11)(C++11)
nearest integer not greater than the given value
(function) [edit]
(C++11)(C++11)(C++11)
nearest integer not greater in magnitude than the given value
(function) [edit]
(C++11)(C++11)(C++11)(C++11)(C++11)(C++11)(C++11)(C++11)(C++11)
nearest integer, rounding away from zero in halfway cases
(function) [edit]
(C++11)(C++11)(C++11)
nearest integer using current rounding mode
(function) [edit]
(C++11)(C++11)(C++11)(C++11)(C++11)(C++11)(C++11)(C++11)(C++11)
nearest integer using current rounding mode with
exception if the result differs
(function) [edit]
Floating point manipulation functions
(C++11)(C++11)
decomposes a number into significand and base-2 exponent
(function) [edit]
(C++11)(C++11)
multiplies a number by 2 raised to an integral power
(function) [edit]
(C++11)(C++11)
decomposes a number into integer and fractional parts
(function) [edit]
(C++11)(C++11)(C++11)(C++11)(C++11)(C++11)
multiplies a number by FLT_RADIX raised to a power
(function) [edit]
(C++11)(C++11)(C++11)
extracts exponent of the number
(function) [edit]
(C++11)(C++11)(C++11)
extracts exponent of the number
(function) [edit]
(C++11)(C++11)(C++11)(C++11)(C++11)(C++11)
next representable floating-point value towards the given value
(function) [edit]
(C++11)(C++11)(C++11)
copies the sign of a floating point value
(function) [edit]
Classification and comparison
categorizes the given floating-point value
(function) [edit]
(C++11)
checks if the given number has finite value
(function) [edit]
(C++11)
checks if the given number is infinite
(function) [edit]
(C++11)
checks if the given number is NaN
(function) [edit]
(C++11)
checks if the given number is normal
(function) [edit]
(C++11)
checks if the given number is negative
(function) [edit]
(C++11)
checks if the first floating-point argument is greater than the second
(function) [edit]
checks if the first floating-point argument is greater or equal than the second
(function) [edit]
(C++11)
checks if the first floating-point argument is less than the second
(function) [edit]
checks if the first floating-point argument is less or equal than the second
(function) [edit]
checks if the first floating-point argument is less or greater than the second
(function) [edit]
checks if two floating-point values are unordered
(function) [edit]

[edit] Types

Defined in header <cstdlib>
structure type, returned by std::div
(typedef) [edit]
structure type, returned by std::ldiv
(typedef) [edit]
(C++11)
structure type, returned by std::lldiv
(typedef) [edit]
Defined in header <cinttypes>
(C++11)
structure type, returned by std::imaxdiv
(typedef) [edit]
Defined in header <cmath>
float_t
(C++11)
most efficient floating-point type at least as wide as float
(typedef) [edit]
double_t
(C++11)
most efficient floating-point type at least as wide as double
(typedef) [edit]

[edit] Macro constants

Defined in header <cmath>
indicates the overflow value for float, double and long double respectively
(macro constant) [edit]
(C++11)
evaluates to positive infinity or the value guaranteed to overflow a float
(macro constant) [edit]
(C++11)
evaluates to a quiet NaN of type float
(macro constant) [edit]
defines the error handling mechanism used by the common mathematical functions
(macro constant) [edit]
Classification
(C++11)(C++11)(C++11)(C++11)(C++11)
indicates a floating-point category
(macro constant) [edit]

[edit] Notes

Feature-test macro Value Std Feature
__cpp_lib_constexpr_cmath 202202L (C++23) Constexpr for FP environment agnostic mathematical functions in <cmath> and <cstdlib>
202306L (C++26) Constexpr for most mathematical functions in <cmath>

[edit] See also

Mathematical special functions
C documentation for Common mathematical functions