- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
在 Python 3.6.2、Win10-x64 上
只是我遇到的一些奇怪的事情,无法解释。
在:
x = 10.0
for i in range(10):
print(str(i) + " | " + str(x))
x *= x
输出:
0 | 10.0
1 | 100.0
2 | 10000.0
3 | 100000000.0
4 | 1e+16
5 | 1e+32
6 | 1.0000000000000002e+64
7 | 1.0000000000000003e+128
8 | 1.0000000000000005e+256
9 | inf
怎么就变成了inf
?为什么不抛出异常呢?
例如,如果我用 **=
替换最后一行中的 *=
,那么在第二次迭代时它会引发 OverflowError: (34, '结果太大')
,这是有道理的(因为它只是 10.000.000.000^10.000.000.000)。
这是否意味着对 float 有一种“软”限制——超过时——将它们变成 inf
?如果是这样,那个限制是什么,无论算术运算如何,它都是一样的吗?这难道不意味着类似 inf == this_limit + anything
的东西吗?
.
添加:
我知道有 sys.float_info.max
。这是极限吗?
我刚刚有了一个想法并测试了一些东西:
print(sys.float_info.max)
print(sys.float_info.max + 1)
print(sys.float_info.max * 2)
print(sys.float_info.max * 1.000000000000001)
print(sys.float_info.max * 1.0000000000000001)
这给了我:
1.7976931348623157e+308
1.7976931348623157e+308
inf
inf
1.7976931348623157e+308
这对我来说很奇怪......
最佳答案
How come it just turns into inf?
因为结果对于最大的有限 float 来说太大了,所以会溢出。
Why doesn't it throw an exception?
因为 Python 对何时将 inf/nan 转换为异常不是很一致。一些操作给出异常(exception)。有些给 inf/nan。
Does this mean that there is a kind of "soft" limit on floats that -- when exceeded -- turns them into inf? And if so, what is that limit and is it the same regardless of the arithmetic operation? And wouldn't that imply something like inf == this_limit + anything?
任何在四舍五入后大于 1.7976931348623157e+308 的结果都会变成一个 inf(或者一个异常,如果 Python 喜欢的话)。确切的限制不能用 float 表示;您实际上不能执行 this_limit + anything
,因为尝试将 this_limit
放入 float 中会将其四舍五入为 1.7976931348623157e+308。
关于Python:一个大的 float 怎么会自动变成 *inf*?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46432189/
问题很简单:看代码。两个静态断言都通过了。我不希望第二个通过。这是错误还是正常行为? #include #include template class Temp, class Specializ
int Sequence::scoreDegeneracy() { cout const&) (in /usr/lib/libstdc++.so.6.0.13) ==17043== b
我已经测试了下面的代码,除了第 29 行之外,一切都按照我的预期进行。final.write(invrow) 实际上并没有写入文件。当我使用简单的 print invrow 时,它显示没有问题。我没有
我的项目中有很多类被单例访问,如下所示: _inline GUI_BS_Map* GUI_GetBS_Map() { static GUI_BS_Map obj; return &ob
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 6 年前。 Improve th
我针对我遇到的问题截取了几张屏幕截图。基本上,我对 vi 的习惯和期望是能够使用箭头键在文档中移动,并且仍然能够阅读文档的实际内容。 Here is a shot of vi editor as I
NSDateFormatter *timeFormatter = [[[NSDateFormatter alloc] init] autorelease]; [timeFormatter se
根据docs : The HTMLCanvasElement.toDataURL() method returns a data URI containing a representation of
我做了一个小函数,可以实际测量最大递归限制: def f(x): r = x try: r = f(x+1) except Exception as e:
我正在开发一个小型 silverlight 应用程序,该应用程序涉及在 javascript 和 silverlight 之间传递一些数据。我也在使用 silverlight 虚拟地球控件。 我遇到的
我在Chrome和Firefox中都试过了,浏览器好像没有问题。我的 CSS 是有效的,但是当我通过验证运行我的 HTML 时,它显示“元素链接上属性 rel 的错误值‘stylesheet’:字符串
如果我有一个类,其中的 ctor 设置为像这样的多重注入(inject): public Shogun(IEnumerable allWeapons) { this.allWeapons =
我现在正在使用 devise/omniauth。通过 facebook/twitter 注册后,我想重定向到一个名为“验证电子邮件”的页面,他们可以在其中验证他们的电子邮件地址是否正确。 我只是想让
我有两个相同的交易,在这种情况下发送相同数量的代币,导致实际消耗的气体不同(不是成本,而是显着差异)。 以下是 tx 哈希值: 0x2cbb4b35d87cabe1a7b7bcb562e4e046e9
如果我这样做: ExpiresActive On ExpiresDefault "access plus 30 days" 它没有正确设置 Expire header
我无法过滤我想要查看的特定软件包,因为 cpusettings 菜单呈灰色。 我正在使用 VisualVM 运行程序从 eclipse 运行我的应用程序。 最佳答案 采样过程中无法更改 CPU 设置。
在 http://www.gwtproject.org/doc/latest/DevGuideUiBinder.html 它指出要在 uiBinder 模板中使用小部件: they must be d
这个问题不太可能帮助任何 future 的访问者;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况有关,这些情况并不普遍适用于互联网的全局受众。为了帮助使这个问题更广泛地适用,visit
我有下面的代码,但是,当我用 visual studio code 修改代码并在我的 Chrome 浏览器中运行它时,我在“热门产品”中看不到下拉菜单。部分。 但是,当我在此处 (stackoverf
我有两个类(class)联系人和群组 FirstName 和LastName 的组合必须是唯一的,并且可以为单个联系人添加多个地址。我如何在 Entity Framework 代码优先方法中做到这一点
我是一名优秀的程序员,十分优秀!