对于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. IntelliJ IDEA遇到Unable to parse template “Class”错误

    在新安装的Ubuntu16下运行IntelliJ IDEA时, 遇到一个错误,在新建class的时候,提示Unable to parse template “Class” 通过查看 Settings ...

  2. 转:ogre的编译及安装

    ogre在Windows环境下的编译及安装过程: 1.从下面网址下载OGRE 1.8.1 Source For Windows.Dependencies source repository with ...

  3. 转:Ogre内部渲染流程

    以下是 Ogre 的代码中的详细说明: Renderable是OGRE中所有可渲染对象的抽象接口 这个接口抽象出了在渲染管线中的被分组的离散的可渲染对象基本的方法. 此接口的实现类必须是基于单一的材质 ...

  4. 实战VMware的三种网络模式

    来源于:http://www.aneasystone.com/archives/2015/04/three-network-modes-of-vmware-in-action.html 一.实验目的 ...

  5. Map遍历的几种方法

    查看Map自带API map遍历方法: public static void main(String[] args) { Map<Integer,String> map = new Has ...

  6. 阿里员工都是这样排查Java问题的,附工具单(转)

    平时的工作中经常碰到很多疑难问题的处理,在解决问题的同时,有一些工具起到了相当大的作用,在此书写下来,一是作为笔记,可以让自己后续忘记了可快速翻阅,二是分享,希望看到此文的同学们可以拿出自己日常觉得帮 ...

  7. shell脚本逐个杀死k8s中某个应用的pod

    #!/bin/bash pod01=`kubectl get pod -o wide -n weifeng-system|grep official-ui-node-prod|awk -F : 'NR ...

  8. [转载]DB2与ORACLE、MYSQL比较2

    原文地址:DB2与ORACLE.MYSQL比较2作者:欣颖 4.2 Oracle9i  Oracle的产品战略是每12到18个月发布一个主要版本.主要发行版本所遵循的命名战略在PC领域中更为常见,它不 ...

  9. Linux系统vi编辑器提示E325: ATTENTION的解决方法

    非正常关闭文件会报这样的警告信息, 原因是系统产生了一个.swp的文件. 删除就行了.(前提是确认.swp文件没用了.) 例如: rm -f /etc/.shadowsocks.json.swp 详情 ...

  10. 【LeetCode】174. Dungeon Game

    Dungeon Game The demons had captured the princess (P) and imprisoned her in the bottom-right corner ...