Aussie AI
Floating-Point Builtin Functions
-
Book Excerpt from "Generative AI in C++"
-
by David Spuler, Ph.D.
Floating-Point Builtin Functions
The alternative to directly accessing the bits as an unsigned integer is to use the existing C++ functions. There are various existing functions for bitwise manipulation of floating-point numbers, in two categories: standard C++ library functions and compiler-specific intrinsics.
C++ has standard functions for the manipulation of floating-point numbers, and their bitwise representations.
std::signbit
— Portably test the sign bit of a floating-point number.std::copysign
— Portably copies the sign bit from onefloat
, merging it with the value of another (i.e. another's exponent and mantissa).
There are also various compiler-specific “intrinsics” or “builtins” to manipulate floating-point numbers.
For Microsoft Visual Studio C++, these are in <intrin.h>
and there are also versions for GCC and other compilers.
frexp
— Get the mantissa and exponent.ldexp
— Bitshifting by an integer shift-count.scalbn
— Also integer bitshift on afloat
.logb
— Extracts the exponent.ilogb
— Extracts the exponent to integer.modf
— Splits into whole and fractional parts.fma
— Fused multiply add onfloat
(Microsoft intrinsic)remainder
— Get fractional part of floating-point (Microsoft intrinsic)_fcvt
— Low-level convertfloat
to string (Microsoft intrinsic)
For many of the listed functions, there are additional versions for different floating-point data types,
such as float
, double
and long double
.
For example, “frexp
” will split a double
type into its significand (fractional part) and exponent integer,
but there's also “frexpf
” for 32-bit float
types, and “frexpl
” for long double types.
• Next: • Up: Table of Contents |
The new AI programming book by Aussie AI co-founders:
Get your copy from Amazon: Generative AI in C++ |