现在的位置: 首页 > 综合 > 正文

深入详解c语言数学函数

2014年02月19日 ⁄ 综合 ⁄ 共 6456字 ⁄ 字号 评论关闭

                                       深入详解c语言数学函数

函数列表:
abs()求绝对值
fabs()求浮点数的绝对值
labs()求长整型数的绝对值
_cabs()计算复数的绝对值
acos()求反余弦
asin()求反正弦
atan()求反正切
atan2()求反正切,按符号判定象限
ceil()求不小于某值的最小整数 (求上界)
cos()求余弦
cosh()求双曲余弦
div()求商和余数
exp()求e的幂
floor()求不大于某值的最大整数 (求下界)
fmod()求模数
frexp()求数的科学表示法形式
ldexp()以科学计数法计算
ldiv()以长整型返回商和余数
log()自然对数
log10()以10为底的自然对数
modf()将一个数分解成整数和小数部分
pow()求幂
sin()求正弦
sinh()求双曲正弦
sqrt()求平方根
tan()求正切
tanh()求双曲正切
_hypot()返回直角三角形斜边的长度

/////////////////////////////////////////////////////////////////////////////////////

#include <stdlib.h>
int abs( int num );
功能: 函数返回参数num.的绝对值。
template<class T>
valarray<T> abs(const valarray<T>& x);
功能:需要额外#include <valarray>。valarray是类似于vector的模板类。
示例:
#include <iostream>                 // for i/o functions
#include <valarray>                 // for valarray
using namespace std;
#define ARRAY_SIZE  10              // array size
typedef valarray<int> INTVALARRAY;  // type for valarray of ints
void main()
{
    INTVALARRAY val_array(ARRAY_SIZE);
    for (int i = 0; i < ARRAY_SIZE; i++)
        val_array[i] = -i;
    cout << "Size of val_array = " << val_array.size() << "/n/n";
    cout << "The values of val_array before calling abs():/n";
    for (i = 0; i < ARRAY_SIZE; i++)
        cout << val_array[i] << "    ";
    cout << "/n/n";
    INTVALARRAY abs_array = abs(val_array);
    cout << "The result of val_array after calling abs():/n";
    for (i = 0; i < ARRAY_SIZE; i++)
        cout << abs_array[i] << "     ";
    cout << "/n/n";
}
输出结果:
Size of val_array = 10

The values of val_array before calling abs():
0    -1    -2    -3    -4    -5    -6    -7    -8    -9

The result of val_array after calling abs():
0     1     2     3     4     5     6     7     8     9

#include <stdlib.h>
long labs( long num );
功能: 函数返回参数num的绝对值。
#include <math.h>
double fabs( double arg );
功能: 函数返回参数arg的绝对值
#include <math.h>
double _cabs( struct _complex z );
功能:计算复数的绝对值,返回值为sqrt( z.x*z.x + z.y*z.y )。
示例:
#include<iostream>
#include<cmath>
using namespace std;
void main()
{
    
 struct _complex number = {3.0, 4.0 };
    double d;
    d = _cabs( number );
    printf( "The absolute value of %f + %fi is %f/n",
           number.x, number.y, d );
}
输出结果:
The absolute value of 3.000000 + 4.000000i is 5.000000

#include <math.h>
double acos( double arg );
功能:函数返回参数arg的反余弦值。参数arg 应当在-1和1之间
template<class T>
    valarray<T> acos(const valarray<T>& x);
#include <math.h>
double asin( double arg );
功能:函数返回参数arg的反正弦值。参数arg 应当在-1和1之间
template<class T>
    valarray<T> asin(const valarray<T>& x);
double atan( double arg );
功能:函数返回参数arg的反正切值
double atan2( double y, double x );
功能:函数计算y/x的反正切值,按照参数的符号计算所在的象限
atan与atan2的区别:
1.atan()计算的返回值在区间[-π/2,π/2]中。
2.atan 2()能根据任意点(x,y)的x,y的符号计算y/x的弧度数。返回值∈[–π,π].
示例:
#include <math.h>
#include <stdio.h>
#include <errno.h>
void main( void )
{
   double x1=3, y1=7, x2=2,y2=3,y;
   y = atan( x1 );
   printf( "Arctangent of %f: %f/n", x1, y );
   y = atan2( x1, x2 );
   printf( "Arctangent of %f / %f: %f/n", x1, x2, y );
   //计算点(3,7)和(2,3)构成的连线的夹角
   printf( "点(3,7)和(2,3)构成的连线的夹角为 %f/n", atan2(7-3,3-2));
}
输出结果:
Arctangent of 3.000000: 1.249046
Arctangent of 3.000000 / 2.000000: 0.982794
点(3,7)和(2,3)构成的连线的夹角为 1.325818

double ceil( double num );
功能: 函数返回参数不小于num 的最小整数。例如,
y = 6.04;
x = ceil( y );
x为7.0.
double cos( double arg );
功能: 函数返回参数arg的余弦值,arg以弧度表示给出。
double exp( double arg );
功能: 函数返回参数returns e (2.7182818) 的arg次幂
double floor( double arg );
功能: 函数返回参数不大于arg的最大整数。例如,
    y = 6.04;
    x = floor( y );
x的值为6.0.
double fmod( double x, double y );
功能: 函数返回参数x/y的余数。
double frexp( double num, int *exp );
功能: 函数将参数num 分成两部分: 0.5 和1之间的尾数(由函数返回)并返回以2为底的指数exp。转换成如下的科学计数法形式:
num = mantissa * (2 ^ exp)
示例:
double x, y;
int n;
x = 16.4;
y = frexp( x, &n );
printf( "frexp( %f, &n ) = %f, n = %d/n", x, y, n );
输出结果:
frexp( 16.400000, &n ) = 0.512500, n = 5

double ldexp( double num, int exp );
功能: 函数返回参数num * (2 ^ exp)。如果发生溢出返回HUGE_VAL。
示例:
double x = 4.0, y;
   int p = 3;

   y = ldexp( x, p );
   printf( "%2.1f times two to the power of %d is %2.1f/n", x, p, y );
输出结果:
4.0 times two to the power of 3 is 32.0

#include <stdlib.h>
div_t div( long numerator, long denominator );
功能: 函数返回参数numerator / denominator的商和余数。结构类型 ldiv_t 定义在stdlib.h中:

    long quot;    // 商数
    long rem;     // 余数
示例:
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
void main()
{
    div_t div_result;
    div_result=div(15,6);
    printf( "The quotient is %d, and the remainder is %d/n",
           div_result.quot, div_result.rem );
}
输出结果:
The quotient is 2, and the remainder is 3

#include <stdlib.h>
ldiv_t ldiv( long numerator, long denominator );
功能: 函数返回参数numerator / denominator的商和余数。结构类型 ldiv_t 定义在stdlib.h中:
    long quot;    // 商数
    long rem;     // 余数

double log( double num );
功能: 函数返回参数num的自然对数。如果num为负,产生域错误;如果num 为零,产生范围错误
double log10( double num );
功能: 函数返回参数num以10为底的对数。如果num为负,产生域错误;如果num 为零,产生范围错误。
double modf( double num, double *i );
功能: 函数将参数num 分割为整数和小数,返回小数部分并将整数部分赋给i
示例:
#include <math.h>
#include <stdio.h>
void main( void )
{
   double x, y, n;
   x = -14.87654321;      /* Divide x into its fractional */
   y = modf( x, &n );     /* and integer parts            */
   printf( "For %f, the fraction is %f and the integer is %.f/n",
           x, y, n );
}
输出结果:
For -14.876543, the fraction is -0.876543 and the integer is -14

double pow( double base, double exp );
功能: 函数返回以参数base 为底的exp 次幂。
-------------------------------------------------
Values of base and exp Return Value of pow 
x < > 0 and y = 0.0           1
x = 0.0 and y = 0.0           1
x = 0.0 and y < 0             INF
-------------------------------------------------
示例:
   double x = 2.0, y = 3.5, z;
   z = pow( x, y );
   printf( "%.1f to the power of %.1f is %.1f/n", x, y, z );
输出结果:
2.0 to the power of 3.5 is 11.3

double sin( double arg );
功能: 函数返回参数arg的正弦值,arg以弧度表示给出。
double sinh( double arg );
功能: 函数返回参数arg的双曲正弦值。
double sqrt( double num );
功能: 函数返回参数num的平方根。如果num为负,产生域错误
double tan( double arg );
功能: 函数返回参数arg的正切值,arg以弧度表示给出。
template<class T>
    valarray<T> tan(const valarray<T>& x);
double tanh( double arg );
功能: 函数返回参数arg的双曲正切值。
double _hypot( double x, double y );
功能:返回直角三角形斜边的长度z=sqrt(x^2+y^2).

下面介绍两个与数学函数相关的非数学函数srand()和rand().

void srand( unsigned int seed );给rand()函数设定种子
int rand( void );产生一个伪随机unsigned int 整数
1.如果srand每次输入的数值是一样的,那么每次运行产生的随机数也是一样的.因此seed一般由时间函数GetCurrentTime()(srand((UINT)GetCurrentTime()))或time()函数(srand( (unsigned)time(NULL)) )得到现在的系统时间来设置。(系统的时间从1970.1.1午夜算起,单位为秒)当然,也可使用自定义的更好的数据做种子,而不是系统时钟。

2.rand的内部实现是用线性同余法做的,它不是真的随机数,只不过是因为其周期特别长,所以有一定的范围里可看成是随机的,式子如下:
         rand = rand*const_1 + c_var; //srand函数就是给它的第一个rand值。

3.srand 和 rand 应该组和使用。一般来说,srand 是对 rand 进行设置。比如:
           srand((UINT)GetCurrentTime());
           int x = rand() % 100; //生成 [0,100)之间的随机数。
如果要产生整数a到整数b之间([a,b))的随机数, 有以下式子:
           rand()%(b-a+1)+a;
但是还有更好的方法:j=((b-a+1)*rand()/(RAND_MAX+1))+a;//生成 [0,100)之间的随机数。
                                                      //RAND_MAX=0x7fff
4. 示例:
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
void main( void )
{
   int i;

   /* Seed the random-number generator with current time so that
    * the numbers will be different every time we run.
    */
   srand( (unsigned)time( NULL ) );
   /* Display 10 numbers. */
   int a=15,b=150,j;
   for( i = 0;   i < 10;i++ )
   {
       j=((b-a+1)*rand()/(RAND_MAX+1))+a;
    printf( "  %6d/n",j);
   }
}         
输出结果:
     38
    114
    150
     19
    136
     77
    103
    120
    104
    109

 

抱歉!评论已关闭.