C++中的常用数学函数

类别:编程语言 点击:0 评论:0 推荐:

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
     double pi,e,n;
     int i;
     pi = acos(-1.0);
     e = exp(1.0);
 
     cout << "abs(-10) = " << abs(-10) << "\n";//求整数的绝对值
     cout << "fabs(-10.8) = " << fabs(-10.8) << "\n";
     cout << "asin(0.5) = " << asin(0.5) << " (π/6)\n";
     cout << "atan(1.0) = " << atan(1.0) << " (π/4)\n";
     cout << "atan2(1.0,√3) = " << atan2(1.0,sqrt(3.0)) << " (π/6)\n";
     cout << "sin(π/2) = " << sin(pi / 2.0) << "\n";
     cout << "sinh(π/2) = " << sinh(pi /2.0 ) << "\n";
     cout << "cos(π/2) = " << cos(pi / 2.0) << " (≈0.0)\n";
     cout << "cosh(π/2) = " << cosh(pi /2.0 ) << "\n";
     cout << "tan(π/4) = " << tan(pi / 4) << "\n";
     cout << "tanh(π/4) = " << tanh(pi /4 ) << "\n";
     cout << "ceil(1.3) = " << ceil(1.3) << "\n";
     cout << "ceil(-1.3) = " << ceil(-1.3) << "\n";
     cout << "floor(1.3) = " << floor(1.3) << "\n";
     cout << "floor(-1.3) = " << floor(-1.3) << "\n";
     cout << "log(e*e) = " << log(e*e) << "\n";
     cout << "log10(1000.0) = " << log10(1000.0) << "\n";
     cout << "fmod(10.2,3.0) = " << fmod(10.2,3.0) << "\n";
     cout << "fmod(-10.2,3.0) = " << fmod(-10.2,3.0) << "\n";
     cout << "pow(25.0,2.0) = " << pow(25.0,2.0) << "\n";
     cout << "modf(-12.3435,&n) = " << modf(-12.3435,&n) << ",";
     cout << "n = " << n << "\t-12.3435 = n + modf(-12.3435,&n)\n";
     cout << "frexp(17.0,&i) = " << frexp(17.0,&i) << ",";
     cout << "i = " << i << "\t17.0 = frexp(17.0,&i)*2^i\n";
     cout << "ldexp(0.53125,5) = " << ldexp(0.53135,5) << "\tldexp(0.53125,5)=0.53125*2^5\n";

     return 0;
}
/*
输出结果:
abs(-10) = 10
fabs(-10.8) = 10.8
asin(0.5) = 0.523599 (π/6)
atan(1.0) = 0.785398 (π/4)
atan2(1.0,√3) = 0.523599 (π/6)
sin(π/2) = 1
sinh(π/2) = 2.3013
cos(π/2) = 6.12303e-017 (≈0.0)
cosh(π/2) = 2.50918
tan(π/4) = 1
tanh(π/4) = 0.655794
ceil(1.3) = 2
ceil(-1.3) = -1
floor(1.3) = 1
floor(-1.3) = -2
log(e*e) = 2
log10(1000.0) = 3
fmod(10.2,3.0) = 1.2
fmod(-10.2,3.0) = -1.2
pow(25.0,2.0) = 625
modf(-12.3435,&n) = -0.3435,n = -12 -12.3435 = n + modf(-12.3435,&n)
frexp(17.0,&i) = 0.53125,i = 5 17.0 = frexp(17.0,&i)*2^i
ldexp(0.53125,5) = 17.0032 ldexp(0.53125,5)=0.53125*2^5
*/

/*
abs; acos; asin; atan; atan2; ceil;cos; cosh; exp;
fabs; floor; fmod; frexp; ldexp; log;log10; modf; pow;
sin; sinh; sqrt;tan; tanh;

acosf; asinf; atanf; atan2f; ceilf; cosf; coshf; expf;
fabsf; floorf; fmodf; frexpf; ldexpf; logf;
log10f; modff; powf; sinf; sinhf; sqrtf; tanf; tanhf;

acosl; asinl; atanl; atan2l; ceill; cosl; coshl; expl;
fabsl; floorl; fmodl; frexpl; ldexpl; logl;
log10l; modfl; powl; sinl; sinhl; sqrtl; tanl; tanhl;
*/

本文地址:http://com.8s8s.com/it/it26828.htm