Common mathematical functions | |||||||||||||||||||||||||||||||
Mathematical special functions(C++17) | |||||||||||||||||||||||||||||||
Mathematical constants(C++20) | |||||||||||||||||||||||||||||||
Basic linear algebra algorithms(C++26) | |||||||||||||||||||||||||||||||
Data-parallel types (SIMD)(C++26) | |||||||||||||||||||||||||||||||
Floating-point environment(C++11) | |||||||||||||||||||||||||||||||
Complex numbers | |||||||||||||||||||||||||||||||
Numeric array (valarray ) | |||||||||||||||||||||||||||||||
Pseudo-random number generation | |||||||||||||||||||||||||||||||
Bit manipulation(C++20) | |||||||||||||||||||||||||||||||
Saturation arithmetic(C++26) | |||||||||||||||||||||||||||||||
Factor operations | |||||||||||||||||||||||||||||||
|
| ||||||||||||||||||||||||||||||
Interpolations | |||||||||||||||||||||||||||||||
|
| ||||||||||||||||||||||||||||||
Generic numeric operations | |||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||
C-style checked integer arithmetic | |||||||||||||||||||||||||||||||
|
Nearest integer floating point operations | |||||||||||||||||||||||||||||||||||||||||
|
| ||||||||||||||||||||||||||||||||||||||||
Floating point manipulation functions | |||||||||||||||||||||||||||||||||||||||||
|
| ||||||||||||||||||||||||||||||||||||||||
Classification and comparison | |||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||
Types | |||||||||||||||||||||||||||||||||||||||||
Macro constants | |||||||||||||||||||||||||||||||||||||||||
|
|
Defined in header <cmath> | ||
(1) | ||
float remquo(float x,float y,int* quo); double remquo(double x,double y,int* quo); | (since C++11) (until C++23) | |
constexpr/* floating-point-type */ remquo(/* floating-point-type */ x, | (since C++23) | |
float remquof(float x,float y,int* quo); | (2) | (since C++11) (constexpr since C++23) |
longdouble remquol(longdouble x,longdouble y,int* quo); | (3) | (since C++11) (constexpr since C++23) |
Defined in header <cmath> | ||
template<class Arithmetic1,class Arithmetic2> /* common-floating-point-type */ | (A) | (since C++11) (constexpr since C++23) |
std::remquo
for all cv-unqualified floating-point types as the type of the parametersx andy.(since C++23)Contents |
x, y | - | floating-point or integer values |
quo | - | pointer toint to store the sign and some bits ofx/ y |
If successful, returns the floating-point remainder of the divisionx/ y as defined instd::remainder, and stores, in*quo, the sign and at least three of the least significant bits ofx/ y (formally, stores a value whose sign is the sign ofx/ y and whose magnitude is congruentmodulo 2n
to the magnitude of the integral quotient ofx/ y, wheren is an implementation-defined integer greater than or equal to3).
Ify is zero, the value stored in*quo is unspecified.
If a domain error occurs, an implementation-defined value is returned (NaN where supported).
If a range error occurs due to underflow, the correct result is returned if subnormals are supported.
Ify is zero, but the domain error does not occur, zero is returned.
Errors are reported as specified inmath_errhandling.
Domain error may occur ify is zero.
If the implementation supports IEEE floating-point arithmetic (IEC 60559),
POSIX requires that a domain error occurs ifx is infinite ory is zero.
This function is useful when implementing periodic functions with the period exactly representable as a floating-point value: when calculatingsin(πx) for a very largex, callingstd::sin directly may result in a large error, but if the function argument is first reduced withstd::remquo
, the low-order bits of the quotient may be used to determine the sign and the octant of the result within the period, while the remainder may be used to calculate the value with high precision.
On some platforms this operation is supported by hardware (and, for example, on Intel CPUs,FPREM1
leaves exactly 3 bits of precision in the quotient when complete).
The additional overloads are not required to be provided exactly as(A). They only need to be sufficient to ensure that for their first argumentnum1 and second argumentnum2:
| (until C++23) |
Ifnum1 andnum2 have arithmetic types, thenstd::remquo(num1, num2, quo) has the same effect asstd::remquo(static_cast</*common-floating-point-type*/>(num1), If no such floating-point type with the greatest rank and subrank exists, thenoverload resolution does not result in a usable candidate from the overloads provided. | (since C++23) |
#include <cfenv>#include <cmath>#include <iostream> #ifndef __GNUC__#pragma STDC FENV_ACCESS ON#endif constdouble pi=std::acos(-1);// or std::numbers::pi since C++20 double cos_pi_x_naive(double x){returnstd::cos(pi* x);} // the period is 2, values are (0;0.5) positive, (0.5;1.5) negative, (1.5,2) positivedouble cos_pi_x_smart(double x){int quadrant;double rem= std::remquo(x,1,&quadrant); quadrant=static_cast<unsigned>(quadrant)%2;// The period is 2.return quadrant==0?std::cos(pi* rem):-std::cos(pi* rem);} int main(){std::cout<<std::showpos<<"naive:\n"<<" cos(pi * 0.25) = "<< cos_pi_x_naive(0.25)<<'\n'<<" cos(pi * 1.25) = "<< cos_pi_x_naive(1.25)<<'\n'<<" cos(pi * 2.25) = "<< cos_pi_x_naive(2.25)<<'\n'<<"smart:\n"<<" cos(pi * 0.25) = "<< cos_pi_x_smart(0.25)<<'\n'<<" cos(pi * 1.25) = "<< cos_pi_x_smart(1.25)<<'\n'<<" cos(pi * 2.25) = "<< cos_pi_x_smart(2.25)<<'\n'<<"naive:\n"<<" cos(pi * 1000000000000.25) = "<< cos_pi_x_naive(1000000000000.25)<<'\n'<<" cos(pi * 1000000000001.25) = "<< cos_pi_x_naive(1000000000001.25)<<'\n'<<"smart:\n"<<" cos(pi * 1000000000000.25) = "<< cos_pi_x_smart(1000000000000.25)<<'\n'<<" cos(pi * 1000000000001.25) = "<< cos_pi_x_smart(1000000000001.25)<<'\n'; // error handlingstd::feclearexcept(FE_ALL_EXCEPT); int quo;std::cout<<"remquo(+Inf, 1) = "<< std::remquo(INFINITY,1,&quo)<<'\n';if(fetestexcept(FE_INVALID))std::cout<<" FE_INVALID raised\n";}
Possible output:
naive: cos(pi * 0.25) = +0.707107 cos(pi * 1.25) = -0.707107 cos(pi * 2.25) = +0.707107smart: cos(pi * 0.25) = +0.707107 cos(pi * 1.25) = -0.707107 cos(pi * 2.25) = +0.707107naive: cos(pi * 1000000000000.25) = +0.707123 cos(pi * 1000000000001.25) = -0.707117smart: cos(pi * 1000000000000.25) = +0.707107 cos(pi * 1000000000001.25) = -0.707107remquo(+Inf, 1) = -nan FE_INVALID raised
(C++11) | computes quotient and remainder of integer division (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 documentation forremquo |