5451|6

78

帖子

0

TA的资源

一粒金砂(初级)

楼主
 

请教vxworks中计算结果为NaN的问题! [复制链接]

 在vxworks环境下,我的程序数值计算结果偶尔为NaN,导致整个后续的处理全部错误。
请问NaN是个什么数值?这可能是什么原因引起的?怎样才能解决这个问题?
如果不好解决的话,有没有办法识别出NaN?好像是个极值,但又不知道如何判断,请指教!
另:我用的处理器是赛扬533,操作系统是VxWorks5.4

最新回复

多进程的时候每个有浮点数运算的进程taskSpawn的时候第三个参数要添加VX_FP_TASK,否则有可能会出现NaN。 例:         taskSpawn( "tSITcpCfg", 99, VX_FP_TASK, 20480, (FUNCPTR)Cfg_Main, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ) ;   详情 回复 发表于 2008-5-27 23:00
点赞 关注
 

回复
举报

75

帖子

0

TA的资源

一粒金砂(初级)

沙发
 
NaN : Not a Number
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.h defines 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 include math.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. Negative zero behaves identically to zero in all calculations, unless you explicitly test the sign bit with signbit or copysign.
 
 
 

回复

55

帖子

0

TA的资源

一粒金砂(初级)

板凳
 
高手
 
 
 

回复

73

帖子

0

TA的资源

一粒金砂(初级)

4
 
thank you very much
 
 
 

回复

69

帖子

0

TA的资源

一粒金砂(初级)

5
 
是不是浮点运算的问题,现在我也面临同样的问题, 是怎么解决的?
 
 
 

回复

98

帖子

0

TA的资源

一粒金砂(初级)

6
 
很可能是浮点数运算问题,根据上面说的应该是对-1求平方根产生的,但我这个里面没有这个运算还是出现了NaN,一直也没查出来根本原因,现在只是采取了回避措施,当判断得到的数值为NaN时就进行特殊处理。
 
 
 

回复

72

帖子

0

TA的资源

一粒金砂(初级)

7
 
多进程的时候每个有浮点数运算的进程taskSpawn的时候第三个参数要添加VX_FP_TASK,否则有可能会出现NaN。
例:         taskSpawn( "tSITcpCfg", 99, VX_FP_TASK, 20480, (FUNCPTR)Cfg_Main, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ) ;
 
 
 

回复
您需要登录后才可以回帖 登录 | 注册

随便看看
查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
关闭
站长推荐上一条 1/6 下一条

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

站点相关: 国产芯 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网

北京市海淀区中关村大街18号B座15层1530室 电话:(010)82350740 邮编:100190

电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2025 EEWORLD.com.cn, Inc. All rights reserved
快速回复 返回顶部 返回列表