Namespaces
Variants

remquo, remquof, remquol

From cppreference.net
< c ‎ | numeric ‎ | math
Common mathematical functions
Functions
Basic operations
remquo
(C99)
(C99)
(C99)
(C99) (C99) (C99) (C23)
Maximum/minimum operations
Exponential functions
Power functions
Trigonometric and hyperbolic functions
Nearest integer floating-point
(C99) (C99) (C99)
(C23) (C23) (C23) (C23)
Floating-point manipulation
Narrowing operations
(C23)
(C23)
(C23)
(C23)
(C23)
(C23)
Quantum and quantum exponent
Decimal re-encoding functions
Total order and payload functions
Classification
Error and gamma functions
(C99)
(C99)
(C99)
(C99)
Types
Macro constants
Special floating-point values
Arguments and return values
Error handling
Fast operation indicators
定义于头文件 <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 起)
1-3) 计算除法运算 x / y 的浮点余数,实现方式与 remainder() 函数相同。此外,会将 x / y 的符号及最低三位有效比特存储至 quo 中,该信息足以确定结果在周期内的八分圆象限。
4) 类型泛型宏:若任何非指针参数具有类型 long double ,则调用 remquol 。否则,若任何非指针参数具有整数类型或具有类型 double ,则调用 remquo 。否则,调用 remquof

目录

参数

x, y - 浮点数值
quo - 指向整数值的指针,用于存储 x / y 的符号和部分位

返回值

若成功,则返回定义于 remainder 中的除法 x / y 的浮点余数,并在 * quo 中存储 x / y 的符号及至少三个最低有效位(形式上,存储值的符号与 x / y 的符号相同,其大小在模 2 n
意义下与 x / y 的整数商的大小同余,其中 n 是实现定义的整数且不小于 3 )。

如果 y 为零,则存储在 * quo 中的值是未指定的。

如果发生定义域错误,将返回一个由实现定义的值(在支持 NaN 的情况下返回 NaN)。

如果由于下溢发生范围错误,若支持次正规数则返回正确结果。

如果 y 为零但未发生定义域错误,则返回零。

错误处理

错误报告方式遵循 math_errhandling 中的规范。

y 为零时可能出现定义域错误。

如果实现支持 IEEE 浮点算术 (IEC 60559),

注释

POSIX 要求 x 为无穷大或 y 为零时发生定义域错误。

该函数在实现周期函数且周期可精确表示为浮点数值时非常有用:当计算 sin(πx) 中的极大值 x 时,直接调用 sin 可能导致较大误差,但若先使用 remquo 对函数参数进行归约,则可通过商值的低位确定周期内结果的符号和象限,同时利用余数进行高精度计算。

在某些平台上,该操作由硬件支持(例如,在英特尔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);
}
// 周期为2,值在(0;0.5)区间为正,(0.5;1.5)区间为负,(1.5,2)区间为正
double cos_pi_x_smart(double x)
{
    const double pi = acos(-1);
    int extremum;
    double rem = remquo(x, 1, &extremum);
    extremum = (unsigned)extremum % 2; // 保留1位以确定最近的极值点
    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));
    // 错误处理
    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函数(页码:236)
  • 7.22 泛型数学 <tgmath.h>(页码:335-337)
  • F.9.7.3 remquo函数(页码:465)

参考

计算整数除法的商和余数
(函数)
(C99) (C99)
计算浮点除法运算的余数
(函数)
计算浮点除法运算的有符号余数
(函数)
C++ 文档 for remquo