logb, logbf, logbl
来自 cppreference.cn
定义于头文件 <math.h> |
||
float logbf( float arg ); |
(1) | (C99 起) |
double logb( double arg ); |
(2) | (C99 起) |
long double logbl( long double arg ); |
(3) | (C99 起) |
定义于头文件 <tgmath.h> |
||
#define logb( arg ) |
(4) | (C99 起) |
1-3) 从浮点参数 arg 中提取无偏、与基数无关的指数值,并将其作为浮点值返回。
4) 泛型宏:如果 arg 的类型为 long double,则调用
logbl
。否则,如果 arg 的类型为整数类型或 double,则调用 logb
。否则,调用 logbf
。形式上,对于非零 arg,无偏指数是 logr|arg| 的有符号整数部分(此函数将其作为浮点值返回),其中 r
是 FLT_RADIX。如果 arg 是次正规数,则将其视为已归一化。
目录 |
[编辑] 参数
arg | - | 浮点值 |
[编辑] 返回值
如果没有发生错误,则返回 arg 的无偏指数作为带符号浮点值。
若发生定义域错误,返回实现定义的值。
如果发生极点错误,则返回 -HUGE_VAL、-HUGE_VALF
或 -HUGE_VALL
。
[编辑] 错误处理
错误按 math_errhandling
中指定的方式报告。
如果 arg 为零,则可能发生定义域错误或范围错误。
如果实现支持 IEEE 浮点运算 (IEC 60559),
- 如果 arg 为 ±0,则返回 -∞ 并引发 FE_DIVBYZERO。
- 如果 arg 为 ±∞,则返回 +∞。
- 如果 arg 是 NaN,则返回 NaN。
- 在所有其他情况下,结果是精确的(从不引发 FE_INEXACT),并忽略当前舍入模式。
[编辑] 注意
POSIX 要求,如果 arg 为 ±0,则发生极点错误。
logb
返回的指数值总是比 frexp 返回的指数值小 1,因为归一化要求不同:对于 logb
返回的指数 e
,|arg*r-e
| 介于 1 和 r
之间(通常介于 1 和 2 之间),但对于 frexp 返回的指数 e
,|arg*2-e
| 介于 0.5 和 1 之间。
[编辑] 示例
比较不同的浮点分解函数。
运行此代码
#include <fenv.h> #include <float.h> #include <math.h> #include <stdio.h> // #pragma STDC FENV_ACCESS ON int main(void) { double f = 123.45; printf("Given the number %.2f or %a in hex,\n", f, f); double f3; double f2 = modf(f, &f3); printf("modf() makes %.0f + %.2f\n", f3, f2); int i; f2 = frexp(f, &i); printf("frexp() makes %f * 2^%d\n", f2, i); i = logb(f); printf("logb()/logb() make %f * %d^%d\n", f/scalbn(1.0, i), FLT_RADIX, i); // error handling feclearexcept(FE_ALL_EXCEPT); printf("logb(0) = %f\n", logb(0)); if (fetestexcept(FE_DIVBYZERO)) puts(" FE_DIVBYZERO raised"); }
可能的输出
Given the number 123.45 or 0x1.edccccccccccdp+6 in hex, modf() makes 123 + 0.45 frexp() makes 0.964453 * 2^7 logb()/logb() make 1.928906 * 2^6 logb(0) = -Inf FE_DIVBYZERO raised
[编辑] 参考
- C23 标准 (ISO/IEC 9899:2024)
- 7.12.6.11 logb 函数 (p: TBD)
- 7.25 类型通用数学 <tgmath.h> (p: TBD)
- F.10.3.11 logb 函数 (p: TBD)
- C17 标准 (ISO/IEC 9899:2018)
- 7.12.6.11 logb 函数 (p: 179-180)
- 7.25 类型通用数学 <tgmath.h> (p: 373-375)
- F.10.3.11 logb 函数 (p: 381)
- C11 标准 (ISO/IEC 9899:2011)
- 7.12.6.11 logb 函数 (p: 246)
- 7.25 类型通用数学 <tgmath.h> (p: 373-375)
- F.10.3.11 logb 函数 (p: 522)
- C99 标准 (ISO/IEC 9899:1999)
- 7.12.6.11 logb 函数 (p: 227)
- 7.22 类型通用数学 <tgmath.h> (p: 335-337)
- F.9.3.11 logb 函数 (p: 459)
[编辑] 参阅
(C99)(C99) |
将一个数字分解为有效数字和2的幂 (函数) |
(C99)(C99)(C99) |
提取给定数字的指数 (函数) |
(C99)(C99)(C99)(C99)(C99)(C99) |
高效计算一个数字乘以 FLT_RADIX 的幂 (函数) |
C++ 文档 for logb
|