std::remquo
Defined in header <cmath> | | |
|---|---|---|
float remquo( float x, float y, int* quo ); | (1) | (since C++11) |
double remquo( double x, double y, int* quo ); | (2) | (since C++11) |
long double remquo( long double x, long double y, int* quo ); | (3) | (since C++11) |
Promoted remquo( Arithmetic1 x, Arithmetic2 y, int* quo ); | (4) | (since C++11) |
1-3%29计算除法操作的浮点余数。x/y就像std::remainder()功能是这样的。另外,标志和至少最后的三位x/y将存储在quo,足以在一段时间内确定结果的重要性。
的所有参数组合的一组重载或函数模板算术类型不包括在1-3的范围内。如果任何非指针参数积分型,它被铸造成double如果任何其他非指针参数是long double,则返回类型为long double,否则就是double...
参数
x, y | - | floating point values |
|---|---|---|
quo | - | pointer to an integer value to store the sign and some bits of x/y |
返回值
如果成功,则返回除法的浮点余数。x/y中定义的std::remainder,和商店,在*quo的标志和至少三个最不重要的部分x/y%28形式上,存储其符号为x/y其幅度为同余模2n。
的积分商数的大小x/y,其中n是一个实现定义的整数,大于或等于3%29。
如果y中存储的值为零。*quo没有具体说明。
如果发生域错误,则返回支持%29的实现定义值%28 NaN。
如果由于下流而发生范围错误,则如果支持异常,则返回正确的结果。
如果y为零,但域错误不发生,则返回零。
错误处理
错误按数学[医]错误处理...
域错误可能发生在y是零。
如果实现支持ieee浮点算法%28IEC 60559%29,
- 电流舍入方式没有效果。
FE_INEXACT永远不会被提起
- 如果
x是±∞和y不是南,南回来了FE_INVALID提出来
- 如果
y是±0和x不是南,南回来了FE_INVALID提出来
- 如果
x或y是南,南回来了
注记
POSIX要求,则域错误将发生在以下情况下:x是无限的或y是零。
此函数在实现周期函数时非常有用,周期完全可以表示为浮点值:当计算一个非常大的值时,当计算sin%28πx%29时。x,呼叫std::sin直接导致较大的错误,但是如果函数参数首先被还原为std::remquo商的低阶位可用于在周期内确定结果的符号和符号,而剩余的位可用于高精度地计算值。
在某些平台上,硬件%28支持此操作,例如在Intel CPU上,FPREM1当完成%29时,在商中留下精确的3位精度。
例
二次
#include <iostream>
#include <cmath>
#include <cfenv>
#pragma STDC FENV_ACCESS ON
const double pi = std::acos(-1);
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 = (unsigned)quadrant % 2; // The period is 2.
switch(quadrant) {
case 0: return std::cos(pi * rem);
case 1: return -std::cos(pi * rem);
};
}
int main()
{
std::cout << "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'
<< "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'
<< "cos(pi * 1000000000000.25) = "
<< cos_pi_x_naive(1000000000000.25) << '\n'
<< "cos(pi * 1000000000001.25) = "
<< cos_pi_x_naive(1000000000001.25) << '\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";
}二次
可能的产出:
二次
cos(pi * 0.25) = 0.707107
cos(pi * 1.25) = -0.707107
cos(pi * 2.25) = 0.707107
cos(pi * 0.25) = 0.707107
cos(pi * 1.25) = -0.707107
cos(pi * 2.25) = 0.707107
cos(pi * 1000000000000.25) = 0.707123
cos(pi * 1000000000001.25) = -0.707117
cos(pi * 1000000000000.25) = 0.707107
cos(pi * 1000000000001.25) = -0.707107
remquo(+Inf, 1) = -nan
FE_INVALID raised二次
另见
div(int)ldivlldiv (C++11) | computes quotient and remainder of integer division (function) |
|---|---|
fmod | remainder of the floating point division operation (function) |
remainder (C++11) | signed remainder of the division operation (function) |
C.维持原状的文件
© cppreference.com在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。
本文档系腾讯云开发者社区成员共同维护,如有问题请联系 cloudcommunity@tencent.com

