python 计算结果 nan_在Python中可能导致NaN的所有可能的计算是什么?-程序员宅基地

技术标签: python 计算结果 nan  

1586010002-jmsa.png

I've been searching around, and there appear to be scattered discussions about NaNs in different programming languages, including some specific cases, but nothing exhaustive or clear.

What are the most common operations that would cause a NaN, in Python, which originate while working with NumPy or SciPy?

解决方案

If you do any of the following without horsing around with the floating-point environment, you should get a NaN where you didn't have one before:

0/0 (either sign on top and bottom)

inf/inf (either sign on top and bottom)

inf - inf or (-inf) + inf or inf + (-inf) or (-inf) - (-inf)

0 * inf and inf * 0 (either sign on both factors)

sqrt(x) when x < 0

fmod(x, y) when y = 0 or x is infinite; here fmod is floating-point remainder.

The canonical reference for these aspects of machine arithmetic is the IEEE 754 specification. Section 7.1 describes the invalid operation exception, which is the one that is raised when you're about to get a NaN. "Exception" in IEEE 754 means something different than it does in a programming language context.

Lots of special function implementations document their behaviour at singularities of the function they're trying to implement. See the man page for atan2 and log, for instance.

You're asking specifically about NumPy and SciPy. I'm not sure whether this is simply to say "I'm asking about the machine arithmetic that happens under the hood in NumPy" or "I'm asking about eig() and stuff." I'm assuming the former, but the rest of this answer tries to make a vague connection to the higher-level functions in NumPy. The basic rule is: If the implementation of a function commits one of the above sins, you get a NaN.

For fft, for instance, you're liable to get NaNs if your input values are around 1e1010 or larger and a silent loss of precision if your input values are around 1e-1010 or smaller. Apart from truly ridiculously scaled inputs, though, you're quite safe with fft.

For things involving matrix math, NaNs can crop up (usually through the inf - inf route) if your numbers are huge or your matrix is extremely ill-conditioned. A complete discussion of how you can get screwed by numerical linear algebra is too long to belong in an answer. I'd suggest going over a numerical linear algebra book (Trefethen and Bau is popular) over the course of a few months instead.

One thing I've found useful when writing and debugging code that "shouldn't" generate NaNs is to tell the machine to trap if a NaN occurs. In GNU C, I do this:

#include

feenableexcept(FE_INVALID);

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_39601194/article/details/110348946

智能推荐

0-1背包问题的迭代回溯算法实现(C++)_0-1背包问题的回溯算法设计c语言-程序员宅基地

文章浏览阅读1.1k次。编译环境:Dev-C++迭代回溯方法求解0-1背包问题的具体算法实现_0-1背包问题的回溯算法设计c语言

Ubuntu12.10开机输入密码后黑屏无法进入桌面的问题记录_lightdm输入密码后进不去dwm-程序员宅基地

文章浏览阅读2.4k次。Ubuntu12.10安装系统的中文语音的时候一直卡住,安装不上,按我这个急性子不小心我把整个系统关闭了,再重启系统后,当进入账号界面输入正确密码后,电脑一直黑屏,无法进入桌面(可以看到鼠标)。上网查了一些方法试了不行:比如如下:图形界面程序损坏,可能是在更新内核或者安装软件的时候,把与xorg相关的文件给清除了。系统黑屏后 Ctrl+ALT+F4 进入 命令行模式sudo..._lightdm输入密码后进不去dwm

go语言web开发系列之十六:gin框架中通过gorm使用事务_gorm 事务-程序员宅基地

文章浏览阅读5.4k次,点赞7次,收藏24次。一,演示项目的相关信息1,地址:https://github.com/liuhongdi/digv162,功能:以下订单为例,演示了在gorm中启用事务3, 项目结构:如图:说明:刘宏缔的go森林是一个专注golang的博客, 地址:https://blog.csdn.net/weixin_43881017说明:作者:刘宏缔 邮箱: [email protected]二,数据库及sql1,数据表:2,建表sql:CREATE TABLE `..._gorm 事务

线性二次型调节控制-程序员宅基地

文章浏览阅读200次。线性二次型调节控制现在我们讨论一个用于连续状态MDP的一个寻找最优策略的一个方法。该方法中我们直接近似V∗,而不采用离散化。该方法称之为值函数近似,在很多实际RL问题都有很好的应用。使用一个模型或为了发展一个值函数近似算法,我们假设对于MDP,我们有一个模型(model)或仿真器(simulator)。通俗的说,一个仿真器是一个黑盒子,输入是任何一个连续值的状态st,动作at,..._线性二次型调节器优势

分位数回归-Quantile regression-程序员宅基地

文章浏览阅读5.3w次,点赞29次,收藏187次。文章目录一、分位数回归概念二、相关推导2.1 分位数概念2.2 离差绝对值LAD2.3 分位数回归2.4 效果以及理解三、模型检验四、求解方法一、分位数回归概念分位数回归是估计一组回归变量X与被解释变量Y的分位数之间线性关系的建模方法。以往的回归模型实际上是研究被解释变量的条件期望。而人们也关心解释变量与被解释变量分布的中位数,分位数呈何种关系。它最早由Koenker和Bassett(197..._分位数回归

北理 计算机学院 钟宏,袁汉宁_北京理工大学计算机学院-程序员宅基地

文章浏览阅读139次。代表性学术成果[1] Hanning Yuan, Meng Fang,Xinquan Zhu,Hierarchical Sampling for Multi-Instance Ensemble Learning, IEEE Transactions on Knowledge and Data Engineering, 2013,25(12):2900~2905(SCI/Ei)[2] Hanning..._北京理工大学袁汉宁

随便推点

html css 隐藏div显示,css如何隐藏div?-程序员宅基地

文章浏览阅读939次。在我们平时布局网站的时候,想要把div进行隐藏,但是很多人不知道css控制div显示隐藏?下面我们来讲解一下css如何让div隐藏。一:使用display:none属性来隐藏div我们可以使用display:none属性来隐藏所有的信息,包括文本和图片,语法为:这里你是看不到的当我们使用div中的css样式,我们就可以使用display:none属性对内容进行隐藏,并且通过浏览器也是看不到的,同时..._隐藏 css

二、animate.css动画插件_css3动画插件-程序员宅基地

文章浏览阅读1.6k次。css3动画库,预设了抖动(shake)、闪烁(flash)、弹跳(bounce)、旋转(rotateIn|rotateOut)、翻转(flip)、淡入淡出(fadeIn|fadeOut)等动画效果。1、过渡动画是需要触发条件的(hover事件或click事件等),只能完成简单的动画。2、加统一的类名animated,然后选择自己想要的动画效果,然后复制粘贴。第一个animated是必须添加的样式名,第二个是指定的动画样式名。2、帧动画不需要任何的触发条件,可以完成复杂轨迹的动画。_css3动画插件

分享5个插件,助你在Python的道路越战越勇_python插件大全-程序员宅基地

文章浏览阅读1.1w次,点赞80次,收藏300次。你所不知道的编程插件,看完直呼内行_python插件大全

长连接简单使用;_一看特别长的链接怎么用-程序员宅基地

文章浏览阅读730次。很多IM应用中会用到这个东西,然而真正自己搭建IM应用服务端的很少,一般都是找的IM云,环信,融云,网易云信……不可否认,基础工能实现几家平台做的都很好,有幸用过网易云信,当时只是看别人文档,复制demo代码。搭建界面而已,没有想过内部实现原理。突然研究这个东西的时候,就会发现 长连接不一定只是用在IM上面的,所以了解一下简单实现原理,还是对更多功能的接入,有很大帮助的。Asy_一看特别长的链接怎么用

Android :clang.exe: error: linker command failed with exit code 1 (use -v to see invocation)_android clang: error: linker command failed with e-程序员宅基地

文章浏览阅读3.2k次。 今天在使用Android Studio3.0.1 ,升级了build:gradle 后,编译项目报错:clang.exe: error: linker command failed with exit code 1 (use -v to see invocation) ninja:build stopped:subcommand failed. FAILURE: Build f..._android clang: error: linker command failed with exit code 1 (use -v to see

ubuntu 8.04 bochs 调试linux 0.11内核_gcc4 hdc-0.11 hdc-0.11.img-程序员宅基地

文章浏览阅读1.8k次。要下载的软件包:bochs-2.3.5linux-0.11-081030.tar.gz linux-0.11-devel-060625.zip(hdc-0.11-new.img 文件系统)(hdc-0.11-new.img 只用这一个文件也可以时入linux 0.11系统,只需在配置bochs时稍做修改就可以,但是要调试源码的话,还是自己编译来得爽)安装bochs之前做一些_gcc4 hdc-0.11 hdc-0.11.img

推荐文章

热门文章

相关标签