Skip to content

Latest commit

 

History

History
135 lines (126 loc) · 15.2 KB

floating-point-support.md

File metadata and controls

135 lines (126 loc) · 15.2 KB
titledescriptionms.datef1_keywordshelpviewer_keywords
Math and floating-point support
Describes floating-point support in the Microsoft Universal C Runtime library (UCRT)
9/14/2020
c.math
floating-point numbers, math routines
math routines
floating-point numbers

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.

Newer versions of the UCRT might improve the precision and accuracy of the floating-point math library functions. Since the UCRT is part of the Windows operating system, you might get different results for these functions on different operating system versions or between debug and release builds. Although it is not recommended, you can statically link to the UCRT to guarantee consistent results if you need these functions will produce identical results everywhere.

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

RoutineUse
abs, labs, llabs, _abs64Computes the absolute value of an integer type
acos, acosf, acoslComputes the arc cosine
acosh, acoshf, acoshlComputes the hyperbolic arc cosine
asin, asinf, asinlComputes the arc sine
asinh, asinhf, asinhlComputes the hyperbolic arc sine
atan, atanf, atanl, atan2, atan2f, atan2lComputes the arc tangent
atanh, atanhf, atanhlComputes the hyperbolic arc tangent
_atodbl, _atodbl_lConverts a locale-specific string to a double
atof, _atof_lConverts a string to a double
_atoflt, _atoflt_l, _atoldbl, _atoldbl_lConverts a locale-specific string to a float or long double
cbrt, cbrtf, cbrtlComputes the cube root
ceil, ceilf, ceillComputes the ceiling
_chgsign, _chgsignf, _chgsignlComputes the additive inverse
_clear87, _clearfpGets and clears the floating-point status register
_control87, _controlfp, __control87_2Gets and sets the floating-point control word
_controlfp_sSecure version of _controlfp
copysign, copysignf, copysignl, _copysign, _copysignf, _copysignlReturns a value that has the magnitude of one argument and the sign of another
cos, cosf, coslComputes the sine
cosh, coshf, coshlComputes the hyperbolic sine
div, ldiv, lldivComputes the quotient and the remainder of two integer values
_ecvt, ecvtConverts a double to a string
_ecvt_sSecure version of _ecvt
erf, erff, erflComputes the error function
erfc, erfcf, erfclComputes the complementary error function
exp, expf, explComputes the exponential ex
exp2, exp2f, exp2lComputes the exponential 2x
expm1, expm1f, expm1lComputes ex-1
fabs, fabsf, fabslComputes the absolute value of a floating-point type
_fcvt, fcvtConverts a floating-point number to a string
_fcvt_sSecure version of _fcvt
fdim, fdimf, fdimlDetermines the positive difference between two values
feclearexceptClears specified floating-point exceptions
fegetenvStores the current floating-point environment
fegetexceptflagGets the specified floating-point exception status
fegetroundGets the floating-point rounding mode
feholdexceptSets non-stop floating-point exception mode
feraiseexceptRaises the specified floating-point exceptions
fesetenvSets the current floating-point environment
fesetexceptflagSets the specified floating-point status flags
fesetroundSets the specified floating-point rounding mode
fetestexceptDetermines which floating-point exception status flags are set
feupdateenvRestores a floating-point environment then raises previous exceptions
floor, floorf, floorlComputes the floor
fma, fmaf, fmalComputes a fused multiply-add
fmax, fmaxf, fmaxlComputes the maximum of the arguments
fmin, fminf, fminlComputes the minimum of the arguments
fmod, fmodf, fmodlComputes the floating-point remainder
_fpclass, _fpclassfReturns the classification of a floating-point value
fpclassifyReturns the classification of a floating-point value
_fpieee_fltSets a handler for floating-point exceptions
_fpresetResets the floating-point environment
frexp, frexpf, frexplGets the mantissa and exponent of a floating-point number
_gcvt, gcvtConverts a floating-point number to a string
_gcvt_sSecure version of _gcvt
_get_FMA3_enable, _set_FMA3_enableGets or sets a flag for use of FMA3 instructions on x64
hypot, hypotf, hypotl, _hypot, _hypotf, _hypotlComputes the hypotenuse
ilogb, ilogbf, ilogblComputes the integer base-2 exponent
imaxabsComputes the absolute value of an integer type
imaxdivComputes the quotient and the remainder of two integer values
isfinite, _finite, _finitefDetermines whether a value is finite
isgreater, isgreaterequal, isless, islessequal, islessgreater, isunorderedCompare the order of two floating-point values
isinfDetermines whether a floating-point value is infinite
isnan, _isnan, _isnanfTests a floating-point value for NaN
isnormalTests whether a floating-point value is both finite and not subnormal
_j0, _j1, _jnComputes the Bessel function
ldexp, ldexpf, ldexplComputes x*2n
lgamma, lgammaf, lgammalComputes the natural logarithm of the absolute value of the gamma function
llrint, llrintf, llrintlRounds a floating-point value to the nearest long long value
llround, llroundf, llroundlRounds a floating-point value to the nearest long long value
log, logf, logl, log10, log10f, log10lComputes the natural or base-10 logarithm
log1p, log1pf, log1plComputes the natural logarithm of 1+x
log2, log2f, log2lComputes the base-2 logarithm
logb, logbf, logbl, _logb, _logbfReturns the exponent of a floating-point value
lrint, lrintf, lrintlRounds a floating-point value to the nearest long value
_lrotl, _lrotrRotates an integer value left or right
lround, lroundf, lroundlRounds a floating-point value to the nearest long value
_matherrThe default math error handler
__maxMacro that returns the larger of two values
__minMacro that returns the smaller of two values
modf, modff, modflSplits a floating-point value into fractional and integer parts
nan, nanf, nanlReturns a quiet NaN value
nearbyint, nearbyintf, nearbyintlReturns the rounded value
nextafter, nextafterf, nextafterl, _nextafter, _nextafterfReturns the next representable floating-point value
nexttoward, nexttowardf, nexttowardlReturns the next representable floating-point value
pow, powf, powlReturns the value of xy
remainder, remainderf, remainderlComputes the remainder of the quotient of two floating-point values
remquo, remquof, remquolComputes the remainder of two integer values
rint, rintf, rintlRounds a floating-point value
_rotl, _rotl64, _rotr, _rotr64Rotates bits in integer types
round, roundf, roundlRounds a floating-point value
_scalb, _scalbfScales argument by a power of 2
scalbn, scalbnf, scalbnl, scalbln, scalblnf, scalblnlMultiplies a floating-point number by an integral power of FLT_RADIX
_set_controlfpSets the floating-point control word
_set_SSE2_enableEnables or disables SSE2 instructions
signbitTests the sign bit of a floating-point value
sin, sinf, sinlComputes the sine
sinh, sinhf, sinhlComputes the hyperbolic sine
sqrt, sqrtf, sqrtlComputes the square root
_status87, _statusfp, _statusfp2Gets the floating-point status word
strtof, _strtof_lConverts a string to a float
strtold, _strtold_lConverts a string to a long double
tan, tanf, tanlComputes the tangent
tanh, tanhf, tanhlComputes the hyperbolic tangent
tgamma, tgammaf, tgammalComputes the gamma function
trunc, truncf, trunclTruncates the fractional part
_wtof, _wtof_lConverts a wide string to a double
_y0, _y1, _ynComputes the Bessel function

See also

Universal C runtime routines by category
Floating-point primitives

close