对于C Standard Library 可以参考:http://www.acm.uiuc.edu/webmonkeys/book/c_guide/ 或者 http://www.cplusplus.com/reference/

(一) <math.h>

常用函数:

1、 三角函数
double sin(double);正弦
double cos(double);余弦
double tan(double);正切
2 、反三角函数
double asin (double); 结果介于[-PI/2,PI/2]
double acos (double); 结果介于[0,PI]
double atan (double); 反正切(主值),结果介于[-PI/2,PI/2]
double atan2 (double,double); 反正切(整圆值),结果介于[-PI,PI]
3 、双曲三角函数
double sinh (double);
double cosh (double);
double tanh (double);
4 、指数与对数
double frexp(double value,int *exp);这是一个将value值拆分成小数部分f和(以2为底的)指数部分exp,并返回小数部分f,即f*2^exp。其中f取值在0.5~1.0范围或者0。
double ldexp(double x,int exp);这个函数刚好跟上面那个frexp函数功能相反,它的返回值是x*2^exp
double modf(double value,double *iptr);拆分value值,返回它的小数部分,iptr指向整数部分。
double log (double); 以e为底的对数
double log10 (double);以10为底的对数
double pow(double x,double y);计算以x为底数的y次幂
float powf(float x,float y); 功能与pow一致,只是输入与输出皆为浮点数
double exp (double);求取自然数e的幂
double sqrt (double);开平方
5 、取整
double ceil (double); 取上整,返回不比x小的最小整数
double floor (double); 取下整,返回不比x大的最大整数,即高斯函数[x]
6 、绝对值
int abs(int i); 求整型的绝对值
double fabs (double);求实型的绝对值
double cabs(struct complex znum);求复数的绝对值
7 、标准化浮点数
double frexp (double f,int *p); 标准化浮点数,f = x * 2^p,已知f求x,p (x介于[0.5,1])
double ldexp (double x,int p); 与frexp相反,已知x,p求f
8 、取整与取余
double modf (double,double*); 将参数的整数部分通过指针回传,返回小数部分
double fmod (double,double); 返回两参数相除的余数
9 、其他
double hypot(double x,double y);已知直角三角形两个直角边长度,求斜边长度
double ldexp(double x,int exponent);计算x*(2的exponent次幂)
double poly(double x,int degree,double coeffs []);计算多项式
int matherr(struct exception *e);数学错误计算处理程序
 
注意:包括像abs, labs, div ldiv,这些看起来像是属于math.h,实际也是在stdlib.h中声明的,math.h中主要声明的是一些和浮点实数相关的数学函数及一些宏,和整数类型相关的都在stdlib.h中声明。这是C标准定义的,和系统无关。
 
对于math.h中的宏,有例如下:

<math.h>文件中已经定义了M_PI,如下所示,用户可以直接使用;

//math.h
........................ #if defined(_USE_MATH_DEFINES) && !defined(_MATH_DEFINES_DEFINED)
#define _MATH_DEFINES_DEFINED /* Define _USE_MATH_DEFINES before including math.h to expose these macro
* definitions for common math constants. These are placed under an #ifdef
* since these commonly-defined names are not part of the C/C++ standards.
*/ /* Definitions of useful mathematical constants
* M_E - e
* M_LOG2E - log2(e)
* M_LOG10E - log10(e)
* M_LN2 - ln(2)
* M_LN10 - ln(10)
* M_PI - pi
* M_PI_2 - pi/2
* M_PI_4 - pi/4
* M_1_PI - 1/pi
* M_2_PI - 2/pi
* M_2_SQRTPI - 2/sqrt(pi)
* M_SQRT2 - sqrt(2)
* M_SQRT1_2 - 1/sqrt(2)
*/ #define M_E 2.71828182845904523536
#define M_LOG2E 1.44269504088896340736
#define M_LOG10E 0.434294481903251827651
#define M_LN2 0.693147180559945309417
#define M_LN10 2.30258509299404568402
#define M_PI 3.14159265358979323846
#define M_PI_2 1.57079632679489661923
#define M_PI_4 0.785398163397448309616
#define M_1_PI 0.318309886183790671538
#define M_2_PI 0.636619772367581343076
#define M_2_SQRTPI 1.12837916709551257390
#define M_SQRT2 1.41421356237309504880
#define M_SQRT1_2 0.707106781186547524401 #endif /* _USE_MATH_DEFINES */

但必须在使用的文件中,

#include<math.h>之前,加入#define _USE_MATH_DEFINES,如下所示:

1 //------------------------------------------------------------------------------
2 //>>>
4 //使用math.h中定义M_PI的定义
5 #define _USE_MATH_DEFINES
6 #include <math.h>
7 const double Rad2Deg = (180.0/M_PI);
8 //<<<
9 //------------------------------------------------------------------------------

(二)<float.h>

由于浮点数存在的精度误差,造成浮点数与0比较的问题,一般定义其误差值来解决。float.h中已经定义了float,double两种浮点数的误差值,用户可以直接使用。

//float.h

.....................

#define DBL_DIG         15                      /* # of decimal digits of precision */
#define DBL_EPSILON 2.2204460492503131e-016 /* smallest such that 1.0+DBL_EPSILON != 1.0 */
#define DBL_MANT_DIG 53 /* # of bits in mantissa */
#define DBL_MAX 1.7976931348623158e+308 /* max value */
#define DBL_MAX_10_EXP 308 /* max decimal exponent */
#define DBL_MAX_EXP 1024 /* max binary exponent */
#define DBL_MIN 2.2250738585072014e-308 /* min positive value */
#define DBL_MIN_10_EXP (-307) /* min decimal exponent */
#define DBL_MIN_EXP (-1021) /* min binary exponent */
#define _DBL_RADIX 2 /* exponent radix */
#define _DBL_ROUNDS 1 /* addition rounding: near */ #define FLT_DIG 6 /* # of decimal digits of precision */
#define FLT_EPSILON 1.192092896e-07F /* smallest such that 1.0+FLT_EPSILON != 1.0 */
#define FLT_GUARD 0
#define FLT_MANT_DIG 24 /* # of bits in mantissa */
#define FLT_MAX 3.402823466e+38F /* max value */
#define FLT_MAX_10_EXP 38 /* max decimal exponent */
#define FLT_MAX_EXP 128 /* max binary exponent */
#define FLT_MIN 1.175494351e-38F /* min positive value */
#define FLT_MIN_10_EXP (-37) /* min decimal exponent */
#define FLT_MIN_EXP (-125) /* min binary exponent */
#define FLT_NORMALIZE 0
#define FLT_RADIX 2 /* exponent radix */
#define FLT_ROUNDS 1 /* addition rounding: near */

应用举例:

//------------------------------------------------------------------------------
//>>>
#include <float.h>
#define FLOAT_EQ(a,b) (fabs(a-b)<=FLT_EPSILON)
#define DOUBLE_EQ(a,b) (fabs(a-b)<=DBL_EPSILON)
//<<<
//------------------------------------------------------------------------------ float x,y; x=0.0; y=0.0000001; if(fabs(a)<FLT_EPSILON) //判断单精度类型变量x是否为零
...... if(fabls(a-b)<FLT_EPSILON) //判断单精度变量x,y是否相等
......

注意:对以上数学计算的宏定义,不要重复定义。

Standard C 之 math.h和float.h的更多相关文章

  1. <math.h>与<float.h>

    (一) <math.h> <math.h>文件中已经定义了M_PI,如下所示,用户可以直接使用: //math.h........................ #if de ...

  2. <limits.h>和<float.h>

    头文件<limits.h>中定义了用于表示整类型大小的常量.以下所列的值是可接受的最小值,实际系统中可能有更大的值. CHAR_BIT char类型的位数 CHAR_MAX UCHAR_M ...

  3. C 标准库 - <float.h>

    C 标准库 - <float.h> 简介 C 标准库的 float.h 头文件包含了一组与浮点值相关的依赖于平台的常量.这些常量是由 ANSI C 提出的,这让程序更具有可移植性.在讲解这 ...

  4. C 标准库系列之float.h

    float.h 内部主要包含了一系列的浮点数宏.指明可移植程序必要的常量:浮点数格式一般为Spxbe;其中S表示+-:p表示底数.b表示基数如2.8.10.16等进制,e为指数标识E或e: 在一般情况 ...

  5. <cfloat> (float.h)

    头文件: <cfloat> (float.h) 浮点类型的特性 这个头文件为特殊系统和编译器的实现描述了浮点类型的特征. 一个浮点数包含四个元素: 一个标志(a sign):正或负; 一个 ...

  6. float.h

    float.h 一背景知识 浮点算术非常复杂   很多小的处理器在硬件指令方面甚至不支持浮点算术   其他的则需要一个独立的协处理器来处理这种运算   只有最复杂的计算机才在硬件指令集中支持浮点运算 ...

  7. sys/types.h fcntl.h unistd.h sys/stat.h

    sys/types.h 是Unix/Linux系统的基本系统数据类型的头文件,含有size_t,time_t,pid_t等类型. 在应用程序源文件中包含 <sys/types.h> 以访问 ...

  8. #include<stdio.h> #include "stdio.h"

    https://baike.baidu.com/item/#include <stdio.h> #include <stdio.h> 编辑 #include<stdio. ...

  9. 解决Winsock2.h和afxsock.h定义冲突的办法

    如果我们在工程中使用了afxsock.h,但在其它的地方又加了些 使用winsock2.h,哈哈,VC会告诉你一大堆错误,大意就是有定义重复,该怎么解决? 由于MFC的SOCKET类使用的是Winso ...

随机推荐

  1. Zookeeper的Watcher机制

    ZooKeeper 提供了分布式数据的发布/订阅功能, 在 ZooKeeper 中引入了 Watcher 机制来实现这种分布式的通知功能. ZooKeeper 允许客户端向服务端注册一个 Watche ...

  2. Android Intent之Action应用

    Log.i("txrjsms", "whereDoYouJumpFrom:"+getIntent().getPackage()); 结果是null Log.i( ...

  3. Vista/Win7以上系统查看和清除本地DNS缓存新方法

    你是否因修改网站DNS解析后,却因本机DNS缓存而需要等待... 你是否遇到修改了本机的hosts文件后,必须重起firefox和ie才起作用... 其实只要清空DNS缓存这些问题都可以解决. 查看D ...

  4. ios中一级导航

    效果图 第一步先下载文件 http://pan.baidu.com/share/link?shareid=2148445651&uk=9237761877 将目录引入到xcode中 在xcod ...

  5. Deep Introduction to Go Interfaces.

    Standard Interface Intro Go’s interfaces are one of it’s best features, but they’re also one of the ...

  6. OpenCV 学习笔记03 boundingRect、minAreaRect、minEnclosingCircle、boxPoints、int0、circle、rectangle函数的用法

    函数中的代码是部分代码,详细代码在最后 1 cv2.boundingRect 作用:矩形边框(boundingRect),用于计算图像一系列点的外部矩形边界. cv2.boundingRect(arr ...

  7. mysql homedir迁移

    随着数据库的增长,innodb文件和日志文件会越来越大,如果是默认安装的mysql,这些文件一般是放在 /usr/lib/mysql 下面进行转移: 1,安全关闭mysql mysqladmin -u ...

  8. sql server 2008评估期已到的解决办法

    点击开始-所有程序-Microsoft SQL Server 2008-配置工具-SQL Server 安装中心然后点击左侧的维护,在点击右侧的版本升级,接着按照提示一直点下一步,到产品密钥的时候输入 ...

  9. windbg分析执行在64位环境下的32位程序的dump

    windbg命令例如以下 1.   .load wow64exts 2.   !sw 3.   ~* kvnf

  10. STM8的数据@near @tiny定义

    总是记不住这个:stm8 stvd下 near等于51的xdata tiny等于51的idata http://www.waveshare.net/article/STM8-3-1-10.htm 如何 ...