?
This document uses PHP Chinese website manual Release
在頭文件<stdlib.h>中定義 | ||
---|---|---|
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 <tgmath.h> | ||
#define remainder( x, y ) | (4) | (since C99) |
1-3)計算浮點除法運算的IEEE余數(shù)x/y
。
4)類型 - 通用宏:如果任何參數(shù)具有類型long double
,remainderl
則被調(diào)用。否則,如果任何參數(shù)具有整數(shù)類型或具有類型double
,remainder
則被調(diào)用。否則,remainderf
被調(diào)用。
x/y
由該函數(shù)計算的除法操作的IEEE浮點余數(shù)恰好是該值x - n*y
,其中該值n
是與精確值最接近的整數(shù)值x/y
。當(dāng)| nx / y |時 =?,該值n
被選為偶數(shù)。
與之相反fmod()
,返回的值不能保證具有相同的符號x
。
如果返回的值是0
,它將具有與。相同的符號x
。
x,y | - | 浮點值 |
---|
如果成功,則返回x/y
如上定義的除法的IEEE浮點余數(shù)。
如果發(fā)生域錯誤,則返回實現(xiàn)定義的值(NaN,如果支持)。
如果由于下溢而發(fā)生范圍錯誤,則返回正確的結(jié)果。
如果y
為零,但域錯誤不會發(fā)生,則返回零。
按照math_errhandling中的指定報告錯誤。
如果y
為零,則可能會出現(xiàn)域錯誤。
如果實現(xiàn)支持IEEE浮點運算(IEC 60559),
當(dāng)前的舍入模式不起作用。
FE_INEXACT
永遠不會升起,結(jié)果總是確切的。
如果x
是±∞并且y
不是NaN,則返回并FE_INVALID
提升NaN
如果y
是±0并且x
不是NaN,則返回并FE_INVALID
提升NaN
如果任一參數(shù)是NaN,則返回NaN
如果x
是無限的或者y
是零,POSIX要求發(fā)生域錯誤。
fmod
,但remainder
對于將浮點類型靜默包裝為無符號整數(shù)類型非常有用:(0.0
<=
(y =
fmod(rint(x), 65536.0))
? y :
65536.0
+ y)
處于范圍內(nèi)[-0.0 .. 65535.0]
,該范圍與范圍相對應(yīng)unsigned short
,但remainder(rint(x), 65536.0
位于范圍[-32767.0, +32768.0]
之外的范圍內(nèi)signed short
。
#include <stdio.h>#include <math.h>#include <fenv.h> #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.9remainder(-5.1, +3.0) = 0.9remainder(+5.1, -3.0) = -0.9remainder(-5.1, -3.0) = 0.9remainder(+0.0, 1.0) = 0.0remainder(-0.0, 1.0) = -0.0remainder(+5.1, Inf) = 5.1remainder(+5.1, 0) = -nan FE_INVALID raised
C11標(biāo)準(zhǔn)(ISO / IEC 9899:2011):
7.12.10.2其余職能(p:254-255)
7.25類型通用數(shù)學(xué)<tgmath.h>(p:373-375)
F.10.7.2其余功能(p:529)
C99標(biāo)準(zhǔn)(ISO / IEC 9899:1999):
7.12.10.2其余職能(p:235)
7.22類型通用數(shù)學(xué)<tgmath.h>(p:335-337)
F.9.7.2其余功能(p:465)