C Standard Library 头文件 math.h 中函数实例

/************************************************************************************************
 * 名  称: main.c
 * 功  能:C标准库,头文件math.h学习,笔记
 * 描  述:math.h中 提供的函数说明
 * 作  者:JarvisChu
 * 时  间:2011-7-16 创建
 *************************************************************************************************/
#include <stdio.h>
#include <math.h>

int main()
{
    double iptr;
    double d;
    int extr;
    //int i=0;

    //绝对值函数,int abs(int x); double fabs(double x);
    printf("abs:%d\n",abs(-1));
    printf("fabs:%f\n\n",fabs(-1.45));

/**********************************三角函数*********************************/
    //正弦函数 double sin(double x);  x为弧度,其他三角函数类似
    //没有cot函数
    printf("sin: %f\n",sin(1));
    printf("cos: %f\n",cos(1));
    printf("tan: %f\n", tan(1));
    printf("arcsin: %f\n",asin(1/2));
    printf("arccos: %f\n",acos(1/2));
    printf("arctan: %f\n\n",atan(1));

/**********************************双曲函数*********************************/
    //双曲正弦 double sinh(double x);
    //双曲余弦 double cosh(double x);
    printf("sinh: %f\n",sinh(1));
    printf("cosh: %f\n",cosh(1));
    printf("tanh: %f\n\n",tanh(1));

/**********************************科学计算*********************************/
    //e^x
    printf("e^x: %f\n",exp(1));
    //log 10
    printf("log10: %f\n", log10(100));
    //log e
    printf("loge: %f\n", log(4));

    //x的y次方
    printf("x^y: %f\n",pow(12,2));
    //根号
    printf("sqrt: %f\n",sqrt(4));

    //求余数
    printf("fmod: %f\n",fmod(4.2,2));

    //求不大于x的最大整数
    printf("floor: %f\n\n",floor(4.1));

    //把double型变量val分解成整数部分iptr和小数部分(返回值),
    //double modf(double val,double* iptr);
    printf("modf:  整数%f;小数%f\n",iptr,modf(3.14,&iptr));

    //把double型变量val分解:val = x* (2^n),n存放在eptr所指变量中,返回x(0.5 <=  x < 1)
    d = frexp(5,&extr);
    printf("5 = %2f * 2^%d\n",d,extr);

    //随机数
    printf("rand: %d\n",rand());
    return 0;
}

posted @ 2011-07-16 20:44  JarvisChu  阅读(236)  评论(0编辑  收藏  举报