Стандарт языка Си С99 TC (1113411), страница 78
Текст из файла (страница 78)
This transformation,which would be desirable if extra code were required tocause the ‘‘invalid’’ floating-point exception forunordered cases, could be performed provided the stateof the FENV_ACCESS pragma is ‘‘off’’.The sense of relational operators shall be maintained. This includes handling unorderedcases as expressed by the source code.2EXAMPLE// calls g and raises ‘‘invalid’’ if a and b are unorderedif (a < b)f();elseg();is not equivalent to// calls f and raises ‘‘invalid’’ if a and b are unorderedif (a >= b)g();elsef();nor to// calls f without raising ‘‘invalid’’ if a and b are unorderedif (isgreaterequal(a,b))g();elsef();nor, unless the state of the FENV_ACCESS pragma is ‘‘off’’, to§F.8.3IEC 60559 floating-point arithmetic453ISO/IEC 9899:TC3Committee Draft — Septermber 7, 2007WG14/N1256// calls g without raising ‘‘invalid’’ if a and b are unorderedif (isless(a,b))f();elseg();but is equivalent toif (!(a < b))g();elsef();F.8.4 Constant arithmetic1The implementation shall honor floating-point exceptions raised by execution-timeconstant arithmetic wherever the state of the FENV_ACCESS pragma is ‘‘on’’.
(See F.7.4and F.7.5.) An operation on constants that raises no floating-point exception can befolded during translation, except, if the state of the FENV_ACCESS pragma is ‘‘on’’, afurther check is required to assure that changing the rounding direction to downward doesnot alter the sign of the result,319) and implementations that support dynamic roundingprecision modes shall assure further that the result of the operation raises no floatingpoint exception when converted to the semantic type of the operation.F.9 Mathematics <math.h>1This subclause contains specifications of <math.h> facilities that are particularly suitedfor IEC 60559 implementations.2The Standard C macro HUGE_VAL and its float and long double analogs,HUGE_VALF and HUGE_VALL, expand to expressions whose values are positiveinfinities.3Special cases for functions in <math.h> are covered directly or indirectly byIEC 60559.
The functions that IEC 60559 specifies directly are identified in F.3. Theother functions in <math.h> treat infinities, NaNs, signed zeros, subnormals, and(provided the state of the FENV_ACCESS pragma is ‘‘on’’) the floating-point status flagsin a manner consistent with the basic arithmetic operations covered by IEC 60559.4The expression math_errhandling & MATH_ERREXCEPT shall evaluate to anonzero value.5The ‘‘invalid’’ and ‘‘divide-by-zero’’ floating-point exceptions are raised as specified insubsequent subclauses of this annex.6The ‘‘overflow’’ floating-point exception is raised whenever an infinity — or, because ofrounding direction, a maximal-magnitude finite number — is returned in lieu of a value319) 0 − 0 yields −0 instead of +0 just when the rounding direction is downward.454IEC 60559 floating-point arithmetic§F.9WG14/N1256Committee Draft — Septermber 7, 2007ISO/IEC 9899:TC3whose magnitude is too large.7The ‘‘underflow’’ floating-point exception is raised whenever a result is tiny (essentiallysubnormal or zero) and suffers loss of accuracy.320)8Whether or when library functions raise the ‘‘inexact’’ floating-point exception isunspecified, unless explicitly specified otherwise.9Whether or when library functions raise an undeserved ‘‘underflow’’ floating-pointexception is unspecified.321) Otherwise, as implied by F.7.6, the <math.h> functions donot raise spurious floating-point exceptions (detectable by the user), other than the‘‘inexact’’ floating-point exception.10Whether the functions honor the rounding direction mode is implementation-defined,unless explicitly specified otherwise.11Functions with a NaN argument return a NaN result and raise no floating-point exception,except where stated otherwise.12The specifications in the following subclauses append to the definitions in <math.h>.For families of functions, the specifications apply to all of the functions even though onlythe principal function is shown.
Unless otherwise specified, where the symbol ‘‘±’’occurs in both an argument and the result, the result has the same sign as the argument.Recommended practice13If a function with one or more NaN arguments returns a NaN result, the result should bethe same as one of the NaN arguments (after possible type conversion), except perhapsfor the sign.F.9.1 Trigonometric functionsF.9.1.1 The acos functions1— acos(1) returns +0.— acos(x) returns a NaN and raises the ‘‘invalid’’ floating-point exception for| x | > 1.320) IEC 60559 allows different definitions of underflow. They all result in the same values, but differ onwhen the floating-point exception is raised.321) It is intended that undeserved ‘‘underflow’’ and ‘‘inexact’’ floating-point exceptions are raised only ifavoiding them would be too costly.§F.9.1.1IEC 60559 floating-point arithmetic455ISO/IEC 9899:TC3Committee Draft — Septermber 7, 2007WG14/N1256F.9.1.2 The asin functions1— asin(±0) returns ±0.— asin(x) returns a NaN and raises the ‘‘invalid’’ floating-point exception for| x | > 1.F.9.1.3 The atan functions1— atan(±0) returns ±0.— atan(±∞) returns ±π /2.F.9.1.4 The atan2 functions1— atan2(±0, −0) returns ±π .322)— atan2(±0, +0) returns ±0.— atan2(±0, x) returns ±π for x < 0.— atan2(±0, x) returns ±0 for x > 0.— atan2(y, ±0) returns −π /2 for y < 0.— atan2(y, ±0) returns π /2 for y > 0.— atan2(±y, −∞) returns ±π for finite y > 0.— atan2(±y, +∞) returns ±0 for finite y > 0.— atan2(±∞, x) returns ±π /2 for finite x.— atan2(±∞, −∞) returns ±3π /4.— atan2(±∞, +∞) returns ±π /4.F.9.1.5 The cos functions1— cos(±0) returns 1.— cos(±∞) returns a NaN and raises the ‘‘invalid’’ floating-point exception.F.9.1.6 The sin functions1— sin(±0) returns ±0.— sin(±∞) returns a NaN and raises the ‘‘invalid’’ floating-point exception.322) atan2(0, 0) does not raise the ‘‘invalid’’ floating-point exception, nor does atan2( y ,the ‘‘divide-by-zero’’ floating-point exception.456IEC 60559 floating-point arithmetic0) raise§F.9.1.6WG14/N1256Committee Draft — Septermber 7, 2007ISO/IEC 9899:TC3F.9.1.7 The tan functions1— tan(±0) returns ±0.— tan(±∞) returns a NaN and raises the ‘‘invalid’’ floating-point exception.F.9.2 Hyperbolic functionsF.9.2.1 The acosh functions1— acosh(1) returns +0.— acosh(x) returns a NaN and raises the ‘‘invalid’’ floating-point exception for x < 1.— acosh(+∞) returns +∞.F.9.2.2 The asinh functions1— asinh(±0) returns ±0.— asinh(±∞) returns ±∞.F.9.2.3 The atanh functions1— atanh(±0) returns ±0.— atanh(±1) returns ±∞ and raises the ‘‘divide-by-zero’’ floating-point exception.— atanh(x) returns a NaN and raises the ‘‘invalid’’ floating-point exception for| x | > 1.F.9.2.4 The cosh functions1— cosh(±0) returns 1.— cosh(±∞) returns +∞.F.9.2.5 The sinh functions1— sinh(±0) returns ±0.— sinh(±∞) returns ±∞.F.9.2.6 The tanh functions1— tanh(±0) returns ±0.— tanh(±∞) returns ±1.§F.9.2.6IEC 60559 floating-point arithmetic457ISO/IEC 9899:TC3Committee Draft — Septermber 7, 2007WG14/N1256F.9.3 Exponential and logarithmic functionsF.9.3.1 The exp functions1— exp(±0) returns 1.— exp(−∞) returns +0.— exp(+∞) returns +∞.F.9.3.2 The exp2 functions1— exp2(±0) returns 1.— exp2(−∞) returns +0.— exp2(+∞) returns +∞.F.9.3.3 The expm1 functions1— expm1(±0) returns ±0.— expm1(−∞) returns −1.— expm1(+∞) returns +∞.F.9.3.4 The frexp functions1— frexp(±0, exp) returns ±0, and stores 0 in the object pointed to by exp.— frexp(±∞, exp) returns ±∞, and stores an unspecified value in the objectpointed to by exp.— frexp(NaN, exp) stores an unspecified value in the object pointed to by exp(and returns a NaN).2frexp raises no floating-point exceptions.3On a binary system, the body of the frexp function might be{*exp = (value == 0) ? 0 : (int)(1 + logb(value));return scalbn(value, -(*exp));}F.9.3.5 The ilogb functions1If the correct result is outside the range of the return type, the numeric result isunspecified and the ‘‘invalid’’ floating-point exception is raised.458IEC 60559 floating-point arithmetic§F.9.3.5WG14/N1256Committee Draft — Septermber 7, 2007ISO/IEC 9899:TC3F.9.3.6 The ldexp functions1On a binary system, ldexp(x, exp) is equivalent to scalbn(x, exp).F.9.3.7 The log functions1— log(±0) returns −∞ and raises the ‘‘divide-by-zero’’ floating-point exception.— log(1) returns +0.— log(x) returns a NaN and raises the ‘‘invalid’’ floating-point exception for x < 0.— log(+∞) returns +∞.F.9.3.8 The log10 functions1— log10(±0) returns −∞ and raises the ‘‘divide-by-zero’’ floating-point exception.— log10(1) returns +0.— log10(x) returns a NaN and raises the ‘‘invalid’’ floating-point exception for x < 0.— log10(+∞) returns +∞.F.9.3.9 The log1p functions1— log1p(±0) returns ±0.— log1p(−1) returns −∞ and raises the ‘‘divide-by-zero’’ floating-point exception.— log1p(x) returns a NaN and raises the ‘‘invalid’’ floating-point exception forx < −1.— log1p(+∞) returns +∞.F.9.3.10 The log2 functions1— log2(±0) returns −∞ and raises the ‘‘divide-by-zero’’ floating-point exception.— log2(1) returns +0.— log2(x) returns a NaN and raises the ‘‘invalid’’ floating-point exception for x < 0.— log2(+∞) returns +∞.F.9.3.11 The logb functions1— logb(±0) returns −∞ and raises the ‘‘divide-by-zero’’ floating-point exception.— logb(±∞) returns +∞.§F.9.3.11IEC 60559 floating-point arithmetic459ISO/IEC 9899:TC3Committee Draft — Septermber 7, 2007WG14/N1256F.9.3.12 The modf functions1— modf(±x, iptr) returns a result with the same sign as x.— modf(±∞, iptr) returns ±0 and stores ±∞ in the object pointed to by iptr.— modf(NaN, iptr) stores a NaN in the object pointed to by iptr (and returns aNaN).2modf behaves as though implemented by#include <math.h>#include <fenv.h>#pragma STDC FENV_ACCESS ONdouble modf(double value, double *iptr){int save_round = fegetround();fesetround(FE_TOWARDZERO);*iptr = nearbyint(value);fesetround(save_round);return copysign(isinf(value) ? 0.0 :value - (*iptr), value);}F.9.3.13 The scalbn and scalbln functions1— scalbn(±0, n) returns ±0.— scalbn(x, 0) returns x.— scalbn(±∞, n) returns ±∞.F.9.4 Power and absolute value functionsF.9.4.1 The cbrt functions1— cbrt(±0) returns ±0.— cbrt(±∞) returns ±∞.F.9.4.2 The fabs functions1— fabs(±0) returns +0.— fabs(±∞) returns +∞.460IEC 60559 floating-point arithmetic§F.9.4.2WG14/N1256Committee Draft — Septermber 7, 2007ISO/IEC 9899:TC3F.9.4.3 The hypot functions1— hypot(x, y), hypot(y, x), and hypot(x, −y) are equivalent.— hypot(x, ±0) is equivalent to fabs(x).— hypot(±∞, y) returns +∞, even if y is a NaN.F.9.4.4 The pow functions1— pow(±0, y) returns ±∞ and raises the ‘‘divide-by-zero’’ floating-point exceptionfor y an odd integer < 0.— pow(±0, y) returns +∞ and raises the ‘‘divide-by-zero’’ floating-point exceptionfor y < 0 and not an odd integer.— pow(±0, y) returns ±0 for y an odd integer > 0.— pow(±0, y) returns +0 for y > 0 and not an odd integer.— pow(−1, ±∞) returns 1.— pow(+1, y) returns 1 for any y, even a NaN.— pow(x, ±0) returns 1 for any x, even a NaN.— pow(x, y) returns a NaN and raises the ‘‘invalid’’ floating-point exception forfinite x < 0 and finite non-integer y.— pow(x, −∞) returns +∞ for | x | < 1.— pow(x, −∞) returns +0 for | x | > 1.— pow(x, +∞) returns +0 for | x | < 1.— pow(x, +∞) returns +∞ for | x | > 1.— pow(−∞, y) returns −0 for y an odd integer < 0.— pow(−∞, y) returns +0 for y < 0 and not an odd integer.— pow(−∞, y) returns −∞ for y an odd integer > 0.— pow(−∞, y) returns +∞ for y > 0 and not an odd integer.— pow(+∞, y) returns +0 for y < 0.— pow(+∞, y) returns +∞ for y > 0.§F.9.4.4IEC 60559 floating-point arithmetic461ISO/IEC 9899:TC3Committee Draft — Septermber 7, 2007WG14/N1256F.9.4.5 The sqrt functions1sqrt is fully specified as a basic arithmetic operation in IEC 60559.F.9.5 Error and gamma functionsF.9.5.1 The erf functions1— erf(±0) returns ±0.— erf(±∞) returns ±1.F.9.5.2 The erfc functions1— erfc(−∞) returns 2.— erfc(+∞) returns +0.F.9.5.3 The lgamma functions1— lgamma(1) returns +0.— lgamma(2) returns +0.— lgamma(x) returns +∞ and raises the ‘‘divide-by-zero’’ floating-point exception forx a negative integer or zero.— lgamma(−∞) returns +∞.— lgamma(+∞) returns +∞.F.9.5.4 The tgamma functions1— tgamma(±0) returns ±∞ and raises the ‘‘divide-by-zero’’ floating-point exception.— tgamma(x) returns a NaN and raises the ‘‘invalid’’ floating-point exception for x anegative integer.— tgamma(−∞) returns a NaN and raises the ‘‘invalid’’ floating-point exception.— tgamma(+∞) returns +∞.F.9.6 Nearest integer functionsF.9.6.1 The ceil functions1— ceil(±0) returns ±0.— ceil(±∞) returns ±∞.2The double version of ceil behaves as though implemented by462IEC 60559 floating-point arithmetic§F.9.6.1WG14/N1256Committee Draft — Septermber 7, 2007ISO/IEC 9899:TC3#include <math.h>#include <fenv.h>#pragma STDC FENV_ACCESS ONdouble ceil(double x){double result;int save_round = fegetround();fesetround(FE_UPWARD);result = rint(x); // or nearbyint instead of rintfesetround(save_round);return result;}F.9.6.2 The floor functions1— floor(±0) returns ±0.— floor(±∞) returns ±∞.2See the sample implementation for ceil in F.9.6.1.F.9.6.3 The nearbyint functions1The nearbyint functions use IEC 60559 rounding according to the current roundingdirection.