remquo, remquof, remquol
来自 cppreference.cn
定义于头文件 <math.h> |
||
float remquof( float x, float y, int *quo ); |
(1) | (C99 起) |
double remquo( double x, double y, int *quo ); |
(2) | (C99 起) |
long double remquol( long double x, long double y, int *quo ); |
(3) | (C99 起) |
定义于头文件 <tgmath.h> |
||
#define remquo( x, y, quo ) |
(4) | (C99 起) |
4) 泛型宏:如果任何非指针参数的类型为 long double,则调用
remquol
。否则,如果任何非指针参数具有整数类型或类型为 double,则调用 remquo
。否则,调用 remquof
。目录 |
[编辑] 参数
x, y | - | 浮点值 |
quo | - | 指向一个整数值,用于存储 x/y 的符号和一些位 |
[编辑] 返回值
如果成功,返回除法 x/y 的浮点余数,其定义与 remainder 相同,并在 *quo 中存储 x/y 的符号和至少三位最低有效位(形式上,存储一个值,其符号是 x/y 的符号,其幅值与 x/y 的整数商的幅值模 2n
同余,其中 n 是一个实现定义的整数,大于或等于 3)。
如果 y 为零,则存储在 *quo 中的值是未指定的。
如果发生域错误,则返回实现定义的值 (支持 NaN 时返回 NaN)。
如果由于下溢发生范围错误,并且支持非正规数,则返回正确结果。
如果 y 为零,但未发生域错误,则返回零。
[编辑] 错误处理
错误按 math_errhandling
中指定的方式报告。
如果 y 为零,则可能发生域错误。
如果实现支持 IEEE 浮点运算 (IEC 60559),
- 当前的舍入模式无效。
- FE_INEXACT 从不引发
- 如果 x 是 ±∞ 且 y 不是 NaN,则返回 NaN 并引发 FE_INVALID
- 如果 y 是 ±0 且 x 不是 NaN,则返回 NaN 并引发 FE_INVALID
- 如果 x 或 y 是 NaN,则返回 NaN
[编辑] 注意
POSIX 要求如果 x 是无穷大或 y 为零,则会发生域错误。
此函数在实现周期函数时很有用,其中周期可以精确表示为浮点值:当计算非常大的 x 的 sin(πx) 时,直接调用 sin 可能会导致很大误差,但如果首先使用 remquo
对函数参数进行约化,则商的低阶位可用于确定结果在周期内的符号和象限,而余数可用于高精度计算值。
在某些平台上,此操作由硬件支持(例如,在 Intel CPU 上,FPREM1
在商中保留精确的 3 位精度)。
[编辑] 示例
运行此代码
#include <fenv.h> #include <math.h> #include <stdio.h> #ifndef __GNUC__ #pragma STDC FENV_ACCESS ON #endif double cos_pi_x_naive(double x) { const double pi = acos(-1); return 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) { const double pi = acos(-1); int extremum; double rem = remquo(x, 1, &extremum); extremum = (unsigned)extremum % 2; // keep 1 bit to determine nearest extremum return extremum ? -cos(pi * rem) : cos(pi * rem); } int main(void) { printf("cos(pi * 0.25) = %f\n", cos_pi_x_naive(0.25)); printf("cos(pi * 1.25) = %f\n", cos_pi_x_naive(1.25)); printf("cos(pi * 1000000000000.25) = %f\n", cos_pi_x_naive(1000000000000.25)); printf("cos(pi * 1000000000001.25) = %f\n", cos_pi_x_naive(1000000000001.25)); printf("cos(pi * 1000000000000.25) = %f\n", cos_pi_x_smart(1000000000000.25)); printf("cos(pi * 1000000000001.25) = %f\n", cos_pi_x_smart(1000000000001.25)); // error handling feclearexcept(FE_ALL_EXCEPT); int quo; printf("remquo(+Inf, 1) = %.1f\n", remquo(INFINITY, 1, &quo)); if (fetestexcept(FE_INVALID)) puts(" FE_INVALID raised"); }
可能的输出
cos(pi * 0.25) = 0.707107 cos(pi * 1.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
[编辑] 参考文献
- C23 标准 (ISO/IEC 9899:2024)
- 7.12.10.3 remquo 函数 (p: TBD)
- 7.25 类型通用数学 <tgmath.h> (p: TBD)
- F.10.7.3 remquo 函数 (p: TBD)
- C17 标准 (ISO/IEC 9899:2018)
- 7.12.10.3 remquo 函数 (p: 186)
- 7.25 类型通用数学 <tgmath.h> (p: 272-273)
- F.10.7.3 remquo 函数 (p: 385)
- C11 标准 (ISO/IEC 9899:2011)
- 7.12.10.3 remquo 函数 (p: 255)
- 7.25 类型通用数学 <tgmath.h> (p: 373-375)
- F.10.7.3 remquo 函数 (p: 529)
- C99 标准 (ISO/IEC 9899:1999)
- 7.12.10.3 remquo 函数 (p: 236)
- 7.22 类型通用数学 <tgmath.h> (p: 335-337)
- F.9.7.3 remquo 函数 (p: 465)
[编辑] 另请参阅
(C99) |
计算整数除法的商和余数 (函数) |
(C99)(C99) |
计算浮点除法运算的余数 (函数) |
(C99)(C99)(C99) |
计算浮点除法运算的带符号余数 (函数) |
C++ 文档 关于 remquo
|