数学和浮点支持Math and floating-point support

通用 C 运行时库 (UCRT) 提供了许多整型和浮点数学库函数,包括 ISO C99 所需的所有函数。The Universal C Runtime library (UCRT) provides many integral and floating-point math library functions, including all of those required by ISO C99. 这些浮点函数的实现是为了平衡性能与正确性。The floating-point functions are implemented to balance performance with correctness. 因为产生正确舍入的结果可能成本过高,这些函数旨在有效地生成接近正确舍入结果的近似结果。Because producing the correctly rounded result may be prohibitively expensive, these functions are designed to efficiently produce a close approximation to the correctly rounded result. 在大多数情况下,虽然可能存在误差较大的情况,但生成的结果在正确舍入结果的 + /-1 ulp 范围内。In most cases, the result produced is within +/-1 ulp of the correctly rounded result, though there may be cases where there is greater inaccuracy.

许多浮点数学库函数具有不同 CPU 体系结构的不同实现。Many of the floating point math library functions have different implementations for different CPU architectures. 例如,相比 64 位 x64 CRT,32 位 x86 CRT 可能具有不同的实现。For example, the 32-bit x86 CRT may have a different implementation than the 64-bit x64 CRT. 此外,某些函数可能有适用于给定 CPU 体系结构的多个实现。In addition, some of the functions may have multiple implementations for a given CPU architecture. 在运行时动态地选择最有效的实现,具体取决于受 CPU 支持的指令集。The most efficient implementation is selected dynamically at run-time depending on the instruction sets supported by the CPU. 例如,在 32 位 x86 CRT 中,一些函数同时具有 x87 实现和 SSE2 实现。For example, in the 32-bit x86 CRT, some functions have both an x87 implementation and an SSE2 implementation. 在支持 SSE2 的 CPU 上运行时,使用速度更快的 SSE2 实现。When running on a CPU that supports SSE2, the faster SSE2 implementation is used. 在不支持 SSE2 的 CPU 上运行时,使用速度较慢的 x87 实现。When running on a CPU that does not support SSE2, the slower x87 implementation is used. 数学库函数的不同实现可能会使用不同的 CPU 指令和不同的算法来生成其结果,因此,这些函数可能会在各 CPU 中产生不同的结果。Because different implementations of the math library functions may use different CPU instructions and different algorithms to produce their results, the functions may produce different results across CPUs. 在大多数情况下,结果在正确舍入结果的 +/-1 ulp 范围内,但实际结果在各 CPU 中可能会有所不同。In most cases, the results are within +/-1 ulp of the correctly rounded result, but the actual results may vary across CPUs.

以前的 16 位版本 Microsoft C/C ++ 和 Microsoft Visual C++ 支持长双精度类型作为 80 位精度浮点数据类型。Previous 16-bit versions of Microsoft C/C++ and Microsoft Visual C++ supported the long double type as an 80-bit precision floating-point data type. 在更高版本的 Visual C++ 中,长双精度数据类型是与双精度类型相同的 64 位精度浮点数据类型。In later versions of Visual C++, the long double data type is a 64-bit precision floating-point data type identical to the double type. 编译器将长双精度数据类型和双精度数据类型视为不同类型,但长双精度函数与双精度函数相同。The compiler treats long double and double as distinct types, but the long double functions are identical to their double counterparts. CRT 为 ISO C99 源代码兼容性提供了数学函数的长双精度版本,但请注意,二进制表示形式可能不同于其他编译器。The CRT provides long double versions of the math functions for ISO C99 source code compatibility, but note that the binary representation may differ from other compilers.

受支持的数学和浮点例程Supported math and floating-point routines

例程所返回的值Routine 使用Use
abs、labs、llabs、_abs64abs, labs, llabs, _abs64 计算整数类型的绝对值Computes the absolute value of an integer type
acos、acosf、acoslacos, acosf, acosl 计算反余弦值Computes the arc cosine
acosh、acoshf、acoshlacosh, acoshf, acoshl 计算双曲反余弦值Computes the hyperbolic arc cosine
asin、asinf、asinlasin, asinf, asinl 计算反正弦值Computes the arc sine
asinh、asinhf、asinhlasinh, asinhf, asinhl 计算双曲反正弦值Computes the hyperbolic arc sine
atan、atanf、atanl、atan2、atan2f、atan2latan, atanf, atanl, atan2, atan2f, atan2l 计算反正切值Computes the arc tangent
atanh、atanhf、atanhlatanh, atanhf, atanhl 计算双曲反正切值Computes the hyperbolic arc tangent
_atodbl、_atodbl_l_atodbl, _atodbl_l 将特定于区域设置的字符串转换为双精度型Converts a locale-specific string to a double
atof、_atof_latof, _atof_l 将字符串转换为双精度型Converts a string to a double
_atoflt、_atoflt_l、_atoldbl、_atoldbl_l_atoflt, _atoflt_l, _atoldbl, _atoldbl_l 将特定于区域设置的字符串转换为浮点型或长双精度型Converts a locale-specific string to a float or long double
cbrt、cbrtf、cbrtlcbrt, cbrtf, cbrtl 计算立方根Computes the cube root
ceil、ceilf、ceillceil, ceilf, ceill 计算上限Computes the ceiling
_chgsign、_chgsignf、_chgsignl_chgsign, _chgsignf, _chgsignl 计算相反数Computes the additive inverse
_clear87、_clearfp_clear87, _clearfp 获取并清除浮点状态注册Gets and clears the floating-point status register
_control87、__control87_2、_controlfp_control87, __control87_2, _controlfp 获取并设置浮点控制字Gets and sets the floating-point control word
_controlfp_s_controlfp_s _controlfp 的安全版本Secure version of _controlfp
copysign、copysignf、copysignl、_copysign、_copysignf、_copysignlcopysign, copysignf, copysignl, _copysign, _copysignf, _copysignl 返回一个值,该值具有一个自变量的数值和另一个自变量的符号Returns a value that has the magnitude of one argument and the sign of another
cos、cosf、coslcos, cosf, cosl 计算正弦值Computes the sine
cosh、coshf、coshlcosh, coshf, coshl 计算双曲正弦值Computes the hyperbolic sine
div、ldiv、lldivdiv, ldiv, lldiv 计算两个整数值的商和余数Computes the quotient and the remainder of two integer values
_ecvtecvt_ecvt, ecvt 将双精度型转换为字符串Converts a double to a string
_ecvt_s_ecvt_s _ecvt 的安全版本Secure version of _ecvt
erf、erff、erflerf, erff, erfl 计算错误函数Computes the error function
erfc、erfcf、erfclerfc, erfcf, erfcl 计算互补错误函数Computes the complementary error function
exp、expf、explexp, expf, expl 计算指数 exComputes the exponential ex
exp2、exp2f、exp2lexp2, exp2f, exp2l 计算指数 2xComputes the exponential 2x
expm1、expm1f、expm1lexpm1, expm1f, expm1l 计算 ex-1Computes ex-1
fabs、fabsf、fabslfabs, fabsf, fabsl 计算浮点类型的绝对值Computes the absolute value of a floating-point type
_fcvtfcvt_fcvt, fcvt 将浮点数转换为字符串Converts a floating-point number to a string
_fcvt_s_fcvt_s _fcvt 的安全版本Secure version of _fcvt
fdim、fdimf、fdimlfdim, fdimf, fdiml 确定两个值之间的正数差Determines the positive difference between two values
feclearexceptfeclearexcept 清除指定的浮点异常Clears specified floating-point exceptions
fegetenvfegetenv 存储当前的浮点环境Stores the current floating-point environment
fegetexceptflagfegetexceptflag 获取指定的浮点异常状态Gets the specified floating-point exception status
fegetroundfegetround 获取浮点舍入模式Gets the floating-point rounding mode
feholdexceptfeholdexcept 设置不间断的浮点异常模式Sets non-stop floating-point exception mode
feraiseexceptferaiseexcept 引发指定的浮点异常Raises the specified floating-point exceptions
fesetenvfesetenv 设置当前的浮点环境Sets the current floating-point environment
fesetexceptflagfesetexceptflag 获取指定的浮点异常状态标志Sets the specified floating-point status flags
fesetroundfesetround 设置指定的浮点舍入标志Sets the specified floating-point rounding mode
fetestexceptfetestexcept 确定要设置的浮点异常状态标志Determines which floating-point exception status flags are set
feupdateenvfeupdateenv 还原浮点环境,然后引发以前的异常Restores a floating-point environment then raises previous exceptions
_finite、_finitef_finite, _finitef 确定某值是否有限Determines whether a value is finite
floor、floorf、floorlfloor, floorf, floorl 计算下限Computes the floor
fma、fmaf、fmalfma, fmaf, fmal 计算浮点混合乘加运算Computes a fused multiply-add
fmax、fmaxf、fmaxlfmax, fmaxf, fmaxl 计算参数的最大值Computes the maximum of the arguments
fmin、fminf、fminlfmin, fminf, fminl 计算参数的最小值Computes the minumum of the arguments
fmod、fmodf、fmodlfmod, fmodf, fmodl 计算浮点余数Computes the floating-point remainder
_fpclass、_fpclassf_fpclass, _fpclassf 返回浮点值的分类Returns the classification of a floating-point value
fpclassifyfpclassify 返回浮点值的分类Returns the classification of a floating-point value
_fpieee_flt_fpieee_flt 设置浮点异常的处理程序Sets a handler for floating-point exceptions
_fpreset_fpreset 重置浮点环境Resets the floating-point environment
frexp、frexpf、frexplfrexp, frexpf, frexpl 获取浮点数的尾数和指数Gets the mantissa and exponent of a floating-point number
_gcvtgcvt_gcvt, gcvt 将浮点数转换为字符串Converts a floating-point number to a string
_gcvt_s_gcvt_s _gcvt 的安全版本Secure version of _gcvt
_get_FMA3_enable、_set_FMA3_enable_get_FMA3_enable, _set_FMA3_enable 获取或设置用于 x64 上 FMA3 指令的标志Gets or sets a flag for use of FMA3 instructions on x64
hypot、hypotf、hypotl、_hypot、_hypotf、_hypotlhypot, hypotf, hypotl, _hypot, _hypotf, _hypotl 计算斜边Computes the hypotenuse
ilogb、ilogbf、ilogblilogb, ilogbf, ilogbl 计算以整数 2 为底的指数Computes the integer base-2 exponent
imaxabsimaxabs 计算整数类型的绝对值Computes the absolute value of an integer type
imaxdivimaxdiv 计算两个整数值的商和余数Computes the quotient and the remainder of two integer values
isnan、_isnan、_isnanfisnan, _isnan, _isnanf 测试 NaN 的浮点值Tests a floating-point value for NaN
_j0、_j1、_jn_j0, _j1, _jn 计算贝塞尔函数Computes the Bessel function
ldexp、ldexpf、ldexplldexp, ldexpf, ldexpl 计算 x*2nComputes x*2n
lgamma、lgammaf、lgammallgamma, lgammaf, lgammal 计算 gamma 函数的绝对值的自然对数Computes the natural logarithm of the absolute value of the gamma function
llrint、llrintf、llrintlllrint, llrintf, llrintl 将浮点值舍入为最接近的超长值Rounds a floating-point value to the nearest long long value
llround、llroundf、llroundlllround, llroundf, llroundl 将浮点值舍入为最接近的超长值Rounds a floating-point value to the nearest long long value
log、logf、logl、log10、log10f、log10llog, logf, logl, log10, log10f, log10l 计算自然对数或以 10 为底的对数Computes the natural or base-10 logarithm
log1p、log1pf、log1pllog1p, log1pf, log1pl 计算 1+x 的自然对数Computes the natural logarithm of 1+x
log2、log2f、log2llog2, log2f, log2l 计算以 2 为底的对数Computes the base-2 logarithm
logb、logbf、logbl、_logb、_logbflogb, logbf, logbl, _logb, _logbf 返回浮点值的指数Returns the exponent of a floating-point value
lrint、lrintf、lrintllrint, lrintf, lrintl 将浮点值舍入为最接近的长值Rounds a floating-point value to the nearest long value
_lrotl、_lrotr_lrotl, _lrotr 向左或向右旋转整数值Rotates an integer value left or right
lround、lroundf、lroundllround, lroundf, lroundl 将浮点值舍入为最接近的长值Rounds a floating-point value to the nearest long value
_matherr_matherr 默认数学错误处理程序The default math error handler
__max__max 返回两个值中的较大者的宏Macro that returns the larger of two values
__min__min 返回两个值中的较小者的宏Macro that returns the smaller of two values
modf、modff、modflmodf, modff, modfl 将浮点值拆分为小数部分和整数部分Splits a floating-point value into fractional and integer parts
nan、nanf、nanlnan, nanf, nanl 返回 quiet NaN 值Returns a quiet NaN value
nearbyint、nearbyintf、nearbyintlnearbyint, nearbyintf, nearbyintl 返回舍入后的值Returns the rounded value
nextafter、nextafterf、nextafterl、_nextafter、_nextafterfnextafter, nextafterf, nextafterl, _nextafter, _nextafterf 返回下一个可表示的浮点值Returns the next representable floating-point value
nexttoward、nexttowardf、nexttowardlnexttoward, nexttowardf, nexttowardl 返回下一个可表示的浮点值Returns the next representable floating-point value
pow、powf、powlpow, powf, powl 返回 xy 的值Returns the value of xy
remainder、remainderf、remainderlremainder, remainderf, remainderl 计算两个浮点值的商的余数Computes the remainder of the quotient of two floating-point values
remquo、remquof、remquolremquo, remquof, remquol 计算两个整数值的余数Computes the remainder of two integer values
rint、rintf、rintlrint, rintf, rintl 舍入浮点值Rounds a floating-point value
_rotl、_rotl64、_rotr、_rotr64_rotl, _rotl64, _rotr, _rotr64 旋转整数类型中的位Rotates bits in integer types
round、roundf、roundlround, roundf, roundl 舍入浮点值Rounds a floating-point value
_scalb、_scalbf_scalb, _scalbf 按 2 的幂缩放参数Scales argument by a power of 2
scalbn、scalbnf、scalbnl、scalbln、scalblnf、scalblnlscalbn, scalbnf, scalbnl, scalbln, scalblnf, scalblnl 将浮点数乘以 FLT_RADIX 的整数幂Multiplies a floating-point number by an integral power of FLT_RADIX
_set_controlfp_set_controlfp 设置浮点控制字Sets the floating-point control word
_set_SSE2_enable_set_SSE2_enable 启用或禁用 SSE2 指令Enables or disables SSE2 instructions
sin、sinf、sinlsin, sinf, sinl 计算正弦值Computes the sine
sinh、sinhf、sinhlsinh, sinhf, sinhl 计算双曲正弦值Computes the hyperbolic sine
sqrt、sqrtf、sqrtlsqrt, sqrtf, sqrtl 计算平方根Computes the square root
_status87、_statusfp、_statusfp2_status87, _statusfp, _statusfp2 获取浮点状态字Gets the floating-point status word
strtof、_strtof_lstrtof, _strtof_l 将字符串转换为浮点型Converts a string to a float
strtold、_strtold_lstrtold, _strtold_l 将字符串转换为长双精度型Converts a string to a long double
tan、tanf、tanltan, tanf, tanl 计算正切值Computes the tangent
tanh、tanhf、tanhltanh, tanhf, tanhl 计算双曲正切值Computes the hyperbolic tangent
tgamma、tgammaf、tgammaltgamma, tgammaf, tgammal 计算 gamma 函数Computes the gamma function
trunc、truncf、truncltrunc, truncf, truncl 截断小数部分Truncates the fractional part
_wtof、_wtof_l_wtof, _wtof_l 将宽字符串转换为双精度型Converts a wide string to a double
_y0、_y1、_yn_y0, _y1, _yn 计算贝塞尔函数Computes the Bessel function

请参阅See also

按类别分的通用 C 运行时例程Universal C runtime routines by category