std::remquo...std::remquol(3)
β¨ NAME β¨
The NAME block is literally std::remquo,std::remquof,std::remquol -, and this is the std::remquo...std::remquol(3) page from the C++ Standard Libary. Yes bestie, we are doing floating-point remainder math with quotient crumbs, and itβs lowkey iconic. π
π¦ Synopsis π¦
Okay so the page lays it out like this β keep the labels, theyβre doing important overload bookkeeping β¨:
Defined in header <cmath>
float remquo ( float x, float y, int* quo );
double remquo ( double x, double y, int* quo ); (since C++11)
(until C++23)
long double remquo ( long double x, long double y,
int* quo );
constexpr /* floating-point-type */
remquo ( /* floating-point-type */ x, (since C++23)
/* floating-point-type */ y,
int* quo ); (1)
float remquof( float x, float y, int* quo ); (2) (since C++11)
(constexpr since C++23)
long double remquol( long double x, long double y, (3) (since C++11)
int* quo );
(constexpr since C++23)
Additional overloads
Defined in header <cmath>
template< class Arithmetic1, class Arithmetic2 >
/* common-floating-point-type */ (A) (since C++11)
(constexpr since C++23)
remquo( Arithmetic1 x, Arithmetic2 y, int* quo );
1-3) Computes the floating-point remainder of the division operation x / y as the std::remainder() function does. Additionally, the sign and at least the three of the last bits of x / y will be stored in quo, sufficient to determine the octant of the result within a period. π
The library provides overloads of std::remquo for all cv-unqualified floating-point types as the type of the parameters x and y. (since C++23)
A) Additional overloads are provided for all other combinations of arithmetic types.
π Parameters π
x,yβ floating-point or integer values.quoβ pointer tointto store the sign and some bits ofx / y.
π₯ Return value π₯
If successful, returns the floating-point remainder of the division x / y as defined in std::remainder, and stores, in *quo, the sign and at least three of the least significant bits of x / y (formally, stores a value whose sign is the sign of x / y and whose magnitude is congruent modulo 2n to the magnitude of the integral quotient of x / y, where n is an implementation-defined integer greater than or equal to 3). π«Ά
Heads up bestie: ifyis zero, the value stored in*quois 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.
If y is zero, but the domain error does not occur, zero is returned.
π Error handling π
Errors are reported as specified in math_errhandling.
Domain error may occur if y is zero.
If the implementation supports IEEE floating-point arithmetic (IEC 60559):
- The current rounding mode has no effect.
FE_INEXACTis never raised.- If
xis Β±β andyis not NaN, NaN is returned andFE_INVALIDis raised. - If
yis Β±0 andxis not NaN, NaN is returned andFE_INVALIDis raised. - If either
xoryis NaN, NaN is returned.
πΈ Notes πΈ
POSIX requires that a domain error occurs if x is infinite or y is zero.
This function is useful when implementing periodic functions with the period exactly representable as a floating-point value: when calculating sin(Οx) for a very large x, calling std::sin directly may result in a large error, but if the function argument is first reduced with std::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 argument num1 and second argument num2:
- If
num1ornum2has typelong double, thenstd::remquo(num1, num2, quo)has the same effect asstd::remquo(static_cast<long double>(num1), static_cast<long double>(num2), quo). - Otherwise, if
num1and/ornum2has typedoubleor an integer type, thenstd::remquo(num1, num2, quo)has the same effect as (until C++23)std::remquo(static_cast<double>(num1), static_cast<double>(num2), quo). - Otherwise, if
num1ornum2has typefloat, thenstd::remquo(num1, num2, quo)has the same effect asstd::remquo(static_cast<float>(num1), static_cast<float>(num2), quo).
If num1 and num2 have arithmetic types, then std::remquo(num1, num2, quo) has the same effect as std::remquo(static_cast</* common-floating-point-type */>(num1), static_cast</* common-floating-point-type */>(num2), quo), where /* common-floating-point-type */ is the floating-point type with the greatest floating-point conversion rank and greatest floating-point conversion subrank between the types of num1 and num2, (since C++23) arguments of integer type are considered to have the same floating-point conversion rank as double.
If no such floating-point type with the greatest rank and subrank exists, then overload resolution does not result in a usable candidate from the overloads provided.
π§ Example π§
The page has the usual // Run this code label, then this absolute unit of a demo:
#include <cfenv>
#include <cmath>
#include <iostream>
#ifndef __GNUC__
#pragma STDC FENV_ACCESS ON
#endif
const double pi = std::acos(-1); // or std::numbers::pi since C++20
double cos_pi_x_naive(double x)
{
return std::cos(pi * x);
}
// the period is 2, values are (0;0.5) positive, (0.5;1.5) negative, (1.5,2) positive
double 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 handling
std::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.707107
smart:
cos(pi * 0.25) = +0.707107
cos(pi * 1.25) = -0.707107
cos(pi * 2.25) = +0.707107
naive:
cos(pi * 1000000000000.25) = +0.707123
cos(pi * 1000000000001.25) = -0.707117
smart:
cos(pi * 1000000000000.25) = +0.707107
cos(pi * 1000000000001.25) = -0.707107
remquo(+Inf, 1) = -nan
FE_INVALID raised
π See also π
div(int)β computes quotient and remainder of integer division(function).ldivβ computes quotient and remainder of integer division(function).lldivβ computes quotient and remainder of integer division(function)(C++11).fmod,fmodf,fmodlβ remainder of the floating point division operation(function)(C++11)(C++11).remainder,remainderf,remainderlβ signed remainder of the division operation(function)(C++11)(C++11)(C++11).- C documentation for
remquo.
Okay thatβs the whole page, bestie β floating-point quotient crumbs, IEEE drama, and all. π β¨
source/version/date cuteness, as promised: http://cppreference.com Β· 2024.06.10 Β· std::remquo...std::remquol(3) ππ¦π
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms