跳轉到內容

C++ 程式設計/程式碼/標準 C 庫/數學

來自華夏公益教科書,自由的教科書

標準 C 數學

[編輯 | 編輯原始碼]

本節將介紹 C 標準庫的數學元素。

語法
#include <cstdlib>
int abs( int num );

abs() 函式返回 num 的絕對值。例如

int magic_number = 10;
cout << "Enter a guess: ";
cin >> x;
cout << "Your guess was " << abs( magic_number - x ) << " away from the magic number." << endl;
相關主題
fabs - labs
語法
#include <cmath>
double acos( double arg );

acos() 函式返回 arg 的反餘弦,其範圍為 [0, pi]。arg 應介於 -1 和 1 之間。如果 arg 超出此範圍,acos() 將返回 NAN 並引發浮點異常。

相關主題
asin - atan - atan2 - cos - cosh - sin - sinh - tan - tanh
語法
#include <cmath>
double asin( double arg );

asin() 函式返回 arg 的反正弦,其範圍為 [-pi/2, +pi/2]。arg 應介於 -1 和 1 之間。如果 arg 超出此範圍,asin() 將返回 NAN 並引發浮點異常。

相關主題
acos - atan - atan2 - cos - cosh - sin - sinh - tan - tanh
語法
#include <cmath>
double atan( double arg );

atan() 函式返回 arg 的反正切,其範圍為 [-pi/2, +pi/2]。

相關主題
acos - asin - atan2 - cos - cosh - sin - sinh - tan - tanh
語法
#include <cmath>
double atan2( double y, double x );

atan2() 函式計算 y/x 的反正切,使用引數的符號計算返回值的象限。

相關主題
acos - asin - atan - cos - cosh - sin - sinh - tan - tanh
語法
#include <cmath>
double ceil( double num );

ceil() 函式返回不小於 num 的最小整數。例如

y = 6.04;
x = ceil( y );

將把 x 設定為 7.0。

相關主題
floor - fmod
語法
#include <cmath>
float cos( float arg );
double cos( double arg );
long double cos( long double arg );

cos() 函式返回 arg 的餘弦,其中 arg 以弧度表示。cos() 的返回值範圍為 [-1,1]。如果 arg 是無窮大,cos() 將返回 NAN 並引發浮點異常。

相關主題
acos - asin - atan - atan2 - cosh - sin - sinh - tan - tanh
語法
#include <cmath>
float cosh( float arg );
double cosh( double arg );
long double cosh( long double arg );

cosh() 函式返回arg的雙曲餘弦。

相關主題
acos - asin - atan - atan2 - cos - sin - sinh - tan - tanh
語法
#include <cstdlib>
div_t div( int numerator, int denominator );

div() 函式返回numerator / denominator 操作的商和餘數。div_t 結構在 cstdlib 中定義,至少包含

int quot; // The quotient
int rem; // The remainder

例如,以下程式碼顯示了 x/y 的商和餘數

div_t temp;
temp = div( x, y );
printf( "%d divided by %d yields %d with a remainder of %d\n",
  x, y, temp.quot, temp.rem );
相關主題
ldiv
語法
#attrid <cmath>
double exp( double arg );

exp() 函式返回 e (2.7182818) 的arg次方。

相關主題
log - pow - sqrt
語法
#include <cmath>
double fabs( double arg );

fabs() 函式返回arg的絕對值。

相關主題
abs - fmod - labs

floor

[edit | edit source]
語法
#include <cmath>
double floor( double arg );

floor() 函式返回不大於 arg 的最大整數。

// Example for positive numbers
y = 6.04;
x = floor( y );

將導致 x 設定為 6 (double 6.0)。

// Example for negative numbers
y = -6.04;
x = floor( y );

將導致 x 設定為 -7 (double -7.0)。

相關主題
ceil - fmod
語法
#include <cmath>
double fmod( double x, double y );

fmod() 函式返回 x/y 的餘數。

相關主題
ceil - fabs - floor

frexp

[edit | edit source]
語法
#include <cmath>
double frexp( double num, int* exp );

frexp() 函式用於將num分解為兩個部分:一個介於 0.5 和 1 之間的尾數(由函式返回)和一個以exp返回的指數。 科學記數法是這樣的

num = mantissa * (2 ^ exp)
相關主題
ldexp - modf
語法
#include <cstdlib>
long labs( long num );

labs() 函式返回num的絕對值。

相關主題
abs - fabs

ldexp

[edit | edit source]
語法
#include <cmath>
double ldexp( double num, int exp );

ldexp() 函式返回num * (2 ^ exp)。 如果發生溢位,將返回HUGE_VAL

相關主題
frexp - modf
語法
#include <cstdlib>
ldiv_t ldiv( long numerator, long denominator );

測試:adiv_tdiv_tldiv_t

ldiv() 函式返回numerator / denominator 操作的商和餘數。ldiv_t 結構在 cstdlib 中定義,至少包含

long quot;  // the quotient
long rem;   // the remainder
相關主題
div
語法
#include <cmath>
double log( double num );

log() 函式返回num的自然對數(以 e 為底)。如果num為負數,則會出現域錯誤;如果num為零,則會出現範圍錯誤。

為了計算 x 以任意底數 b 為底的對數,可以使用

double answer = log(x) / log(b);
相關主題
exp - log10 - pow - sqrt

log10

[edit | edit source]
語法
#include <cmath>
double log10( double num );

log10() 函式返回num的以 10 為底的對數(或常用對數)。如果num為負數,則會出現域錯誤;如果num為零,則會出現範圍錯誤。

相關主題
log
語法
#include <cmath>
double modf( double num, double *i );

modf() 函式將num拆分為整數和小數部分。它返回小數部分並將整數部分載入到i中。

相關主題
frexp - ldexp
語法
#include <cmath>
double pow( double base, double exp );

pow() 函式返回 baseexp 次方。 如果 base 為零,且 exp 小於或等於零,則會出現域錯誤。 如果 base 為負數,而 exp 不是整數,也會出現域錯誤。 如果發生溢位,則會出現範圍錯誤。

相關主題
exp - log - sqrt
語法
#include <cmath>
double sin( double arg );

如果您不想使用 cmath,您可以編寫 sin 函式,它為:

  1. include <iostream>

using namespace std;

double sin(double x) //sin 函式 {return x-((x*x*x)/6.)+((x*x*x*x*x)/120.);}

int main () {

   double a;
   cin>>a;
 
                                                           cout<<"sin("<<a<<")="<<sin(a*(3.14159/180.))<<endl;

return 0;}


sin() 函式返回 arg 的正弦值,其中 arg 以弧度給出。 sin() 的返回值將在 [-1,1] 範圍內。 如果 arg 是無限的,sin() 將返回 NAN 並引發浮點異常。

相關主題
acos - asin - atan - atan2 - cos - cosh - sinh - tan - tanh
語法
#include <cmath>
double sinh( double arg );

sinh() 函式返回 arg 的雙曲正弦值。

相關主題
acos - asin - atan - atan2 - cos - cosh - sin - tan - tanh
語法
#include <cmath>
double sqrt( double num );

sqrt() 函式返回 num 的平方根。 如果 num 為負數,則會發生域錯誤。

相關主題
exp - log - pow
語法
#include <cmath>
double tan( double arg );

tan() 函式返回 arg 的正切值,其中 arg 以弧度給出。 如果 arg 是無限的,tan() 將返回 NAN 並引發浮點異常。

相關主題
acos - asin - atan - atan2 - cos - cosh - sin - sinh - tanh
語法
#include <cmath>
double tanh( double arg );


/*example*/
#include <stdio.h>
#include <math.h>
int main (){
	double c, p;
	c = log(2.0);
	p = tanh (c);
	printf ("The hyperbolic tangent of %lf is %lf.\n", c, p );
return 0;
}

tanh() 函式返回 arg 的雙曲正切值。

相關主題
acos - asin - atan - atan2 - cos - cosh - sin - sinh - tan
華夏公益教科書