Standard C 之 math.h和float.h
对于C Standard Library 可以参考:http://www.acm.uiuc.edu/webmonkeys/book/c_guide/ 或者 http://www.cplusplus.com/reference/
(一) <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的更多相关文章
- <math.h>与<float.h>
(一) <math.h> <math.h>文件中已经定义了M_PI,如下所示,用户可以直接使用: //math.h........................ #if de ...
- <limits.h>和<float.h>
头文件<limits.h>中定义了用于表示整类型大小的常量.以下所列的值是可接受的最小值,实际系统中可能有更大的值. CHAR_BIT char类型的位数 CHAR_MAX UCHAR_M ...
- C 标准库 - <float.h>
C 标准库 - <float.h> 简介 C 标准库的 float.h 头文件包含了一组与浮点值相关的依赖于平台的常量.这些常量是由 ANSI C 提出的,这让程序更具有可移植性.在讲解这 ...
- C 标准库系列之float.h
float.h 内部主要包含了一系列的浮点数宏.指明可移植程序必要的常量:浮点数格式一般为Spxbe;其中S表示+-:p表示底数.b表示基数如2.8.10.16等进制,e为指数标识E或e: 在一般情况 ...
- <cfloat> (float.h)
头文件: <cfloat> (float.h) 浮点类型的特性 这个头文件为特殊系统和编译器的实现描述了浮点类型的特征. 一个浮点数包含四个元素: 一个标志(a sign):正或负; 一个 ...
- float.h
float.h 一背景知识 浮点算术非常复杂 很多小的处理器在硬件指令方面甚至不支持浮点算术 其他的则需要一个独立的协处理器来处理这种运算 只有最复杂的计算机才在硬件指令集中支持浮点运算 ...
- sys/types.h fcntl.h unistd.h sys/stat.h
sys/types.h 是Unix/Linux系统的基本系统数据类型的头文件,含有size_t,time_t,pid_t等类型. 在应用程序源文件中包含 <sys/types.h> 以访问 ...
- #include<stdio.h> #include "stdio.h"
https://baike.baidu.com/item/#include <stdio.h> #include <stdio.h> 编辑 #include<stdio. ...
- 解决Winsock2.h和afxsock.h定义冲突的办法
如果我们在工程中使用了afxsock.h,但在其它的地方又加了些 使用winsock2.h,哈哈,VC会告诉你一大堆错误,大意就是有定义重复,该怎么解决? 由于MFC的SOCKET类使用的是Winso ...
随机推荐
- shell 脚本启动tomcat服务
#!/bin/bash # kill tomcat进程 tomcat_fashion_dev_pid=`ps aux|grep tomcat_fashion_dev|grep -v "gre ...
- iOS 设备的CPU架构以及查看framework支持架构
记录一下,感觉摘录的还是比较全的: 模拟器:4s-5: i3865s-7 Plus: x86_64 真机(iOS设备):armv6: iPhone.iPhone 2.iPhone 3G.iPod To ...
- MATLAB 的循环语句
1.MATLAB while循环语法 在MATLAB 中 while循环的语法如下: while <expression> <statements> end while 循环反 ...
- 关于Linux防火墙'iptables'的面试问答
1. 你听说过Linux下面的iptables和Firewalld么?知不知道它们是什么,是用来干什么的? 答案 : iptables和Firewalld我都知道,并且我已经使用iptables好一段 ...
- (转)Groupon前传:从10个月的失败作品修改,1个月找到成功 并不挶泥在这个点子上面,它反而往后站一步,看看他们已经做好的这个网站,可以再怎么包装成另一个完完全全不同的网站?所有的人所做的每件失败的事情中, 一定有碰到或含有成功的答案」在里面,只是他们不知道而已。 人不怕失败」,只怕宣布失败」
(转)Groupon前传:从10个月的失败作品修改,1个月找到成功 今天读到 一个非常励志人心的故事 ,就像现在「叶问」有「前传」,最近很火红的团集购网站Groupon 也出现了「Groupon前传」 ...
- System.Web.HttpValueCollection.ThrowIfMaxHttpCollectionKeysExceeded
昨天客户跟我说,突然一个页面频繁地报ThrowIfMaxHttpCollectionKeysExceeded这个异常.而且是数据量大的时候报错,数据量小的时候OK. 根据异常的名称也能看得差不多超过了 ...
- 通过Stetho在Chrome上调试Android App
引 入依赖包 compile 'com.facebook.stetho:stetho:1.3.1' 初始化一下 public class MyApplication extends Applicati ...
- golang学习笔记 ---面向并发的内存模型
Go语言是基于消息并发模型的集大成者,它将基于CSP模型的并发编程内置到了语言中,通过一个go关键字就可以轻易地启动一个Goroutine,与Erlang不同的是Go语言的Goroutine之间是共享 ...
- Oracle 12C -- ADRCI查看DDL日志
$ adrci ADRCI: Release - Production on Tue Nov :: Copyright (c) , , Oracle and/or its affiliates. Al ...
- Objective-C 如何让非等宽的数字和空格对齐
在printf中,我们可以通过格式字符串来对文字进行对齐输出,比如: printf("%5d\n%5d", 12, 345); 在使用等宽字体的Console中,我们可以看到数字右 ...