Remquo
定义于头文件 <cmath>
中。
描述
计算除法操作 x / y
的浮点余数,与 std::remainder 函数相同。此外,x / y
的符号和至少最后三位将存储在 quo
中,足以确定结果在一个周期内的八分位。
声明
- C++23
- C++11
// 1)
constexpr /* floating-point-type */
remquo ( /* floating-point-type */ x,
/* floating-point-type */ y, int* quo );
// 2)
constexpr float remquof( float x, float y, int* quo );
// 3)
constexpr long double remquol( long double x, long double y, int* quo );
// 4)
template< class Arithmetic1, class Arithmetic2 >
/* common-floating-point-type */
constexpr remquo( Arithmetic1 x, Arithmetic2 y, int* quo );
// 1)
float remquo ( float x, float y, int* quo );
// 2)
double remquo ( double x, double y, int* quo );
// 3)
long double remquo ( long double x, long double y, int* quo );
// 4)
float remquof( float x, float y, int* quo );
// 5)
long double remquol( long double x, long double y, int* quo );
// 6)
template< class Arithmetic1, class Arithmetic2 >
/* common-floating-point-type */
remquo( Arithmetic1 x, Arithmetic2 y, int* quo );
参数
x
,y
- 浮点或整数值 quo
- 指向 int 的指针,用于存储 x / y
的符号和一些位
返回值
如果成功,返回除法 x / y
的浮点余数,如 std::remainder 中所定义,并在 *quo
中存储 x / y
的符号和至少三个最低有效位(形式上,存储一个值,其符号是 x / y
的符号,其大小与 x / y
的整数商的大小模 2n 同余,其中 n
是一个实现定义的整数,大于或等于 3)。
如果 y
为零,则存储在 *quo
中的值未指定。
如果发生域错误,则返回实现定义的值(如果支持,返回 NaN)。
如果由于下溢导致范围错误,如果支持次正规数,则返回正确的结果。
如果 y
为零,但未发生域错误,则返回零。
错误处理
如果 y
为零,可能会发生域错误。
如果实现支持 IEEE 浮点运算(IEC 60559)
当前的舍入模式无效
FE_INEXACT 从不引发
- 如果
x
是 ±∞ 且y
不是 NaN,返回 NaN 并引发 FE_INVALID - 如果
y
是 ±0 且x
不是 NaN,返回 NaN 并引发 FE_INVALID - 如果
x
或y
为 NaN,则返回 NaN
示例
#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";
}
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