inf -inf nam

http://www.gnu.org/software/libc/manual/html_node/Infinity-and-NaN.html

20.5.2 Infinity and NaN

IEEE 754 floating point numbers can represent positive or negative infinity, and NaN (not a number). These three values arise from calculations whose result is undefined or cannot be represented accurately. You can also deliberately set a floating-point variable to any of them, which is sometimes useful. Some examples of calculations that produce infinity or NaN:

1/0 = ∞
log (0) = -∞
sqrt (-1) = NaN

When a calculation produces any of these values, an exception also occurs; see FP Exceptions.

The basic operations and math functions all accept infinity and NaN and produce sensible output. Infinities propagate through calculations as one would expect: for example, 2 + ∞ = ∞, 4/∞ = 0, atan (∞) = π/2. NaN, on the other hand, infects any calculation that involves it. Unless the calculation would produce the same result no matter what real value replaced NaN, the result is NaN.

In comparison operations, positive infinity is larger than all values except itself and NaN, and negative infinity is smaller than all values except itself and NaN. NaN is unordered: it is not equal to, greater than, or less than anything, including itself. x == x is false if the value of x is NaN. You can use this to test whether a value is NaN or not, but the recommended way to test for NaN is with the isnan function (see Floating Point Classes). In addition, <, >, <=, and >= will raise an exception when applied to NaNs.

math.hdefines macros that allow you to explicitly set a variable to infinity or NaN.

Macro: float INFINITY

An expression representing positive infinity. It is equal to the value produced by mathematical operations like 1.0 / 0.0. -INFINITY represents negative infinity.

You can test whether a floating-point value is infinite by comparing it to this macro. However, this is not recommended; you should use the isfinite macro instead. See Floating Point Classes.

This macro was introduced in the ISO C99 standard.

Macro: float NAN

An expression representing a value which is “not a number”. This macro is a GNU extension, available only on machines that support the “not a number” value—that is to say, on all machines that support IEEE floating point.

You can use ‘#ifdef NAN’ to test whether the machine supports NaN. (Of course, you must arrange for GNU extensions to be visible, such as by defining _GNU_SOURCE, and then you must includemath.h.)

IEEE 754 also allows for another unusual value: negative zero. This value is produced when you divide a positive number by negative infinity, or when a negative result is smaller than the limits of representation.

inf :infinity (linux)  等同于   #INF:infinity  (windows)

nan :not a number     等同于      #IND:indeterminate (windows)

注意:1、inf一般是因为得到的数值,超出浮点数的表示范围(溢出,即阶码部分超过其能表示的最大值);而nan一般是因为对浮点数进行了未定义的操作,如对-1开方。

2、nan==nan 结果是0或false,即不能和nan进行比较,和nan进行比较得到的结果总是false或0。所以可以用函数:   int isNumber(double d){return (d==d);}来判断d是否为nan,若d是nan则返回0,否则返回非零值。

3、1.0/0.0等于inf,-1.0/0.0等于-inf,0.0+inf=inf;

4、对负数开方sqrt(-1.0)、对负数求对数(log(-1.0))、0.0/0.0、0.0*inf、inf/inf、inf-inf这些操作都会得到nan。(0/0会产生操作异常;0.0/0.0不会产生操作异常,而是会得到nan)

5、得到inf时就查看是否有溢出或者除以0,得到nan时就查看是否有非法操作。

6、C语言的头文件<float.h>中,有定义的常量DBL_MAX,这个常量表示“能表示出来的最大的双精度浮点型数 值”。<float.h>中还有常量DBL_MIN,DBL_MIN表示可以用规格化表示的最小的正浮点数,但DBL_MIN并不是最小的正 浮点数,因为可以用可以用非规格化浮点数表示的更小。可以用函数:int isFiniteNumber(double d){return (d<=DBL_MAX&&d>=-DBL_MAX);}来判断d是否为一个finite数(既不是inf,又不是 nan(加入d为nan,则d参加比较就会得到false(0)值))。

7、1.0/inf等于0.0。

8、inf是可以与其他浮点数进行比较的,即可以参与<=、>+、==、!=等运算。

下面这几个宏(用宏实现的,使用时跟函数的形式基本相同)是判断一个表达式的结果是否为inf、nan或其他:

头文件:include<math.h>

宏的用法(类似于函数原型):int fpclassify(x);

int isfinite(x);

int isnormal(x);

int isnan(x);

int isinf(x);

具体用法:

1、int fpclassify(x)  用来查看浮点数x的情况,fpclassify可以用任何浮点数表达式作为参数,fpclassify的返回值有以下几种情况。

FP_NAN:x是一个“not a number”。

FP_INFINITE: x是正、负无穷。

FP_ZERO: x是0。

FP_SUBNORMAL: x太小,以至于不能用浮点数的规格化形式表示。

FP_NORMAL: x是一个正常的浮点数(不是以上结果中的任何一种)。

2、int isfinite(x)  当(fpclassify(x)!=FP_NAN&&fpclassify(x)!=FP_INFINITE)时,此宏得到一个非零值。

3、int isnormal(x)  当(fpclassify(x)==FP_NORMAL)时,此宏得到一个非零值。

4、int isnan(x)   当(fpclassify(x)==FP_NAN)时,此宏返回一个非零值。

5、int isinf(x)   当x是正无穷是返回1,当x是负无穷时返回-1。(有些较早的编译器版本中,无论是正无穷还是负无穷,都返回非零值,不区分正负无穷)。

时间: 2024-10-06 21:24:15

inf -inf nam的相关文章

[hdu 2102]bfs+注意INF

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2102 感觉这个题非常水,结果一直WA,最后发现居然是0x3f3f3f3f不够大导致的--把INF改成INF+INF就过了. #include<bits/stdc++.h> using namespace std; bool vis[2][15][15]; char s[2][15][15]; const int INF=0x3f3f3f3f; const int fx[]={0,0,1,-1};

C语言中的nan和inf使用

本文总结nan和inf在C语言当中的含义.产生和判定方法. C语言当中的nan 表示not a number,等同于 #IND:indeterminate (windows) 产生: 对浮点数进行了未定义的操作: 对负数开方,对负数求对数,0.0/0.0,0.0*inf.inf/inf.inf-inf这些操作都会得到nan.(0/0会产生操作异常:0.0/0.0不会产生操作异常,而是会得到nan): 在GNU中,使用宏:float NAN对浮点数赋值: 判定: 库函数方法:(推荐) <见后>

inf&amp;nan(摘录自百度)

摘录自百度(http://zhidao.baidu.com/link?url=Vv8vThjOgVIl1WV_WF_9yJcGVwt_2RkwdK3nyNuRZJa_tKOf0hSDUpratetydnXnb6FsBaUbJF3xtCsVEfULga) inf :infinity (linux) 等同于 #INF:infinity (windows) nan :not a number 等同于 #IND:indeterminate (windows) 注意:1.inf一般是因为得到的数值,超出浮

浮点数NaN和INF(#IND, #INF)

参考: http://www.cplusplus.com/reference/cmath/isnan/ http://technet.microsoft.com/zh-cn/tzthab44(v=vs.95) http://blog.163.com/chen_dawn/blog/static/112506320125494852135/ http://blog.csdn.net/zhang11wu4/article/details/7781099 http://baike.baidu.com/v

Dijkstra

//在这里采用两种方式实现Dijkstra算法,经过测试两种方式:第一种:通过Dist[]让每一个顶点中Dist[]中寻找最短路径的点:第二种方式:从已经收录的顶点的相邻顶点中选择出最短路//径的的顶点,此种方式适合用于稠密图,减少点的遍历. #include<iostream> #include<malloc.h> using namespace std; #define INF (100000) //相连顶点的信息 typedef struct AdjNode{ int Wei

Java数据结构——带权图

带权图的最小生成树——Prim算法和Kruskal算法 带权图的最短路径算法——Dijkstra算法 package graph; // path.java // demonstrates shortest path with weighted, directed graphs 带权图的最短路径算法 // to run this program: C>java PathApp ////////////////////////////////////////////////////////////

1076. Trash(KM算法 二分最佳完美匹配)

1076. Trash Time limit: 1.0 second Memory limit: 64 MB You were just hired as CEO of the local junkyard.One of your jobs is dealing with the incoming trash and sorting it for recycling.The trash comes every day in N containers and each of these conta

Python内建方法

参考: https://docs.python.org/3.4/library/functions.html https://docs.python.org/2/library/functions.html http://blog.csdn.net/jgood/article/details/4371991 以上链接分别为Python官网的3.4版本的内建方法说明.2.X(指2.6和2.7)版本的内建方法说明.以及JGood对2.X版本的内建方法说明的翻译. abs(x) 返回一个数的绝对值.参

HDU 2297 半平面交

Run Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 640    Accepted Submission(s): 181 Problem Description Since members of Wuhan University ACM Team are lack of exercise, they plan to particip