計算對數。
語法
double log(double x);
float logf(float x);
long double logl(double x);
double log10(double x);
float log10f (float x);
long double log10l(double x);
#define log(X) // Requires C11 or later
#define log10(X) // Requires C11 or later
float log(float x); // C++ only
long double log(long double x); // C++ only
float log10(float x); // C++ only
long double log10(long double x); // C++ only
參數
x
要求得其對數的值。
傳回值
如果成功,函式會log傳回 的自然對數(基底e)。x 函式會 log10 傳回base-10對數。 如果 x 為負數,則這些函式預設會傳回無限期 (IND)。 如果 x 為 0,則會傳回無限 (INF)。
| 輸入 | SEH 例外狀況 |
_matherr 例外 |
|---|---|---|
| ± QNaN,IND | none | _DOMAIN |
| ± 0 | ZERODIVIDE |
_SING |
x < 0 |
INVALID |
_DOMAIN |
log 和 log10 具有使用串流 SIMD 延伸模組 2 (SSE2) 的實作。 如需使用 SSE2 實作的資訊和限制,請參閱 _set_SSE2_enable 。
備註
C++允許多載,因此您可以呼叫 和 的多載 log ,並 log10 接受 和 傳回 float 或 long double 值。 在 C 程式中,除非您使用 <tgmath.h> 巨集來呼叫此函式, log 而且 log10 一律接受 並傳回 double。
如果您使用 <tgmath.h> log() 巨集,自變數的類型會決定選取哪一個函式版本。 如需詳細資料,請參閱型別泛型數學。
根據預設,此函式的全域狀態會限定於應用程式。 若要變更此行為,請參閱 CRT 中的全域狀態。
需求
| 常式 | 必要的標頭 |
|---|---|
log、、logflogl、log10、、log10f、log10l |
<math.h> |
log 巨集 |
<tgmath.h> |
如需相容性詳細資訊,請參閱相容性。
範例
// crt_log.c
/* This program uses log and log10
* to calculate the natural logarithm and
* the base-10 logarithm of 9,000.
*/
#include <math.h>
#include <stdio.h>
int main( void )
{
double x = 9000.0;
double y;
y = log( x );
printf( "log( %.2f ) = %f\n", x, y );
y = log10( x );
printf( "log10( %.2f ) = %f\n", x, y );
}
log( 9000.00 ) = 9.104980
log10( 9000.00 ) = 3.954243
若要產生其他底數的對數,請使用數學關聯︰a 的對數底數 b == 自然對數 (a) / 自然對數 (b)。
// logbase.cpp
#include <math.h>
#include <stdio.h>
double logbase(double a, double base)
{
return log(a) / log(base);
}
int main()
{
double x = 65536;
double result;
result = logbase(x, 2);
printf("Log base 2 of %lf is %lf\n", x, result);
}
Log base 2 of 65536.000000 is 16.000000
另請參閱
數學與浮點支援
exp、 、 expfexpl
_matherr
pow、 、 powfpowl
_CIlog
_CIlog10