std:: logb, std:: logbf, std:: logbl
|
定义于头文件
<cmath>
|
||
| (1) | ||
|
float
logb
(
float
num
)
;
double
logb
(
double
num
)
;
|
(C++23 前) | |
|
constexpr
/*floating-point-type*/
logb ( /*floating-point-type*/ num ) ; |
(C++23 起) | |
|
float
logbf
(
float
num
)
;
|
(2) |
(C++11 起)
(C++23 起为 constexpr) |
|
long
double
logbl
(
long
double
num
)
;
|
(3) |
(C++11 起)
(C++23 起为 constexpr) |
|
SIMD 重载
(C++26 起)
|
||
|
定义于头文件
<simd>
|
||
|
template
<
/*math-floating-point*/
V
>
constexpr
/*deduced-simd-t*/
<
V
>
|
(S) | (C++26 起) |
|
附加重载
(C++11 起)
|
||
|
定义于头文件
<cmath>
|
||
|
template
<
class
Integer
>
double logb ( Integer num ) ; |
(A) | (C++23 起为 constexpr) |
std::logb
的重载,作为参数的类型。
(since C++23)
|
S)
SIMD重载对
v_num
执行逐元素的
std::logb
运算。
|
(since C++26) |
|
A)
为所有整数类型提供了额外的重载,这些类型被视为
double
。
|
(since C++11) |
形式上,对于非零的
num
,无偏指数是指
log
r
|num|
的带符号整数部分(由本函数以浮点值形式返回),其中
r
是
std::
numeric_limits
<
T
>
::
radix
,而
T
是
num
的浮点类型。如果
num
是次正规数,则将其视为已正规化处理。
目录 |
参数
| num | - | 浮点数或整数值 |
返回值
若无错误发生,将返回 num 的无偏指数作为带符号浮点数值。
如果发生定义域错误,则返回一个实现定义的值。
如果出现极点错误,将返回
-HUGE_VAL
、
-HUGE_VALF
或
-HUGE_VALL
。
错误处理
错误报告方式遵循 math_errhandling 中的规范。
当 num 为零时可能出现定义域或值域错误。
如果实现支持 IEEE 浮点算术 (IEC 60559),
- 如果 num 为 ±0,则返回 -∞ 并引发 FE_DIVBYZERO 。
- 如果 num 为 ±∞,则返回 +∞。
- 如果 num 为 NaN,则返回 NaN。
- 在所有其他情况下,结果是精确的(永不引发 FE_INEXACT )且忽略 当前舍入模式 。
注释
POSIX 要求 ,如果 num 为 ±0,则发生极点错误。
std::logb
返回的指数值始终比
std::frexp
返回的指数小 1,这是由于不同的归一化要求所致:对于
std::logb
返回的指数
e
,
|num*r
-e
|
的取值范围在
1
到
r
之间(通常为
1
到
2
),而对于
std::frexp
返回的指数
e
,
|num*2
-e
|
的取值范围在
0.5
到
1
之间。
额外的重载并不要求完全按照 (A) 提供。只需确保对于整数类型的实参 num , std :: logb ( num ) 与 std :: logb ( static_cast < double > ( num ) ) 具有相同效果即可。
示例
比较不同的浮点数分解函数:
#include <cfenv> #include <cmath> #include <iostream> #include <limits> // #pragma STDC FENV_ACCESS ON int main() { double f = 123.45; std::cout << "给定数值 " << f << " 或十六进制格式 " << std::hexfloat << f << std::defaultfloat << ",\n"; double f3; double f2 = std::modf(f, &f3); std::cout << "modf() 生成 " << f3 << " + " << f2 << '\n'; int i; f2 = std::frexp(f, &i); std::cout << "frexp() 生成 " << f2 << " * 2^" << i << '\n'; i = std::ilogb(f); std::cout << "logb()/ilogb() 生成 " << f / std::scalbn(1.0, i) << " * " << std::numeric_limits<double>::radix << "^" << std::ilogb(f) << '\n'; // 错误处理 std::feclearexcept(FE_ALL_EXCEPT); std::cout << "logb(0) = " << std::logb(0) << '\n'; if (std::fetestexcept(FE_DIVBYZERO)) std::cout << " 触发 FE_DIVBYZERO 异常\n"; }
可能的输出:
给定数值 123.45 或十六进制格式 0x1.edccccccccccdp+6,
modf() 生成 123 + 0.45
frexp() 生成 0.964453 * 2^7
logb()/ilogb() 生成 1.92891 * 2^6
logb(0) = -Inf
触发 FE_DIVBYZERO 异常
参见
|
(C++11)
(C++11)
|
将数字分解为有效数字和以
2
为底的指数
(函数) |
|
(C++11)
(C++11)
(C++11)
|
提取数字的指数
(函数) |
|
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
|
将数字乘以
FLT_RADIX
的指定次幂
(函数) |
|
C 文档
for
logb
|
|