Namespaces
Variants

logb, logbf, logbl

From cppreference.net
< c ‎ | numeric ‎ | math
Common mathematical functions
Functions
Basic operations
(C99)
(C99)
(C99)
(C99) (C99) (C99) (C23)
Maximum/minimum operations
Exponential functions
Power functions
Trigonometric and hyperbolic functions
Nearest integer floating-point
(C99) (C99) (C99)
(C23) (C23) (C23) (C23)
Floating-point manipulation
(C99) (C99)
(C99) (C23)
logb
(C99)
Narrowing operations
(C23)
(C23)
(C23)
(C23)
(C23)
(C23)
Quantum and quantum exponent
Decimal re-encoding functions
Total order and payload functions
Classification
Error and gamma functions
(C99)
(C99)
(C99)
(C99)
Types
Macro constants
Special floating-point values
Arguments and return values
Error handling
Fast operation indicators
定义于头文件 <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 ,无偏指数是指数部分的带符号整数值(由本函数以浮点值返回),其计算公式为 log r |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函数(第227页)
  • 7.22 泛型数学 <tgmath.h>(第335-337页)
  • F.9.3.11 logb函数(第459页)

参考

将数值分解为有效数字和 2 的幂次
(函数)
(C99) (C99) (C99)
提取给定数值的指数
(函数)
(C99) (C99) (C99) (C99) (C99) (C99)
高效计算数值乘以 FLT_RADIX 的指定幂次
(函数)
C++ 文档 关于 logb