c语言remainder函数,remainder

remainder

在头文件中定义

float remainderf( float x, float y );

(1)

(since C99)

double remainder( double x, double y );

(2)

(since C99)

long double remainderl( long double x, long double y );

(3)

(since C99)

Defined in header

#define remainder( x, y )

(4)

(since C99)

1-3)计算浮点除法运算的IEEE余数x/y。

4)类型 - 通用宏:如果任何参数具有类型long double,remainderl则被调用。否则,如果任何参数具有整数类型或具有类型double,remainder则被调用。否则,remainderf被调用。

x/y由该函数计算的除法操作的IEEE浮点余数恰好是该值x - n*y,其中该值n是与精确值最接近的整数值x/y。当| nx / y |时 =½,该值n被选为偶数。

与之相反fmod(),返回的值不能保证具有相同的符号x。

如果返回的值是0,它将具有与。相同的符号x。

参数

x,y

-

浮点值

返回值

如果成功,则返回x/y如上定义的除法的IEEE浮点余数。

如果发生域错误,则返回实现定义的值(NaN,如果支持)。

如果由于下溢而发生范围错误,则返回正确的结果。

如果y为零,但域错误不会发生,则返回零。

错误处理

按照math_errhandling中的指定报告错误。

如果y为零,则可能会出现域错误。

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

当前的舍入模式不起作用。

FE_INEXACT 永远不会升起,结果总是确切的。

如果x是±∞并且y不是NaN,则返回并FE_INVALID提升NaN

如果y是±0并且x不是NaN,则返回并FE_INVALID提升NaN

如果任一参数是NaN,则返回NaN

笔记

如果x是无限的或者y是零,POSIX要求发生域错误。

fmod,但remainder对于将浮点类型静默包装为无符号整数类型非常有用:(0.0 <= (y = fmod(rint(x), 65536.0)) ? y : 65536.0 + y)处于范围内[-0.0 .. 65535.0],该范围与范围相对应unsigned short,但remainder(rint(x), 65536.0位于范围[-32767.0, +32768.0]之外的范围内signed short。

#include

#include

#include

#pragma STDC FENV_ACCESS ON

int main(void)

{

printf("remainder(+5.1, +3.0) = %.1f\n", remainder(5.1,3));

printf("remainder(-5.1, +3.0) = %.1f\n", remainder(-5.1,3));

printf("remainder(+5.1, -3.0) = %.1f\n", remainder(5.1,-3));

printf("remainder(-5.1, -3.0) = %.1f\n", remainder(-5.1,-3));

// special values

printf("remainder(-0.0, 1.0) = %.1f\n", remainder(-0.0, 1));

printf("remainder(+5.1, Inf) = %.1f\n", remainder(5.1, INFINITY));

// error handling

feclearexcept(FE_ALL_EXCEPT);

printf("remainder(+5.1, 0) = %.1f\n", remainder(5.1, 0));

if(fetestexcept(FE_INVALID)) puts(" FE_INVALID raised");

}

输出:

remainder(+5.1, +3.0) = -0.9

remainder(-5.1, +3.0) = 0.9

remainder(+5.1, -3.0) = -0.9

remainder(-5.1, -3.0) = 0.9

remainder(+0.0, 1.0) = 0.0

remainder(-0.0, 1.0) = -0.0

remainder(+5.1, Inf) = 5.1

remainder(+5.1, 0) = -nan

FE_INVALID raised

参考

C11标准(ISO / IEC 9899:2011): 7.12.10.2其余职能(p:254-255)

7.25类型通用数学(p:373-375)

F.10.7.2其余功能(p:529)

C99标准(ISO / IEC 9899:1999): 7.12.10.2其余职能(p:235)

7.22类型通用数学(p:335-337)

F.9.7.2其余功能(p:465)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值