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関数 (p: 227)
  • 7.22 型総称数学 <tgmath.h> (p: 335-337)
  • F.9.3.11 logb関数 (p: 459)

関連項目

数値を仮数と2の累乗に分解する
(関数)
(C99) (C99) (C99)
指定された数値の指数部を抽出する
(関数)
(C99) (C99) (C99) (C99) (C99) (C99)
数値に FLT_RADIX の累乗を効率的に乗算する
(関数)