1.函数介绍:

abs()acos()asin()atan()atan2()ceil()cos()cosh()exp()frexp()ldexp()log()log10()pow()sin()sinh()sqrt()tan()tanh()

 

abs(计算整型数的绝对值)

相关函数

labs, fabs

表头文件

#include<stdlib.h>

定义函数

int abs (int j)

函数说明

abs()用来计算参数j的绝对值,然后将结果返回。

返回值

返回参数j的绝对值结果。

范例

#ingclude <stdlib.h>
main(){
int ansert;
answer = abs(-12);
printf("|-12| = %d\n", answer);
}

执行

|-12| = 12

 

acos(取反余弦函数数值)

相关函数

asin , atan , atan2 , cos , sin , tan

表头文件

#include <math.h>

定义函数

double acos (double x);

函数说明

acos()用来计算参数x的反余弦值,然后将结果返回。参数x范围为-1至1之间,超过此范围则会失败。

返回值

返回0至PI之间的计算结果,单位为弧度,在函数库中角度均以弧度来表示。

错误代码

EDOM参数x超出范围。

附加说明

使用GCC编译时请加入-lm。

范例

#include <math.h>
main (){
double angle;
angle = acos(0.5);
printf("angle = %f\n", angle);
}

执行

angle = 1.047198

 

asin(取反正弦函数值)

相关函数

acos , atan , atan2 , cos , sin , tan

表头文件

#include <math.h>

定义函数

double asin (double x)

函数说明

asin()用来计算参数x的反正弦值,然后将结果返回。参数x范围为-1至1之间,超过此范围则会失败。

返回值

返回-PI/2之PI/2之间的计算结果。

错误代码

EDOM参数x超出范围

附加说明

使用GCC编译时请加入-lm

范例

#include<math.h>
main()
{
double angle;
angle = asin (0.5);
printf("angle = %f\n",angle);
}

执行

angle = 0.523599

 

atan(取反正切函数值)

相关函数

acos,asin,atan2,cos,sin,tan

表头文件

#include<math.h>

定义函数

double atan(double x);

函数说明

atan()用来计算参数x的反正切值,然后将结果返回。

返回值

返回-PI/2至PI/2之间的计算结果。

附加说明

使用GCC编译时请加入-lm

范例

#include<math.h>
main()
{
double angle;
angle =atan(1);
printf("angle = %f\n",angle);
}

执行

angle = 1.570796

 

atan2(取得反正切函数值)

相关函数

acos,asin,atan,cos,sin,tan

表头文件

#include<math.h>

定义函数

double atan2(double y,double x);

函数说明

atan2()用来计算参数y/x的反正切值,然后将结果返回。

返回值

返回-PI/2 至PI/2 之间的计算结果。

附加说明

使用GCC编译时请加入-lm。

范例

#include<math.h>
main()
{
double angle;
angle = atan2(1,2);
printf("angle = %f\n", angle);
}

执行

angle = 0.463648

 

ceil(取不小于参数的最小整型数)

相关函数

fabs

表头文件

#include <math.h>

定义函数

double ceil (double x);

函数说明

ceil()会返回不小于参数x的最小整数值,结果以double形态返回。

返回值

返回不小于参数x的最小整数值。

附加说明

使用GCC编译时请加入-lm。

范例

#include<math.h>
main()
{
double value[ ]={4.8,1.12,-2.2,0};
int i;
for (i=0;value[i]!=0;i++)
printf("%f=>%f\n",value[i],ceil(value[i]));
}

执行

4.800000=>5.000000
1.120000=>2.000000
-2.200000=>-2.000000

 

cos(取余玄函数值)

相关函数

acos,asin,atan,atan2,sin,tan

表头文件

#include<math.h>

定义函数

double cos(double x);

函数说明

cos()用来计算参数x 的余玄值,然后将结果返回。

返回值

返回-1至1之间的计算结果。

附加说明

使用GCC编译时请加入-lm。

范例

#include<math.h>
main()
{
double answer = cos(0.5);
printf("cos (0.5) = %f\n",answer);
}

执行

cos(0.5) = 0.877583

 

cosh(取双曲线余玄函数值)

相关函数

sinh,tanh

表头文件

#include<math.h>

定义函数

double cosh(double x);

函数说明

cosh()用来计算参数x的双曲线余玄值,然后将结果返回。数学定义式为:(exp(x)+exp(-x))/2。

返回值

返回参数x的双曲线余玄值。

附加说明

使用GCC编译时请加入-lm。

范例

#include<math.h>
main()
{
double answer = cosh(0.5);
printf("cosh(0.5) = %f\n",answer);
}

执行

cosh(0.5) = 1.127626

 

exp(计算指数)

相关函数

log,log10,pow

表头文件

#include<math.h>

定义函数

double exp(double x);

函数说明

exp()用来计算以e为底的x次方值,即ex值,然后将结果返回。

返回值

返回e的x次方计算结果。

附加说明

使用GCC编译时请加入-lm。

范例

#include<math.h>
main()
{
double answer;
answer = exp (10);
printf("e^10 =%f\n", answer);
}

执行

e^10 = 22026.465795

 

frexp(将浮点型数分为底数与指数)

相关函数

ldexp,modf

表头文件

#include<math.h>

定义函数

double frexp( double x, int *exp);

函数说明

frexp()用来将参数x 的浮点型数切割成底数和指数。底数部分直接返回,指数部分则借参数exp 指针返回,将返回值乘以2 的exp次方即为x的值。

返回值

返回参数x的底数部分,指数部分则存于exp指针所指的地址。

附加说明

使用GCC编译时请加入-lm。

范例

#include <math.h>
main()
{
int exp;
double fraction;
fraction = frexp (1024,&exp);
printf("exp = %d\n",exp);
printf("fraction = %f\n", fraction);
}

执行

exp = 11
fraction = 0.500000 /* 0.5*(2^11)=1024*/

 

ldexp(计算2的次方值)

相关函数

frexp

表头文件

#include<math.h>

定义函数

double ldexp(double x,int exp);

函数说明

ldexp()用来将参数x乘上2的exp次方值,即x*2exp。

返回值

返回计算结果。

附加说明

使用GCC编译时请加入-lm。

范例:

/* 计算3*(2^2)=12 */
#include<math.h>
main()
{
int exp;
double x,answer;
answer = ldexp(3,2);
printf("3*2^(2) = %f\n",answer);
}

执行

3*2^(2) = 12.000000

 

log(计算以e 为底的对数值)

相关函数

exp,log10,pow

表头文件

#include <math.h>

定义函数

double log (double x);

函数说明

log()用来计算以e为底的x 对数值,然后将结果返回。

返回值

返回参数x的自然对数值。

错误代码

EDOM 参数x为负数,ERANGE 参数x为零值,零的对数值无定义。

附加说明

使用GCC编译时请加入-lm。

范例

#include<math.h>
main()
{
double answer;
answer = log (100);
printf("log(100) = %f\n",answer);
}

执行

log(100) = 4.605170

 

log10(计算以10 为底的对数值)

相关函数

exp,log,pow

表头文件

#include<math.h>

定义函数

double log10(double x);

函数说明

log10()用来计算以10为底的x对数值,然后将结果返回。

返回值

返回参数x以10为底的对数值。

错误代码

EDOM参数x为负数。RANGE参数x为零值,零的对数值无定义。

附加说明

使用GCC编译时请加入-lm。

范例

#include<math.h>
main()
{
double answer;
answer = log10(100);
printf("log10(100) = %f\n",answer);
}

执行

log10(100) = 2.000000

 

pow(计算次方值)

相关函数

exp,log,log10

表头文件

#include<math.h>

定义函数

double pow(double x,double y);

函数说明

pow()用来计算以x为底的y次方值,即xy值,然后将结果返回。

返回值

返回x的y次方计算结果。

错误代码

EDOM 参数x为负数且参数y不是整数。

附加说明

使用GCC编译时请加入-lm。

范例

#include <math.h>
main()
{
double answer;
answer =pow(2,10);
printf("2^10 = %f\n", answer);
}

执行

2^10 = 1024.000000

 

sin(取正玄函数值)

相关函数

acos,asin,atan,atan2,cos,tan

表头文件

#include<math.h>

定义函数

double sin(double x);

函数说明

sin()用来计算参数x的正玄值,然后将结果返回。

返回值

返回-1 至1之间的计算结果。

附加说明

使用GCC编译时请加入-lm。

范例

#include<math.h>
main()
{
double answer = sin (0.5);
printf("sin(0.5) = %f\n",answer);
}

执行

sin(0.5) = 0.479426

 

sinh(取双曲线正玄函数值)

相关函数

cosh,tanh

表头文件

#include<math.h>

定义函数

double sinh( double x);

函数说明

sinh()用来计算参数x的双曲线正玄值,然后将结果返回。数学定义式为:(exp(x)-exp(-x))/2。

返回值

返回参数x的双曲线正玄值。

附加说明

使用GCC编译时请加入-lm。

范例

#include<math.h>
main()
{
double answer = sinh (0.5);
printf("sinh(0.5) = %f\n",answer);
}

执行

sinh(0.5) = 0.521095

 

sqrt(计算平方根值)

相关函数

hypotq

表头文件

#include<math.h>

定义函数

double sqrt(double x);

函数说明

sqrt()用来计算参数x的平方根,然后将结果返回。参数x必须为正数。

返回值

返回参数x的平方根值。

错误代码

EDOM 参数x为负数。

附加说明

使用GCC编译时请加入-lm。

范例

/* 计算200的平方根值*/
#include<math.h>
main()
{
double root;
root = sqrt (200);
printf("answer is %f\n",root);
}

执行

answer is 14.142136

 

tan(取正切函数值)

相关函数

atan,atan2,cos,sin

表头文件

#include <math.h>

定义函数

double tan(double x);

函数说明

tan()用来计算参数x的正切值,然后将结果返回。

返回值

返回参数x的正切值。

附加说明

使用GCC编译时请加入-lm。

范例

#include<math.h>
main()
{
double answer = tan(0.5);
printf("tan (0.5) = %f\n",answer);
}

执行

tan(0.5) = 0.546302

 

tanh(取双曲线正切函数值)

相关函数

cosh,sinh

表头文件

#include<math.h>

定义函数

double tanh(double x);

函数说明

tanh()用来计算参数x的双曲线正切值,然后将结果返回。数学定义式为:sinh(x)/cosh(x)。

返回值

返回参数x的双曲线正切值。

附加说明

使用GCC编译时请加入-lm。

范例

#include<math.h>
main()
{
double answer = tanh(0.5);
printf("tanh(0.5) = %f\n",answer);
}

执行

tanh(0.5) = 0.462117

 

2.企业面试实战演练

题目如下:

现有1个点和10000个位置半径各不同的圆,为了判断改点被包含在哪些圆内,需要一个函数判断点(px,py)是否于圆心(x,y)半径r的圆内,请尽快优化运行速度。

函数原型:

bool IsPointInCircle(int px,int py,int x,int y,int r);

流程设计如下:

采用模块化的设计思想,我们创建1个函数

int CreateCircle(int n);

CreateCircle函数功能就是接受参数n并创建n个圆圈,圆心坐标随机产生,半径也随机产生

将创建好的圆圈与点(px,py)进行游戏中的 "碰撞检测"

我们加上本专题的第一篇博客测试程序运行时间。经过本人测试发现10000个圆圈根本就测试不出时间,所以我们在项目中改成了1000,000个圆圈。

另外C语言中没有内建类型bool 所以我们需要换成int

项目实现

#include <stdio.h>

#include <math.h>

#include <time.h>

#include <stdlib.h>

int main(void)

{

 	int CreateCircle(int n,int px,int py);

 	int IsPointInCircle(int px,int py,int x,int y,int r);

 	clock_t t;

 	printf ("Begin clock...\n");

 	t = clock();//第一个clock() t表示从程序启动到现在这个时刻的时间

	CreateCircle(1000000,50,25);

 	t = clock() - t;//第二次调用clock()减去第一次获得的t的差值为两次掐表的间隔

	printf ("\nIt took %d clicks (%f seconds) to test.\n",t,((float)t)/CLOCKS_PER_SEC);

 	return 0;

}

int IsPointInCircle(int px,int py,int x,int y,int r)

 {

	 double distance = sqrt(pow(py-y,2)+pow(px-x,2));

 	if(distance -r >0.00001) //减去double类型的误差 再比较大小

		return 0; //返回0 便是不在圆内

	else

	 return 1; //返回1 在圆内

}

int CreateCircle(int n,int px,int py)

{

 	int r ; //r用来缓冲临时产生圆圈的半径

	int x; //x用来缓冲临时产生圆圈的圆心横坐标

	int y; //y用来缓冲临时产生圆圈的圆心纵坐标

	int iCount = 0; //计数产生圆圈的个数

	srand(time(NULL)); //初始化随即种子

	while(iCount < n)

 	{

 	x = rand()%1000; // rand()函数就是返回一个随机数

	y = rand()%1000;

 	r = rand()%100;

 	if(IsPointInCircle(px,py,x,y,r)) //如果在圆内

  		 printf("%din",iCount); //打印出他在圆内

   	++iCount;

 	}

}

3.在各个平台的运行的情况

在RHEL7上

在REHL6上

在Solaris11上

在MAC上

Unix/Linux环境C编程入门教程(31) 数学函数带你战胜企业面试的更多相关文章

  1. Unix/Linux环境C编程入门教程(32) 环境变量那些事儿

    1. getenv() putenv()setenv()函数介绍 getenv(取得环境变量内容) 相关函数 putenv,setenv,unsetenv 表头文件 #include<stdli ...

  2. Unix/Linux环境C编程入门教程(19)Red Hat Entetprise Linux 7.0环境搭建

    位架构,包括英特尔X-86_64.Power和s390.动态定时能力将降低内核内部中断数量,Open vSwitch 2.0功能可调节虚拟机之间的流量.RHEL 7中默认的文件系统是XFS,包含了一个 ...

  3. Unix/Linux环境C编程入门教程(5) Red Hat Enterprise Linux(RHEL)环境搭建

    Unix/Linux版本众多,我们推荐Unix/Linux初学者选用几款典型的Unix/Linux操作系统进行学习. 通过./a.out ./Y.out执行出结果,证明C++程序编译成功,也就说明li ...

  4. Unix/Linux环境C编程入门教程(4) Debian Linux环境搭建

    Unix/Linux版本众多,我们推荐Unix/Linux初学者选用几款典型的Unix/Linux操作系统进行学习. 1.广义的Debian是指一个致力于创建自由操作系统的合作组织及其作品,由于Deb ...

  5. Unix/Linux环境C编程入门教程(3) Oracle Linux 环境搭建

    Unix/Linux版本众多,我们推荐Unix/Linux初学者选用几款典型的Unix/Linux操作系统进行学习. 2010年9月,Oracle Enterprise Linux发布新版内核--Un ...

  6. Unix/Linux环境C编程入门教程(2) CentOS环境搭建

    Unix/Linux版本众多,我们推荐Unix/Linux初学者选用几款典型的Unix/Linux操作系统进行学习. 本文就带大家来安装CentOS并且配置好C/C++开发环境,这是一款Linux. ...

  7. Unix/Linux环境C编程入门教程(1) Solaris 11 64bit环境搭建

    Unix/Linux版本众多,我们推荐Unix/Linux初学者选用几款典型的Unix/Linux操作系统进行学习. 本文就带大家来安装Solaris 11 64位并且配置好C/C++开发环境 本文所 ...

  8. Unix/Linux环境C编程入门教程(22) C/C++如何获取程序的运行时间

    1.问:知道程序运行时间我们可以做什么? 在<C++应用程序性能优化>一书中,如果大家读过相信大家一定对性能优化这一块非常上心,文中总是对优化前后的时间对比非常直观给我们一个感受. 那么我 ...

  9. Unix/Linux环境C编程入门教程(21) 各个系统HelloWorld跑起来效果如何?

    Unix/Linux家族人员众多,我们无法一一讲解如何配置环境. 本文选定我们在前面安装的RHEL6 RHEL7 MAC10.9.3 Solaris11如何跑起来helloworld RHEL 6 上 ...

随机推荐

  1. truncate 、delete与drop三者的异同

    相同点: 1.truncate和不带where子句的delete.以及drop都会删除表内的数据. 2.drop.truncate都是DDL语句(数据定义语言),执行后会自动提交. 不同点: 1. t ...

  2. Dave(正方形能围成的最大点数)

    Dave Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)Total Submis ...

  3. ABAP - 日期格式转换 &amp; ABAP经常使用日期处理函数

    ABAP - 日期格式转换 如今提供下面一些日期格式转换的函数: Below are several FMs which can be used to convert date format. 1. ...

  4. RelativeLayout经常使用属性介绍

    以下介绍一下RelativeLayout用到的一些重要的属性: 第一类:属性值为true或false     android:layout_centerHrizontal                ...

  5. [Regular Expressions] Find a String that Precedes Another String ?= , ?!

    Let's image tow cases for the following string: var str = `foo foobar foobaz fooboo` First of all: w ...

  6. Remastersys备份linux系统ISO镜像

    1. 安装Remastersys 利用Ubuntu Software Center安装,修改sources.list文件,在文件末尾加入下面三行,添加软件源, #Rsudo remastersys d ...

  7. css1-颜色和长度

    <!DOCTYPE html>CSS1-颜色和长度 <style>div{ /*颜色*/ color:#f00; /*前景色*/ background:#00f; /*背景色* ...

  8. JS 事件对象和事件冒泡

    1.事件对象 js的事件对象中保存了当前被触发事件的一些相关的属性信息,如事件源.事件发生时的鼠标位置.事件按键等. 事件对象的获取方法: IE中可以window.event直接获取,而Firefox ...

  9. 关于arm-linux-gcc的安装与配置

    在嵌入式开发中我们经常会用到arm-linux-gcc来编译我们的应用程序.作为arm-linux-gcc的入门,我们先看看如何安装arm-linux-gcc. 安装arm-linux-gcc还是比较 ...

  10. ios开发中的基本设计模式

    (一)代理模式应用场景:当一个类的某些功能需要由别的类来实现,但是又不确定具体会是哪个类实现.优势:解耦合敏捷原则:开放-封闭原则实例:tableview的 数据源delegate,通过和protoc ...