當前位置:首頁 » 編程語言 » 對數演算法C語言
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

對數演算法C語言

發布時間: 2022-03-31 15:22:16

1. c語言中對數函數的表示。。。

"log10(P)"這個寫法對嗎,好像不需要10吧,直接log(p)就可以了。

2. 如何用C語言編寫一個對數運算

直接用函數,調用數學函數即可。記得包涵頭文件

3. C語言中求對數的函數是什麼

求lnx為log(x)求log 10 x是log10(x)沒有專門的求任意底數對數的函數,不過可以用log(x)/log(y)表示log y x

4. C語言如何實現對數運算

庫函數里有對數函數,如,ln,log等,使用時需要包含頭文件math.h

5. c語言 求對數問題,

沒有專門的求任意底數對數的函數,不過可以用 log(x)/log(y)表示log y x
b=log5(sqrt(a));改為b=log(sqrt(a))/log(5);

6. c語言怎樣輸入對數

#include<stdio.h>

#include <math.h>

void main()

{

float x=5,y;

y=log(x);

printf("%f ",y);

}

(6)對數演算法C語言擴展閱讀:

C語言中使用對數函數的方法

log()函數:返回以e為底的對數值

頭文件:

1#include

log() 函數返回以 e 為底的對數值,其原型為:

1doublelog(doublex);

log()用來計算以e為底的 x 的對數值,然後將結果返回。設返回值為 ret,則

1x = eret

如果 x 為負數或 0,則會發生錯誤並設置 errno 值。錯誤代碼:

EDOM:參數x 為負數;

ERANGE:參數x

為零值,零的對數值無定義。

注意:使用 GCC 編譯時請加入-lm。




7. 怎樣用c語言實現對數及其加減乘除的運算

添加頭文件#include "math.h",聲明雙精度浮點變數,然後調用庫函數log或log10就可以了。如:
#include "stdio.h"
#include "math.h"
void main(void){
double x,sum=0.0;
int i;
for(i=1;i<101;i++)
sum+=log10(i);
printf("%f\n",sum);
x=sum+log(3)/log(2)+log10(7)-log(105)-log10(2)*log10(7);
}
這代碼求出了log1+log2+log3+...+log100的值。而x=sum+log(3)/log(2)+log10(7)-log(105)-log10(2)*log10(7);進行了常用對數(log10)和自然對數(log)的四則運算,並計算結果賦給了雙精度變數x。

8. 請問用C語言怎麼可以實現對數和指數運算

//////////////////////////////////////////////////////////////////////
//
// 對數的例子//
// <filename> : main.cpp
//
// Functions:
//
// exp, log, log10
//////////////////////////////////////////////////////////////////////

#include <iostream> // for i/o functions
#include <valarray> // for valarray
#include <math.h> // for exp(), log(), and log10()

using namespace std;

#define ARRAY_SIZE 3 // array size

typedef valarray<double> DB_VARRAY;

void main()
{
// Set val_array to contain values 1, 10, 100 for the following test.
DB_VARRAY val_array(ARRAY_SIZE);
for (int i = 0; i < ARRAY_SIZE; i++)
val_array[i] = pow(10, i);

// Display the size of val_array.
cout << "Size of val_array = " << val_array.size() << "\r\n";

// Display the content of val_array before calling exp, log, and
// log10 functions.
cout << "The values in val_array:\r\n";
for (i = 0; i < ARRAY_SIZE; i++)
cout << val_array[i] << " ";
cout << "\r\n\r\n";

// rvalue_array to hold the return value from calling the sqrt() and
// pow() functions.
DB_VARRAY rvalue_array;

// ----------------------------------------------------------------
// exp() - display the content of rvalue_array
// ----------------------------------------------------------------
rvalue_array = exp(val_array);
cout << "The result after calling exp():\r\n";
for (i = 0; i < ARRAY_SIZE; i++)
cout << rvalue_array[i] << " ";
cout << "\r\n\r\n";

// ----------------------------------------------------------------
// log() - display the content of rvalue_array
// ----------------------------------------------------------------
rvalue_array = log(val_array);
cout << "The result after calling log():\r\n";
for (i = 0; i < ARRAY_SIZE; i++)
cout << rvalue_array[i] << " ";
cout << "\r\n\r\n";

// ----------------------------------------------------------------
// log10() - display the content of rvalue_array
// ----------------------------------------------------------------
rvalue_array = log10(val_array);
cout << "The result after calling log10():\r\n";
for (i = 0; i < ARRAY_SIZE; i++)
cout << rvalue_array[i] << " ";
cout << "\r\n\r\n";
}

Program Output is:

Size of val_array = 3

The values in val_array:

1 10 100

The result after calling exp():

2.71828 22026.5 2.68812e+043

The result after calling log():

0 2.30259 4.60517

The result after calling log10():

0 1 2

//////////////////////////////////////////////////////////////////////
//
// 指數的例子//
// main.cpp : Illustrates the use of STL sqrt() and pow() functions.
//
// Functions:
//
// sqrt, pow
//////////////////////////////////////////////////////////////////////

#include <iostream> // for i/o functions
#include <valarray> // for valarray
#include <cmath> // for sqrt() and pow()

using namespace std ;

#define ARRAY_SIZE 3 // array size

void main()
{
// Set val_array to contain values 1, 4, 9 for the following test
valarray<double> val_array(ARRAY_SIZE);

for (int i = 0; i < ARRAY_SIZE; i++)
val_array[i] = (i+1) * (i+1);

// Display the size of val_array
cout << "Size of val_array = " << val_array.size() << endl;

// Display the values of val_array before calling sqrt() and pow().
cout << "The values in val_array:" << endl;
for (i = 0; i < ARRAY_SIZE; i++)
cout << val_array[i] << " ";
cout << endl << endl;

// Initialize rev_valarray that is the reverse of val_array.
valarray<double> rev_valarray(ARRAY_SIZE);
for (i = 0; i < ARRAY_SIZE; i++)
rev_valarray[i] = val_array[ARRAY_SIZE - i - 1];

// Display the size of rev_valarray.
cout << "Size of rev_valarray = " << rev_valarray.size() << endl;

// Display the values of rev_valarray.
cout << "The values in rev_valarray:" << endl;
for (i = 0; i < ARRAY_SIZE; i++)
cout << rev_valarray[i] << " ";
cout << endl << endl;

// rvalue_array to hold the return value from calling the sqrt() and
// pow() functions.
valarray<double> rvalue_array;

// ----------------------------------------------------------------
// sqrt() - display the content of rvalue_array
// ----------------------------------------------------------------

// Display the result of val_array after calling sqrt().
rvalue_array = sqrt(val_array);
cout << "The result of val_array after calling sqrt():" << endl;
for (i = 0; i < ARRAY_SIZE; i++)
cout << rvalue_array[i] << " ";
cout << endl << endl;

// ----------------------------------------------------------------
// pow() - display the content of rvalue_array
// ----------------------------------------------------------------

// This template function returns an object of class valarray<T>,
// each of whose elements at I is x[I] raised to the power of y[I].
rvalue_array = pow(val_array, rev_valarray);
cout << "The result after calling pow(val_array, rev_valarray):"
<< endl;
for (i = 0; i < ARRAY_SIZE; i++)
cout << rvalue_array[i] << " ";
cout << endl << endl;

// This template function stores in element I x[I] raised to the
// power of y, where y=2.0.
rvalue_array = pow(val_array, 2.0);
cout << "The result after calling pow(val_array, 2.0):" << endl;
for (i = 0; i < ARRAY_SIZE; i++)
cout << rvalue_array[i] << " ";
cout << endl << endl;

// This template function stores in element I x raised to the
// y[I] power, where x=2.0.
rvalue_array = pow(2.0, val_array);
cout << "The result after calling pow(2.0, val_array):" << endl;
for (i = 0; i < ARRAY_SIZE; i++)
cout << rvalue_array[i] << " ";
cout << endl << endl;
}

Program Output is:

Size of val_array = 3
The values in val_array:
1 4 9

Size of rev_valarray = 3
The values in rev_valarray:
9 4 1

The result of val_array after calling sqrt():
1 2 3

The result after calling pow(val_array, rev_valarray):
1 256 9

The result after calling pow(val_array, 2.0):
1 16 81

The result after calling pow(2.0, val_array):
2 16 512

以上例子全是MSDN的標準例子 , 安裝MSDN吧, 這種問題迎刃而解

9. 在c語言中自然對數怎麼求啊

1、C里直接提供的是 以 e 為底的自然對數 log ,和 以 10 為底的常用對數 log10。

2、常式:

#include<stdio.h>
#include<math.h>
doubleloga(doublen,doublebase);
intmain(void)
{
doublea,b,c;
a=log(exp(1));
b=log10(10);
c=loga(100,5);
printf("%lf%lf%lf",a,b,c);
}
doubleloga(doublen,doublebase)
{returnlog(n)/log(base);}