Floating-Point Support
The new home for Visual Studio documentation is Visual Studio 2017 Documentation on docs.microsoft.com.
The latest version of this topic can be found at Floating-Point Support.
The Microsoft C Runtime library (CRT) provides many floating point math library functions, including all of those required by ISO C99. These 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 of the correctly rounded result, though there may be cases where there is greater inaccuracy.
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 does not 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.
The CRT supports these floating point functions:
atan, atanf, atanl, atan2, atan2f, atan2l
_atoflt, _atoflt_l, _atoldbl, _atoldbl_l
_chgsign, _chgsignf, _chgsignl
_control87, __control87_2, _controlfp
copysign, copysignf, copysignl, _copysign, _copysignf, _copysignl
erf, erff, erfl, erfc, erfcf, erfcl
hypot, hypotf, hypotl, _hypot, _hypotf, _hypotl
logb, logbf, logbl, _logb, _logbf
nearbyint, nearbyintf, nearbyintl
nextafter, nextafterf, nextafterl, _nextafter, _nextafterf, nexttoward, nexttowardf, nexttowardl
remainder, remainderf, remainderl
_rotl, _rotl64, _rotr, _rotr64
scalbn, scalbnf, scalbnl, scalbln, scalblnf, scalblnl
_status87, _statusfp, _statusfp2