std::ldexp,...std::ldexpl(3)
π NAME π
Okay bestie, welcome to the std::ldexp,...std::ldexpl(3) page β manual name and all, from the C++ Standard Libary (yes, that spelling is literally in the header π). Weβre doing std::ldexp, std::ldexpf, and std::ldexpl today, and the vibe is: scale a float by a power of two, no cap π¦.
std::ldexp,std::ldexpf,std::ldexpl - std::ldexp,std::ldexpf,std::ldexpl
The NAME line is literally the trio twice β iconic redundancy, we love her π. These are the C++ overloads for loading an exponent onto a floating-point value.
β¨ Synopsis β¨
The header is <cmath>, and hereβs the signature wall in all its glory:
Defined in header <cmath>
float ldexp ( float num, int exp );
double ldexp ( double num, int exp ); (until C++23)
long double ldexp ( long double num, int exp );
constexpr /* floating-point-type */
ldexp ( /* floating-point-type */ num, (since C++23)
int exp ); (1)
float ldexpf( float num, int exp ); (2) (since C++11)
(constexpr since C++23)
long double ldexpl( long double num, int exp ); (3) (since C++11)
(constexpr since C++23)
Additional overloads (since C++11)
Defined in header <cmath>
template< class Integer > (A) (since C++11)
double ldexp ( Integer num, int exp );
(constexpr since C++23)
1-3)Multiplies a floating point valuenumby the number 2 raised to theexppower.- The library provides overloads of
std::ldexpfor all cv-unqualified floating-point types as the type of the parameternum.(since C++23) A)Additional overloads are provided for all integer types, which are treated asdouble.(since C++11)
π«Ά Parameters π«Ά
numβ floating-point or integer valueexpβ integer value
π Return value π
If no errors occur, num multiplied by 2 to the power of exp (numΓ2exp) is returned.
If a range error due to overflow occurs, Β±HUGE_VAL, Β±HUGE_VALF, or Β±HUGE_VALL is returned.
If a range error due to underflow occurs, the correct result (after rounding) is returned.
π¨ Error handling π¨
Errors are reported as specified in math_errhandling.
If the implementation supports IEEE floating-point arithmetic (IEC 60559),
- Unless a range error occurs,
FE_INEXACTis never raised (the result is exact). - Unless a range error occurs, the current rounding mode is ignored.
- If
numisΒ±0, it is returned, unmodified. - If
numisΒ±β, it is returned, unmodified. - If
expis0, thennumis returned, unmodified. - If
numisNaN,NaNis returned.
π Notes π
On binary systems (where FLT_RADIX is 2), std::ldexp is equivalent to std::scalbn.
The function std::ldexp ("load exponent"), together with its dual, std::frexp, can be used to manipulate the representation of a floating-point number without direct bit manipulations.
On many implementations, std::ldexp is less efficient than multiplication or division by a power of two using arithmetic operators.
The additional overloads are not required to be provided exactly as (A). They only need to be sufficient to ensure that for their argument num of integer type, std::ldexp(num, exp) has the same effect as std::ldexp(static_cast<double>(num), exp).
For exponentiation of 2 by a floating point exponent, std::exp2 can be used.
π» Example π»
// Run this code
#include <cerrno>
#include <cfenv>
#include <cmath>
#include <cstring>
#include <iostream>
// #pragma STDC FENV_ACCESS ON
int main()
{
std::cout
<< "ldexp(5, 3) = 5 * 8 = " << std::ldexp(5, 3) << '\n'
<< "ldexp(7, -4) = 7 / 16 = " << std::ldexp(7, -4) << '\n'
<< "ldexp(1, -1074) = " << std::ldexp(1, -1074)
<< " (minimum positive subnormal float64_t)\n"
<< "ldexp(nextafter(1,0), 1024) = "
<< std::ldexp(std::nextafter(1,0), 1024)
<< " (largest finite float64_t)\n";
// special values
std::cout << "ldexp(-0, 10) = " << std::ldexp(-0.0, 10) << '\n'
<< "ldexp(-Inf, -1) = " << std::ldexp(-INFINITY, -1) <<
'\n';
// error handling
std::feclearexcept(FE_ALL_EXCEPT);
errno = 0;
const double inf = std::ldexp(1, 1024);
const bool is_range_error = errno == ERANGE;
std::cout << "ldexp(1, 1024) = " << inf << '\n';
if (is_range_error)
std::cout << " errno == ERANGE: " << std::strerror(ERANGE)
<< '\n';
if (std::fetestexcept(FE_OVERFLOW))
std::cout << " FE_OVERFLOW raised\n";
}
π Possible output π
ldexp(5, 3) = 5 * 8 = 40
ldexp(7, -4) = 7 / 16 = 0.4375
ldexp(1, -1074) = 4.94066e-324 (minimum positive subnormal float64_t)
ldexp(nextafter(1,0), 1024) = 1.79769e+308 (largest finite float64_t)
ldexp(-0, 10) = -0
ldexp(-Inf, -1) = -inf
ldexp(1, 1024) = inf
errno == ERANGE: Numerical result out of range
FE_OVERFLOW raised
π See also π
frexp,frexpf,frexplβ decomposes a number into significand and base-2 exponent (function).(C++11)(C++11)scalbn,scalbnf,scalbnl,scalbln,scalblnf,scalblnlβ multiplies a number byFLT_RADIXraised to a power.(C++11)(function)(C++11)(C++11)(C++11)(C++11)(C++11)exp2,exp2f,exp2lβ returns 2 raised to the given power (\({\small 2^x}\)2^x).(C++11)(function)(C++11)(C++11)- C documentation for
ldexp
source: http://cppreference.com Β· version/date: 2024.06.10 Β· manual: std::ldexp,...std::ldexpl(3) β bye bestie, mwah π
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms