C语言中math.h中常用的函数
1.绝对值
①函数原型: int abs(int x);
函数功能: 求整数x的绝对值
int number=-1234;
abs(number);
②函数原型:double fabs(double x);
函数功能:求浮点数x的绝对值.
float number=-1234.0;
fabs(number);
③函数原型:double cabs(struct complex znum)
函数功能:求复数的绝对值
参数说明:zuum为用结构struct complex表示的复数,定义如下:
struct complex
{
double m;
double n;
}
#include <stdio.h>
#include <math.h>
int main()
{
struct complex z;
double val;
z.x=2.0;
z.y=1.0;
val=cabs(z);
printf("The absolute value of %.2lfi %.2lfj is %.2lf",z.x,z.y,val);
return 0;
}
2.取整和取余
函数原型: double ceil(double num)
函数功能: 得到不小于num的最小整数
函数返回: 用双精度表示的最小整数
函数原型: double floor(double x);
函数功能: 求出不大于x的最大整数.
函数返回: 该整数的双精度实数
函数原型:double fmod (double x, double y); 返回两参数相除x/y的余数,符号与x相同。如果y为0,则结果与具体的额实现有关
int main()
{
double number=123.54;
double down,up;
down=floor(number);
up=ceil(number);
printf("original number %10.2lf",number);//123.54
printf("number rounded down %10.2lf",down); //123
printf("number rounded up %10.2lf",up); //124
return 0;
}
函数名称: modf
函数原型: double modf(double val,double *iptr);
函数功能: 把双精度数val分解为整数部分和小数部分,把整数部分存到iptr指向的单元.
函数返回: val的小数部分
参数说明: val 待分解的数
所属文件: <math.h>
使用范例:
#include <math.h>
#include <stdio.h>
int main()
{
double fraction,integer;
double number=100000.567;
fraction=modf(number,&integer);
printf("The whole and fractional parts of %lf are %lf and %lf",number,integer,fraction);
return 0;
}
6.指数和对数
函数原型: double exp(double x);
函数功能: 求e的x次幂
函数原型: double fmod(double x,double y);
函数功能: 求整数x/y的余数
函数原型: double frexp(double val,int *eptr);
函数功能: 把双精度数val分解为数字部分(尾数)x和以2为底的指数n,即val=x*2^n,n存放在eptr指向的变量中.
函数名称: pow
函数原型: double pow(double x,double y);
函数功能: 计算以x为底数的y次幂,即计算x^y的值.
函数返回: 计算结果
参数说明: x-底数,y-幂数
所属文件: <math.h>
使用范例:
#include <math.h>
#include <stdio.h>
int main()
{
double x=2.0,y=3.0;
printf("%lf raised to %lf is %lf",x,y,pow(x,y));
return 0;
}
函数原型: double sqrt(double x);
函数功能: 计算x的开平方.
函数返回: 计算结果
参数说明: x>=0
所属文件: <math.h>
使用范例:
#include <math.h>
#include <stdio.h>
int main()
{
double x=4.0,result;
result=sqrt(x);
printf("The square root of %lf is %lf",x,result);
return 0;
}
//
log(10) 以 e 为底的 10 的对数;
log10(100) 以 10 为底的 100 的对数;
如果要算别的对数 log(8) / log(2) 以 2 为底的 8 的对数;
如果要计算自然常数 e exp(1);
//
函数原型: double log(double x);
函数功能: 求logeX(e指的是以e为底),即计算x的自然对数(ln X)
函数返回: 计算结果
参数说明:
所属文件: <math.h>
使用范例:
#include <math.h>
#include <stdio.h>
int main()
{
double result;
double x=8.6872;
result=log(x);
printf("The natural log of %lf is %lf",x,result);
return 0;
}
函数名称: log10
函数原型: double log10(double x);
函数功能: 求log10x(10指的是以10为底).计算x的常用对数
函数返回: 计算结果
参数说明:
所属文件: <math.h>
使用范例:
#include <math.h>
#include <stdio.h>
int main()
{
double result;
double x=800.6872;
result=log10(x);
printf("The common log of %lf is %lf",x,result);
return 0;
}
#include <stdio.h>
#include <math.h>
int main()
{
double result;
double x=4.0;
result=exp(x);
printf("'e' raised to the power of %lf(e^%lf)=%lf",x,x,result);
return 0;
}
#include <math.h>
#include <stdio.h>
int main()
{
double mantissa,number;
int exponent;
number=8.0;
mantissa=frexp(number,&exponent);
printf("The number %lf is",number);
printf("%lf times two to the",mantissa);
printf("power of %d",exponent);
return 0;
}
7.标准化浮点数
函数原型:double modf (double x, double *ip);
函数功能:将参数的整数部分通过指针回传, 返回小数部分,整数部分保存在*ip中
函数原型: double ldexp(double x,int exponent)
函数功能: 计算x*2的exponent次幂,即2*pow(2,exponent)的数值
#include <stdio.h>
#include <math.h>
int main()
{
double value;
double x=2;
value=ldexp(x,3);
printf("The ldexp value is: %lf",value);
return 0;
}
8.多项式
函数名称: poly
函数原型: double poly(double x,int degree,double coeffs[])
函数功能: 计算多项式
函数返回: 多项式的计算结果
参数说明: 计算c[n]*x^n+c[n-1]x^n-1+.....+c[1]*x+c[0]
所属文件: <math.h>
#include <stdio.h>
#include <math.h>
int main()
{
double array[]={-1.0,5.0,-2.0,1.0};
double result;
result=poly(2.0,3,array);
printf("The polynomial: x**3 - 2.0x**2 + 5x - 1 at 2.0 is %lf",result);
return 0;
}
9.数学错误计算处理
@函数名称: matherr
函数原型: int matherr(struct exception *e)
函数功能: 数学错误计算处理程序
函数返回:
参数说明: 该函数不能被直接调用,而是被库函数_matherr()调用
所属文件: <math.h>
#include<math.h>
int matherr(struct exception *a)
{
return 1;
}
C语言中math.h中常用的函数的更多相关文章
- python第二十课——math模块中常用的函数
属性: e:自然数 pi:圆周率 函数: ceil():向上取整 floor():向下取整 sqrt():开平方根 radians():角度转弧度 degrees():弧度转角度 import mat ...
- C语言string.h中常用字符函数介绍
原文:http://www.cnblogs.com/xuwenmin888/archive/2013/05/03/3057883.html strcpy 函数名: strcpy 功 能: 拷贝一个字符 ...
- go语言中strings包中的Trim函数的作用是什么
答:Trim函数原型如下: func Trim(s string, cutset string) string 去掉字符串s中首部以及尾部与字符串cutset中每个相匹配的字符,如: s=" ...
- Mysql中常用的函数汇总
Mysql中常用的函数汇总: 一.数学函数abs(x) 返回x的绝对值bin(x) 返回x的二进制(oct返回八进制,hex返回十六进制)ceiling(x) 返回大于x的最小整数值exp(x) 返回 ...
- SQL点滴30—SQL中常用的函数
原文:SQL点滴30-SQL中常用的函数 该文章转载自http://www.cnblogs.com/jiajiayuan/archive/2011/06/16/2082488.html 别人的总结,很 ...
- string中常用的函数
string中常用的函数 发现在string在处理这符串是很好用,就找了一篇文章放在这里了.. 用 string来代替char * 数组,使用sort排序算法来排序,用unique 函数来去重1.De ...
- jQuery中常用的函数方法
jQuery中常用的函数方法总结 Ajax处理 load(url,[data],[callback]) url (String) : 待装入 HTML 网页网址. data (Map) : (可选) ...
- math.h中的常量
类似于Matlab中经常用到的一些常量,C++里边也是有的.(经查源文件无意中看到) 写入如下代码: #include<iostream> #include<iomanip> ...
- 自己模拟实现math.h中的函数
之前一直都很迷惑pow()函数时怎么实现的,对于整数次的幂我还能很容易做到,但是对于分数次幂就不是那么好做了.需要一些高等数学级数的知识. 我这里实现了求ln(x), pow(double x, do ...
随机推荐
- ubuntu18.04安装配置opencv3.4.0
1.安装配置相关工具及依赖库 sudo apt-get install build-essential # 必须的,gcc编译环境 sudo apt-get install cmake git lib ...
- CF360E Levko and Game【贪心+dijsktra】
先把所有边可动设为r[i]又这些边不是l就是r(如果想一个方向改变能更优的话就尽量多的改变),每次跑dijsktra,对于可动边(x,y),如果dis1[x]<=dis2[x],那么就把这条边改 ...
- 洛谷P1220 关路灯(区间dp)
关路灯 某一村庄在一条路线上安装了n盏路灯,每盏灯的功率有大有小(即同一段时间内消耗的电量有多有少).老张就住在这条路中间某一路灯旁,他有一项工作就是每天早上天亮时一盏一盏地关掉这些路灯.为了给村里节 ...
- 洛谷P4407 [JSOI2009]电子字典
题目描述 人们在英文字典中查找某个单词的时候可能不知道该单词的完整拼法,而只知道该单词的一个错误的近似拼法,这时人们可能陷入困境,为了查找一个单词而浪费大量的时间.带有模糊查询功能的电子字典能够从一定 ...
- fatal pylint error : ......can't find '__main__'module in
fatal pylint error : ......can't find '__main__'module in原因是没有安装pylint,所以提示没有找到__main__模块 解决方案:1.到官网 ...
- 集合中的 for-Each循环
数组的加强型的for-Each循环很简单,我们再来看一下集合中的for-Each 循环又是怎么样的.我们都知道集合中的遍历都是通过迭代(iterator)完成的.也许有人说,也可以按照下面的方式来遍 ...
- CodeForces - 296A-Yaroslav and Permutations(思维)
Yaroslav has an array that consists of n integers. In one second Yaroslav can swap two neighboring a ...
- Codeforces Round 56-C. Mishka and the Last Exam(思维+贪心)
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...
- js弹框怎么获得父页面的元素
js获取父页面的元素可以用$(window.parent.document).find("#customer_id").val();这里的customer_id表示父页面某一个元素 ...
- 去掉word文档两边的空白
1.设置-页面布局-页边距,把左边距和右边距的数据设置到最小就好,一般为0.43CM 2.把WORD页面顶部标尺,左右拉到最底,如图: 3.在打印预览里,设置页边距,操作方法同 上述 1,如图: