2022-1-27 c++中出现-nan(ind)的原因和辨别办法

nan: not a number 非数字
1. 出现原因:
(1)分母为“0”,如果分母为零,自然时不能得到一个确定的数字的。
(2)对负数开平方、对负数求对数(log(-1.0))。注:0/0会产生操作异常;0.0/0.0不会产生操作异常,而是会得到nan。
(3)有些编译器在对无穷大与无穷小的计算时也会出现此类情况。

2. 辨别办法:
isnan(): ture is nan, false otherwise

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Information technology — Languageindependent arithmetic —Part 1:Integer and floating point arithmetic Technologies de l'information — Arithmétique indépendante de langage —Partie 1: Arithmétique de nombres entiers et en virgule flottante Contents Foreword vii Introduction viii 1 Scope 1 1.1 Inclusions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1.2 Exclusions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 2 Conformity 3 3 Normative references 4 4 Symbols and denitions 4 4.1 Symbols . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 4.1.1 Operators and relations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 4.1.2 Sets and intervals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 4.1.3 Exceptional values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 4.1.4 Special values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 4.1.5 The Boolean datatype . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 4.1.6 Operation specication framework . . . . . . . . . . . . . . . . . . . . . . . 6 4.2 Denitions of terms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 5 Specications for integer and oating point datatypes and operations 12 5.1 Integer datatypes and operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 5.1.1 Integer result function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 5.1.2 Integer operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 5.1.2.1 Comparisons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 5.1.2.2 Basic arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 5.2 Floating point datatypes and operations . . . . . . . . . . . . . . . . . . . . . . . . 17 5.2.1 Conformity to IEC 60559 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 5.2.2 Range and granularity constants . . . . . . . . . . . . . . . . . . . . . . . . 19 5.2.3 Approximate operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 5.2.4 Rounding and rounding constants . . . . . . . . . . . . . . . . . . . . . . . 20 5.2.5 Floating point result function . . . . . . . . . . . . . . . . . . . . . . . . . . 21 5.2.6 Floating point operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 5.2.6.1 Comparisons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 5.2.6.2 Basic arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 5.2.6.3 Value dissection . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 5.2.6.4 Value splitting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 5.3 Operations for conversion between numeric datatypes . . . . . . . . . . . . . . . . 29 5.3.1 Integer to integer conversions . . . . . . . . . . . . . . . . . . . . . . . . . . 30 5.3.2 Floating point to integer conversions . . . . . . . . . . . . . . . . . . . . . . 31 5.3.3 Integer to oating point conversions . . . . . . . . . . . . . . . . . . . . . . 31 5.3.4 Floating point to oating point conversions . . . . . . . . . . . . . . . . . . 32 5.3.5 Floating point to xed point conversions . . . . . . . . . . . . . . . . . . . . 32 5.3.6 Fixed point to oating point conversions . . . . . . . . . . . . . . . . . . . . 34 5.4 Numerals as operations in a programming language . . . . . . . . . . . . . . . . . . 34 iii ? ISO/IEC 2012 – All rights reserved ISO/IEC 10967-1:2012(E) 5.4.1 Numerals for integer datatypes . . . . . . . . . . . . . . . . . . . . . . . . . 34 5.4.2 Numerals for oating point datatypes . . . . . . . . . . . . . . . . . . . . . 35 6 Notication 35 6.1 Model for handling of notications . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 6.2 Notication alternatives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 6.2.1 Notication by recording in indicators . . . . . . . . . . . . . . . . . . . . . 36 6.2.2 Notication by alteration of control ow . . . . . . . . . . . . . . . . . . . . 38 6.2.3 Notication by termination with message . . . . . . . . . . . . . . . . . . . 38 6.3 Delays in notication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 6.4 User selection of alternative for notication . . . . . . . . . . . . . . . . . . . . . . 39 7 Relationship with language standards 39 8 Documentation requirements 41 Annex A (informative) Partial conformity 43 A.1 Integer over ow notication relaxation . . . . . . . . . . . . . . . . . . . . . . . . . 44 A.2 Innitary notication relaxation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 A.3 Inexact notication relaxation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 A.4 Under ow notication relaxation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 A.5 Subnormal values relaxation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 A.6 Accuracy relaxation for add, subtract, multiply, and divide . . . . . . . . . . . . . 45 A.7 Accuracy relaxation for oating point conversion . . . . . . . . . . . . . . . . . . . 47 Annex B (informative) IEC 60559 bindings 51 B.1 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 B.2 Notication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 Annex C (informative) Rationale 57 C.1 Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 C.1.1 Inclusions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 C.1.2 Exclusions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 C.1.3 Companion parts to this part . . . . . . . . . . . . . . . . . . . . . . . . . . 58 C.2 Conformity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 C.2.1 Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 C.3 Normative references . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 C.4 Symbols and denitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 C.4.1 Symbols . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 C.4.2 Denitions of terms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 C.5 Specications for integer and oating point datatypes and operations . . . . . . . . 61 C.5.1 Integer datatypes and operations . . . . . . . . . . . . . . . . . . . . . . . . 62 C.5.1.0.1 Unbounded integers . . . . . . . . . . . . . . . . . . . . . 62 C.5.1.0.2 Bounded non-modulo integers . . . . . . . . . . . . . . . 63 C.5.1.0.3 Modulo integers . . . . . . . . . . . . . . . . . . . . . . . 64 C.5.1.1 Integer result function . . . . . . . . . . . . . . . . . . . . . . . . . 64 C.5.1.2 Integer operations . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 C.5.1.2.1 Comparisons . . . . . . . . . . . . . . . . . . . . . . . . . 64 C.5.1.2.2 Basic arithmetic . . . . . . . . . . . . . . . . . . . . . . . 65 iv ISO/IEC 10967-1:2012(E) ? ISO/IEC 2012 – All rights reserved C.5.2 Floating point datatypes and operations . . . . . . . . . . . . . . . . . . . . 65 C.5.2.0.1 Constraints on the oating point parameters . . . . . . . 66 C.5.2.0.2 Radix complement oating point . . . . . . . . . . . . . . 68 C.5.2.1 Conformity to IEC 60559 . . . . . . . . . . . . . . . . . . . . . . . 68 C.5.2.1.1 Subnormal numbers . . . . . . . . . . . . . . . . . . . . . 69 C.5.2.1.2 Signed zero . . . . . . . . . . . . . . . . . . . . . . . . . . 69 C.5.2.1.3 Innities and NaNs . . . . . . . . . . . . . . . . . . . . . 69 C.5.2.2 Range and granularity constants . . . . . . . . . . . . . . . . . . . 70 C.5.2.2.1 Relations among oating point datatypes . . . . . . . . . 70 C.5.2.3 Approximate operations . . . . . . . . . . . . . . . . . . . . . . . . 71 C.5.2.4 Rounding and rounding constants . . . . . . . . . . . . . . . . . . 71 C.5.2.5 Floating point result function . . . . . . . . . . . . . . . . . . . . . 73 C.5.2.6 Floating point operations . . . . . . . . . . . . . . . . . . . . . . . 73 C.5.2.6.1 Comparisons . . . . . . . . . . . . . . . . . . . . . . . . . 73 C.5.2.6.2 Basic arithmetic . . . . . . . . . . . . . . . . . . . . . . . 73 C.5.2.6.3 Value dissection . . . . . . . . . . . . . . . . . . . . . . . 74 C.5.2.6.4 Value splitting . . . . . . . . . . . . . . . . . . . . . . . . 74 C.5.2.7 Levels of predictability . . . . . . . . . . . . . . . . . . . . . . . . 75 C.5.2.8 Identities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 C.5.2.9 Precision, accuracy, and error . . . . . . . . . . . . . . . . . . . . . 78 C.5.2.9.1 LIA-1 and error . . . . . . . . . . . . . . . . . . . . . . . 79 C.5.2.9.2 Empirical and modelling errors . . . . . . . . . . . . . . . 80 C.5.2.9.3 Propagation of errors . . . . . . . . . . . . . . . . . . . . 80 C.5.2.10 Extra precision . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 C.5.3 Operations for conversion between numeric datatypes . . . . . . . . . . . . 82 C.5.4 Numerals as operations in a programming language . . . . . . . . . . . . . 83 C.5.4.1 Numerals for integer datatypes . . . . . . . . . . . . . . . . . . . . 83 C.5.4.2 Numerals for oating point datatypes . . . . . . . . . . . . . . . . 83 C.6 Notication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 C.6.1 Model handling of notications . . . . . . . . . . . . . . . . . . . . . . . . . 84 C.6.2 Notication alternatives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 C.6.2.1 Notication by recording in indicators . . . . . . . . . . . . . . . . 84 C.6.2.2 Notication by alteration of control ow . . . . . . . . . . . . . . . 85 C.6.2.3 Notication by termination with message . . . . . . . . . . . . . . 86 C.6.3 Delays in notication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 C.6.4 User selection of alternative for notication . . . . . . . . . . . . . . . . . . 86 C.7 Relationship with language standards . . . . . . . . . . . . . . . . . . . . . . . . . 87 C.8 Documentation requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 Annex D (informative) Example bindings for specic languages 89 D.1 Ada . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 D.2 C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 D.3 C++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104 D.4 Fortran . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 D.5 Common Lisp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 Annex E (informative) Example of a conformity statement 121 E.1 Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 v ISO/IEC 10967-1:2012(E) ISO/IEC 10967-1:2012(E) ? ISO/IEC 2012 – All rights reserved E.2 Integer parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 E.3 Floating point parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 E.4 Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 E.5 Notication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 Annex F (informative) Example programs 125 F.1 Verifying platform acceptability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 F.2 Selecting alternate code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 F.3 Terminating a loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126 F.4 Estimating error . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126 F.5 Saving exception state . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126 F.6 Fast versus accurate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 F.7 High-precision multiply . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 Bibliography 129

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值