对于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. shell 脚本启动tomcat服务

    #!/bin/bash # kill tomcat进程 tomcat_fashion_dev_pid=`ps aux|grep tomcat_fashion_dev|grep -v "gre ...

  2. iOS 设备的CPU架构以及查看framework支持架构

    记录一下,感觉摘录的还是比较全的: 模拟器:4s-5: i3865s-7 Plus: x86_64 真机(iOS设备):armv6: iPhone.iPhone 2.iPhone 3G.iPod To ...

  3. MATLAB 的循环语句

    1.MATLAB while循环语法 在MATLAB 中 while循环的语法如下: while <expression> <statements> end while 循环反 ...

  4. 关于Linux防火墙'iptables'的面试问答

    1. 你听说过Linux下面的iptables和Firewalld么?知不知道它们是什么,是用来干什么的? 答案 : iptables和Firewalld我都知道,并且我已经使用iptables好一段 ...

  5. (转)Groupon前传:从10个月的失败作品修改,1个月找到成功 并不挶泥在这个点子上面,它反而往后站一步,看看他们已经做好的这个网站,可以再怎么包装成另一个完完全全不同的网站?所有的人所做的每件失败的事情中, 一定有碰到或含有成功的答案」在里面,只是他们不知道而已。 人不怕失败」,只怕宣布失败」

    (转)Groupon前传:从10个月的失败作品修改,1个月找到成功 今天读到 一个非常励志人心的故事 ,就像现在「叶问」有「前传」,最近很火红的团集购网站Groupon 也出现了「Groupon前传」 ...

  6. System.Web.HttpValueCollection.ThrowIfMaxHttpCollectionKeysExceeded

    昨天客户跟我说,突然一个页面频繁地报ThrowIfMaxHttpCollectionKeysExceeded这个异常.而且是数据量大的时候报错,数据量小的时候OK. 根据异常的名称也能看得差不多超过了 ...

  7. 通过Stetho在Chrome上调试Android App

    引 入依赖包 compile 'com.facebook.stetho:stetho:1.3.1' 初始化一下 public class MyApplication extends Applicati ...

  8. golang学习笔记 ---面向并发的内存模型

    Go语言是基于消息并发模型的集大成者,它将基于CSP模型的并发编程内置到了语言中,通过一个go关键字就可以轻易地启动一个Goroutine,与Erlang不同的是Go语言的Goroutine之间是共享 ...

  9. Oracle 12C -- ADRCI查看DDL日志

    $ adrci ADRCI: Release - Production on Tue Nov :: Copyright (c) , , Oracle and/or its affiliates. Al ...

  10. Objective-C 如何让非等宽的数字和空格对齐

    在printf中,我们可以通过格式字符串来对文字进行对齐输出,比如: printf("%5d\n%5d", 12, 345); 在使用等宽字体的Console中,我们可以看到数字右 ...