Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/numeric/math/scalbn"

From cppreference.com
< cpp‎ | numeric‎ | math
m (Return value: +HUGE_VAL lnk)
m (.)
 
(3 intermediate revisions by 2 users not shown)
Line 2: Line 2:
 
{{cpp/numeric/math/navbar}}
 
{{cpp/numeric/math/navbar}}
 
{{dcl begin}}
 
{{dcl begin}}
{{dcl header | cmath}}
+
{{dcl header|cmath}}
{{dcl | since=c++11 | num=1 |
+
{{dcl h|{{c/core|int}} exponent}}
float      scalbn ( float x, int exp );
+
{{dcl rev multi|num=1|since1=c++11|dcl1=
float      scalbnf( float x, int exp );
+
float      scalbn ( float num, int exp );
 +
double      scalbn ( double num, int exp );
 +
long double scalbn ( long double num, int exp );
 +
|since2=c++23|dcl2=
 +
constexpr /* floating-point-type */
 +
            scalbn ( /* floating-point-type */ num, int exp );
 
}}
 
}}
{{dcl | since=c++11 | num=2 |
+
{{dcl|num=2|since=c++11|notes={{mark|constexpr since C++23}}|
double      scalbn ( double x, int exp );
+
float      scalbnf( float num, int exp );
 
}}
 
}}
{{dcl | since=c++11 | num=3 |
+
{{dcl|num=3|since=c++11|notes={{mark|constexpr since C++23}}|
long double scalbn ( long double x, int exp );
+
long double scalbnl( long double num, int exp );
long double scalbnl( long double x, int exp );
+
 
}}
 
}}
{{dcl | since=c++11 | num=4 |
+
{{dcl h|{{c/core|long}} exponent}}
double      scalbn ( IntegralType x, int exp );
+
{{dcl rev multi|num=4|since1=c++11|dcl1=
 +
float      scalbln ( float num, long exp );
 +
double      scalbln ( double num, long exp );
 +
long double scalbln ( long double num, long exp );
 +
|since2=c++23|dcl2=
 +
constexpr /* floating-point-type */
 +
            scalbln ( /* floating-point-type */ num, long exp );
 
}}
 
}}
{{dcl | since=c++11 | num=5 |
+
{{dcl|num=5|since=c++11|notes={{mark|constexpr since C++23}}|
float      scalbln ( float x, long exp );
+
float      scalblnf( float num, long exp );
float      scalblnf( float x, long exp );
+
 
}}
 
}}
{{dcl | since=c++11 | num=6 |
+
{{dcl|num=6|since=c++11|notes={{mark|constexpr since C++23}}|
double     scalbln ( double x, long exp );
+
long double scalblnl( long double num, long exp );
 
}}
 
}}
{{dcl | since=c++11 | num=7 |
+
{{dcl h|[[#Notes|Additional overloads]]}}
long double scalbln ( long double x, long exp );
+
{{dcl header|cmath}}
long double scalblnl( long double x, long exp );
+
{{dcl|num=A|since=c++11|notes={{mark|constexpr since C++23}}|
 +
template< class Integer >
 +
double scalbn( Integer num, int exp );
 
}}
 
}}
{{dcl | since=c++11 | num=8 |
+
{{dcl|num=B|since=c++11|notes={{mark|constexpr since C++23}}|
double     scalbln ( IntegralType x, long exp );
+
template< class Integer >
 +
double scalbln( Integer num, long exp );
 
}}
 
}}
 
{{dcl end}}
 
{{dcl end}}
  
@1-3,5-7@ Multiplies a floating point value {{tt|x}} by {{lc|FLT_RADIX}} raised to power {{tt|exp}}.
+
@1-6@ Multiplies a floating point value {{c|num}} by {{lc|FLT_RADIX}} raised to power {{c|exp}}.{{rev inl|since=c++23| The library provides overloads of {{tt|std::scalbn}} and {{tt|std::scalbln}} for all cv-unqualified floating-point types as the type of the parameter {{c|num}}.}}
@4,8@ A set of overloads or a function template accepting an argument of any [[cpp/types/is_integral|integral type]]. Equivalent to {{v|2}} or {{v|6}} (the argument is cast to {{c|double}}).
+
@A,B@ Additional overloads are provided for all integer types, which are treated as {{c/core|double}}.
  
 
===Parameters===
 
===Parameters===
 
{{par begin}}
 
{{par begin}}
{{par | x | floating point value}}
+
{{par|num|floating-point or integer value}}
{{par | exp | integer value}}
+
{{par|exp|integer value}}
 
{{par end}}
 
{{par end}}
  
 
===Return value===
 
===Return value===
If no errors occur, {{tt|x}} multiplied by {{lc|FLT_RADIX}} to the power of {{tt|arg}} ({{math|x&times;FLT_RADIX{{su|p=exp}}}}) is returned.
+
If no errors occur, {{c|num}} multiplied by {{lc|FLT_RADIX}} to the power of {{c|exp}} ({{math|num&times;FLT_RADIX{{su|p=exp}}}}) is returned.
  
 
If a range error due to overflow occurs, {{lc|HUGE_VAL|±HUGE_VAL}}, {{tt|±HUGE_VALF}}, or {{tt|±HUGE_VALL}} is returned.
 
If a range error due to overflow occurs, {{lc|HUGE_VAL|±HUGE_VAL}}, {{tt|±HUGE_VALF}}, or {{tt|±HUGE_VALL}} is returned.
Line 50: Line 62:
  
 
===Error handling===
 
===Error handling===
Errors are reported as specified in {{lc|math_errhandling}}
+
Errors are reported as specified in {{lc|math_errhandling}}.
  
 
If the implementation supports IEEE floating-point arithmetic (IEC 60559),
 
If the implementation supports IEEE floating-point arithmetic (IEC 60559),
* Unless a range error occurs, {{lc|FE_INEXACT}} is never raised (the result is exact)
+
* Unless a range error occurs, {{lc|FE_INEXACT}} is never raised (the result is exact).
* Unless a range error occurs, [[cpp/numeric/fenv/FE_round|the current rounding mode]] is ignored
+
* Unless a range error occurs, [[cpp/numeric/fenv/FE_round|the current rounding mode]] is ignored.
* If {{tt|x}} is ±0, it is returned, unmodified
+
* If {{c|num}} is ±0, it is returned, unmodified.
* If {{tt|x}} is ±∞, it is returned, unmodified
+
* If {{c|num}} is ±∞, it is returned, unmodified.
* If {{tt|exp}} is 0, then {{tt|x}} is returned, unmodified
+
* If {{c|exp}} is 0, then {{c|num}} is returned, unmodified.
* If {{tt|x}} is NaN, NaN is returned
+
* If {{c|num}} is NaN, NaN is returned.
  
 
===Notes===
 
===Notes===
On binary systems (where {{lc|FLT_RADIX}} is {{tt|2}}), {{tt|std::scalbn}} is equivalent to {{lc|std::ldexp}}.
+
On binary systems (where {{lc|FLT_RADIX}} is {{c|2}}), {{tt|std::scalbn}} is equivalent to {{lc|std::ldexp}}.
  
 
Although {{tt|std::scalbn}} and {{tt|std::scalbln}} are specified to perform the operation efficiently, on many implementations they are less efficient than multiplication or division by a power of two using arithmetic operators.
 
Although {{tt|std::scalbn}} and {{tt|std::scalbln}} are specified to perform the operation efficiently, on many implementations they are less efficient than multiplication or division by a power of two using arithmetic operators.
Line 67: Line 79:
 
The function name stands for "new scalb", where {{tt|scalb}} was an older non-standard function whose second argument had floating-point type.
 
The function name stands for "new scalb", where {{tt|scalb}} was an older non-standard function whose second argument had floating-point type.
  
The {{tt|scalbln}} function is provided because the factor required to scale from the smallest positive floating-point value to the largest finite one may be greater than 32767, the standard-guaranteed {{lc|INT_MAX}}. In particular, for the 80-bit {{tt|long double}}, the factor is 32828.
+
The {{tt|std::scalbln}} function is provided because the factor required to scale from the smallest positive floating-point value to the largest finite one may be greater than 32767, the standard-guaranteed {{lc|INT_MAX}}. In particular, for the 80-bit {{c/core|long double}}, the factor is 32828.
  
The GNU implementation does not set {{tt|errno}} regardless of {{tt|math_errhandling}}
+
The GNU implementation does not set {{tt|errno}} regardless of {{tt|math_errhandling}}.
 +
 
 +
{{cpp/numeric/math/additional integer overload note|scalbn}}
  
 
===Example===
 
===Example===
{{example|code=
+
{{example
#include <iostream>
+
|code=
#include <cmath>
+
 
#include <cerrno>
 
#include <cerrno>
#include <cstring>
 
 
#include <cfenv>
 
#include <cfenv>
 +
#include <cmath>
 +
#include <cstring>
 +
#include <iostream>
 +
// #pragma STDC FENV_ACCESS ON
  
#pragma STDC FENV_ACCESS ON
 
 
int main()
 
int main()
 
{
 
{
Line 88: Line 103:
 
               << std::scalbn(std::nextafter(1,0), 1024)
 
               << std::scalbn(std::nextafter(1,0), 1024)
 
               << " (largest finite double)\n";
 
               << " (largest finite double)\n";
 +
   
 
     // special values
 
     // special values
 
     std::cout << "scalbn(-0, 10) = " << std::scalbn(-0.0, 10) << '\n'
 
     std::cout << "scalbn(-0, 10) = " << std::scalbn(-0.0, 10) << '\n'
 
               << "scalbn(-Inf, -1) = " << std::scalbn(-INFINITY, -1) << '\n';
 
               << "scalbn(-Inf, -1) = " << std::scalbn(-INFINITY, -1) << '\n';
 +
   
 
     // error handling
 
     // error handling
 
     errno = 0;
 
     errno = 0;
 
     std::feclearexcept(FE_ALL_EXCEPT);
 
     std::feclearexcept(FE_ALL_EXCEPT);
 +
   
 
     std::cout << "scalbn(1, 1024) = " << std::scalbn(1, 1024) << '\n';
 
     std::cout << "scalbn(1, 1024) = " << std::scalbn(1, 1024) << '\n';
 +
   
 
     if (errno == ERANGE)
 
     if (errno == ERANGE)
 
         std::cout << "    errno == ERANGE: " << std::strerror(errno) << '\n';
 
         std::cout << "    errno == ERANGE: " << std::strerror(errno) << '\n';
Line 114: Line 133:
 
===See also===
 
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/numeric/math/dsc frexp}}
+
{{dsc inc|cpp/numeric/math/dsc frexp}}
{{dsc inc | cpp/numeric/math/dsc ldexp}}
+
{{dsc inc|cpp/numeric/math/dsc ldexp}}
{{dsc see c | c/numeric/math/scalbn}}
+
{{dsc see c|c/numeric/math/scalbn}}
 
{{dsc end}}
 
{{dsc end}}
  
 
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}
 
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}

Latest revision as of 21:44, 15 October 2023

 
 
 
 
Defined in header <cmath>
int exponent
(1)
float       scalbn ( float num, int exp );

double      scalbn ( double num, int exp );

long double scalbn ( long double num, int exp );
(since C++11)
(until C++23)
constexpr /* floating-point-type */
            scalbn ( /* floating-point-type */ num, int exp );
(since C++23)
float       scalbnf( float num, int exp );
(2) (since C++11)
(constexpr since C++23)
long double scalbnl( long double num, int exp );
(3) (since C++11)
(constexpr since C++23)
long exponent
(4)
float       scalbln ( float num, long exp );

double      scalbln ( double num, long exp );

long double scalbln ( long double num, long exp );
(since C++11)
(until C++23)
constexpr /* floating-point-type */
            scalbln ( /* floating-point-type */ num, long exp );
(since C++23)
float       scalblnf( float num, long exp );
(5) (since C++11)
(constexpr since C++23)
long double scalblnl( long double num, long exp );
(6) (since C++11)
(constexpr since C++23)
Defined in header <cmath>
template< class Integer >
double scalbn( Integer num, int exp );
(A) (since C++11)
(constexpr since C++23)
template< class Integer >
double scalbln( Integer num, long exp );
(B) (since C++11)
(constexpr since C++23)
1-6) Multiplies a floating point value num by FLT_RADIX raised to power exp. The library provides overloads of std::scalbn and std::scalbln for all cv-unqualified floating-point types as the type of the parameter num.(since C++23)
A,B) Additional overloads are provided for all integer types, which are treated as double.

Contents

[edit] Parameters

num - floating-point or integer value
exp - integer value

[edit] Return value

If no errors occur, num multiplied by FLT_RADIX to the power of exp (num×FLT_RADIXexp) 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.

[edit] 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_INEXACT is never raised (the result is exact).
  • Unless a range error occurs, the current rounding mode is ignored.
  • If num is ±0, it is returned, unmodified.
  • If num is ±∞, it is returned, unmodified.
  • If exp is 0, then num is returned, unmodified.
  • If num is NaN, NaN is returned.

[edit] Notes

On binary systems (where FLT_RADIX is 2), std::scalbn is equivalent to std::ldexp.

Although std::scalbn and std::scalbln are specified to perform the operation efficiently, on many implementations they are less efficient than multiplication or division by a power of two using arithmetic operators.

The function name stands for "new scalb", where scalb was an older non-standard function whose second argument had floating-point type.

The std::scalbln function is provided because the factor required to scale from the smallest positive floating-point value to the largest finite one may be greater than 32767, the standard-guaranteed INT_MAX. In particular, for the 80-bit long double, the factor is 32828.

The GNU implementation does not set errno regardless of math_errhandling.

The additional overloads are not required to be provided exactly as (A,B). They only need to be sufficient to ensure that for their argument num of integer type:

  • std::scalbn(num, exp) has the same effect as std::scalbn(static_cast<double>(num), exp).
  • std::scalbln(num, exp) has the same effect as std::scalbln(static_cast<double>(num), exp).

[edit] Example

#include <cerrno>
#include <cfenv>
#include <cmath>
#include <cstring>
#include <iostream>
// #pragma STDC FENV_ACCESS ON
 
int main()
{
    std::cout << "scalbn(7, -4) = " << std::scalbn(7, -4) << '\n'
              << "scalbn(1, -1074) = " << std::scalbn(1, -1074)
              << " (minimum positive subnormal double)\n"
              << "scalbn(nextafter(1,0), 1024) = "
              << std::scalbn(std::nextafter(1,0), 1024)
              << " (largest finite double)\n";
 
    // special values
    std::cout << "scalbn(-0, 10) = " << std::scalbn(-0.0, 10) << '\n'
              << "scalbn(-Inf, -1) = " << std::scalbn(-INFINITY, -1) << '\n';
 
    // error handling
    errno = 0;
    std::feclearexcept(FE_ALL_EXCEPT);
 
    std::cout << "scalbn(1, 1024) = " << std::scalbn(1, 1024) << '\n';
 
    if (errno == ERANGE)
        std::cout << "    errno == ERANGE: " << std::strerror(errno) << '\n';
    if (std::fetestexcept(FE_OVERFLOW))
        std::cout << "    FE_OVERFLOW raised\n";
}

Possible output:

scalbn(7, -4) = 0.4375
scalbn(1, -1074) = 4.94066e-324 (minimum positive subnormal double)
scalbn(nextafter(1,0), 1024) = 1.79769e+308 (largest finite double)
scalbn(-0, 10) = -0
scalbn(-Inf, -1) = -inf
scalbn(1, 1024) = inf
    errno == ERANGE: Numerical result out of range
    FE_OVERFLOW raised

[edit] See also

(C++11)(C++11)
decomposes a number into significand and base-2 exponent
(function) [edit]
(C++11)(C++11)
multiplies a number by 2 raised to an integral power
(function) [edit]
C documentation for scalbn