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 ...
随机推荐
- codevs1553 互斥的数
1553 互斥的数
- python 定位
#字符串定位 使用str.find() 其结果为如下: #列表中元素的定位 使用list.index() 其结果如下:
- 《Python数据科学手册》第五章机器学习的笔记
目录 <Python数据科学手册>第五章机器学习的笔记 0. 写在前面 1. 判定系数 2. 朴素贝叶斯 3. 自举重采样方法 4. 白化 5. 机器学习章节总结 <Python数据 ...
- 一个小时学会 MySQL 数据库
随着移动互联网的结束与人工智能的到来大数据变成越来越重要,下一个成功者应该是拥有海量数据的,数据与数据库你应该知道. 一.数据库概要 数据库(Database)是存储与管理数据的软件系统,就像一个存入 ...
- MySQL server has gone away和Maximum execution time of 120 seconds exceeded
今天在写采集时碰到两个问题1.MySQL server has gone away2.Maximum execution time of 120 seconds exceeded 采集程序写好运行大概 ...
- mysql5.7日志时间戳(log_timestmaps)与系统时间不一致问题以及日志报Got an error reading communication packets情况分析
一.mysql安装后error_log日志时间戳默认为UTC(如下图),因此会造成与系统时间不一致,与北京时间相差8个小时. 解决errro_logs时间戳与linux系统时间不一致问题 step1: ...
- [LOJ 2190] 「SHOI2014」信号增幅仪
[LOJ 2190] 「SHOI2014」信号增幅仪 链接 链接 题解 坐标系直到 \(x\) 轴与椭圆长轴平行 点的坐标变换用旋转公式就可以了 因为是椭圆,所以所有点横坐标除以 \(p\) 然后最小 ...
- 题解 P1004 方格取数
传送门 动态规划Yes? 设i为路径长度,(为什么i这一维可以省掉见下)f[j][k]表示第一个点到了(j,i-j),第二个点到了(k,j-k) 则 int ji=i-j,ki=i-k; f[j][k ...
- JavaScript读取文本,并渲染在html
html <pre id="myText" style="word-wrap: break-word; white-space: pre-wrap; white-s ...
- Linux上安装Docker,并成功部署NET Core 2.0
概述 容器,顾名思义是用来存放并容纳东西的器皿: 而容器技术伴着Docker的兴起也渐渐的映入大家的眼帘,它是一个抽象的概念,同时也是默默存在世上多年的技术,不仅能使应用程序间完全的隔离,而且还能在共 ...