Edit

Share via


Math and floating-point support

The Universal C Runtime library (UCRT) provides many integral and floating-point math library functions, including all of the functions 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. In most cases, the result produced is within +/-1 ULP (unit of least precision) of the correctly rounded result, though there may be cases where there's greater inaccuracy.

For ISO C Standard 11 (C11) and later, the <tgmath.h> header, in addition to including <math.h> and <complex.h>, provides macros that invoke a corresponding math function based on the types of the parameters. See Type-generic math for details.

Many of the floating point math library functions have different implementations for different CPU architectures. For example, the 32-bit x86 CRT may have a different implementation than the 64-bit x64 CRT. In addition, some of the functions may have multiple implementations for a given CPU architecture. The most efficient implementation is selected dynamically at run-time depending on the instruction sets supported by the CPU. For example, in the 32-bit x86 CRT, some functions have both an x87 implementation and an SSE2 implementation. When running on a CPU that supports SSE2, the faster SSE2 implementation is used. When running on a CPU that doesn't support SSE2, the slower x87 implementation is used. 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. In most cases, the results are within +/-1 ULP of the correctly rounded result, but the actual results may vary across CPUs.

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. 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. 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, _abs64 Computes the absolute value of an integer type
acos, acosf, acosl Computes the arc cosine
acosh, acoshf, acoshl Computes the hyperbolic arc cosine
asin, asinf, asinl Computes the arc sine
asinh, asinhf, asinhl Computes the hyperbolic arc sine
atan, atanf, atanl, atan2, atan2f, atan2l Computes the arc tangent
atanh, atanhf, atanhl Computes the hyperbolic arc tangent
_atodbl, _atodbl_l Converts a locale-specific string to a double
atof, _atof_l Converts a string to a double
_atoflt, _atoflt_l, _atoldbl, _atoldbl_l Converts a locale-specific string to a float or long double
cbrt, cbrtf, cbrtl Computes the cube root
ceil, ceilf, ceill Computes the ceiling
_chgsign, _chgsignf, _chgsignl Computes the additive inverse
_clear87, _clearfp Gets and clears the floating-point status register
_control87, _controlfp, __control87_2 Gets and sets the floating-point control word
_controlfp_s Secure version of _controlfp
copysign, copysignf, copysignl, _copysign, _copysignf, _copysignl Returns a value that has the magnitude of one argument and the sign of another
cos, cosf, cosl Computes the sine
cosh, coshf, coshl Computes the hyperbolic sine
div, ldiv, lldiv Computes the quotient and the remainder of two integer values
_ecvt, ecvt Converts a double to a string
_ecvt_s Secure version of _ecvt
erf, erff, erfl Computes the error function
erfc, erfcf, erfcl Computes the complementary error function
exp, expf, expl Computes the exponential ex
exp2, exp2f, exp2l Computes the exponential 2x
expm1, expm1f, expm1l Computes ex-1
fabs, fabsf, fabsl Computes the absolute value of a floating-point type
_fcvt, fcvt Converts a floating-point number to a string
_fcvt_s Secure version of _fcvt
fdim, fdimf, fdiml Determines the positive difference between two values
feclearexcept Clears specified floating-point exceptions
fegetenv Stores the current floating-point environment
fegetexceptflag Gets the specified floating-point exception status
fegetround Gets the floating-point rounding mode
feholdexcept Sets non-stop floating-point exception mode
feraiseexcept Raises the specified floating-point exceptions
fesetenv Sets the current floating-point environment
fesetexceptflag Sets the specified floating-point status flags
fesetround Sets the specified floating-point rounding mode
fetestexcept Determines which floating-point exception status flags are set
feupdateenv Restores a floating-point environment then raises previous exceptions
floor, floorf, floorl Computes the floor
fma, fmaf, fmal Computes a fused multiply-add
fmax, fmaxf, fmaxl Computes the maximum of the arguments
fmin, fminf, fminl Computes the minimum of the arguments
fmod, fmodf, fmodl Computes the floating-point remainder
_fpclass, _fpclassf Returns the classification of a floating-point value
fpclassify Returns the classification of a floating-point value
_fpieee_flt Sets a handler for floating-point exceptions
_fpreset Resets the floating-point environment
frexp, frexpf, frexpl Gets the mantissa and exponent of a floating-point number
_gcvt, gcvt Converts a floating-point number to a string
_gcvt_s Secure version of _gcvt
_get_FMA3_enable, _set_FMA3_enable Gets or sets a flag for use of FMA3 instructions on x64
hypot, hypotf, hypotl, _hypot, _hypotf, _hypotl Computes the hypotenuse
ilogb, ilogbf, ilogbl Computes the integer base-2 exponent
imaxabs Computes the absolute value of an integer type
imaxdiv Computes the quotient and the remainder of two integer values
isfinite, _finite, _finitef Determines whether a value is finite
isgreater, isgreaterequal, isless, islessequal, islessgreater, isunordered Compare the order of two floating point values
isinf Determines whether a floating-point value is infinite
isnan, _isnan, _isnanf Tests a floating-point value for NaN
isnormal Tests whether a floating-point value is both finite and not subnormal
_j0, _j1, _jn Computes the Bessel function
ldexp, ldexpf, ldexpl Computes x*2n
lgamma, lgammaf, lgammal Computes the natural logarithm of the absolute value of the gamma function
llrint, llrintf, llrintl Rounds a floating-point value to the nearest long long value
llround, llroundf, llroundl Rounds a floating-point value to the nearest long long value
log, logf, logl, log10, log10f, log10l Computes the natural or base-10 logarithm
log1p, log1pf, log1pl Computes the natural logarithm of 1+x
log2, log2f, log2l Computes the base-2 logarithm
logb, logbf, logbl, _logb, _logbf Returns the exponent of a floating-point value
lrint, lrintf, lrintl Rounds a floating-point value to the nearest long value
_lrotl, _lrotr Rotates an integer value left or right
lround, lroundf, lroundl Rounds a floating-point value to the nearest long value
_matherr The default math error handler
__max Macro that returns the larger of two values
__min Macro that returns the smaller of two values
modf, modff, modfl Splits a floating-point value into fractional and integer parts
nan, nanf, nanl Returns a quiet NaN value
nearbyint, nearbyintf, nearbyintl Returns the rounded value
nextafter, nextafterf, nextafterl, _nextafter, _nextafterf Returns the next representable floating-point value
nexttoward, nexttowardf, nexttowardl Returns the next representable floating-point value
pow, powf, powl Returns the value of xy
remainder, remainderf, remainderl Computes the remainder of the quotient of two floating-point values
remquo, remquof, remquol Computes the remainder of two integer values
rint, rintf, rintl Rounds a floating-point value
_rotl, _rotl64, _rotr, _rotr64 Rotates bits in integer types
round, roundf, roundl Rounds a floating-point value
_scalb, _scalbf Scales argument by a power of 2
scalbn, scalbnf, scalbnl, scalbln, scalblnf, scalblnl Multiplies a floating-point number by an integral power of FLT_RADIX
_set_controlfp Sets the floating-point control word
_set_SSE2_enable Enables or disables SSE2 instructions
signbit Tests the sign bit of a floating-point value
sin, sinf, sinl Computes the sine
sinh, sinhf, sinhl Computes the hyperbolic sine
sqrt, sqrtf, sqrtl Computes the square root
_status87, _statusfp, _statusfp2 Gets the floating-point status word
strtof, _strtof_l Converts a string to a float
strtold, _strtold_l Converts a string to a long double
tan, tanf, tanl Computes the tangent
tanh, tanhf, tanhl Computes the hyperbolic tangent
tgamma, tgammaf, tgammal Computes the gamma function
trunc, truncf, truncl Truncates the fractional part
_wtof, _wtof_l Converts a wide string to a double
_y0, _y1, _yn Computes the Bessel function

See also

Universal C runtime routines by category
Floating-point primitives