为什么在某些情况下覆盖时 math.infinity 会变成一个大的负整数?

Yuv*_*raj 11 python numpy

我想为最小查找算法覆盖一些数组值。对于这些示例,我希望将第一行的值替换为math.inf.

它在 example 中工作正常c,但我不明白aand 中发生了什么b

import numpy as np
import math

a = np.repeat(0, 9).reshape((3, 3))
a[0, :] = np.ones((3,)) * math.inf
print(a)

b = np.arange(9).reshape((3, 3))
b[0, :] = np.ones((3,)) * math.inf
print(b)

c = np.empty((3, 3))
c[0, :] = np.ones((3,)) * math.inf
print(c)
Run Code Online (Sandbox Code Playgroud)

输出:将无穷大显示为 example 中的行条目c,但-9223372036854775808在 examplea和 中b

在此处输入图片说明

为什么覆盖现有输出时会产生影响?

mka*_*alp 1

repeat和 的整数文字参数arange导致返回的数组具有整数类型。没有empty类似的文字参数,dtype而是有一个参数,默认为float类型。

请参阅以下0.0内容9.0

import numpy as np
import math

a = np.repeat(0.0, 9).reshape((3, 3))
a[0, :] = np.ones((3,)) * math.inf
print(a)

b = np.arange(9.0).reshape((3, 3))
b[0, :] = np.ones((3,)) * math.inf
print(b)
Run Code Online (Sandbox Code Playgroud)
[[inf inf inf]
 [ 0.  0.  0.]
 [ 0.  0.  0.]]
[[inf inf inf]
 [ 3.  4.  5.]
 [ 6.  7.  8.]]
Run Code Online (Sandbox Code Playgroud)

至于为什么-9223372036854775808

to转换使用cvttsd2sifloat指令,该指令返回 1 后跟 63 个零来表示错误:integer

如果转换结果超出有符号四字整数的范围限制(在 64 位模式下且 REX.W/VEX.W/EVEX.W = 1),则会引发浮点无效异常,并且如果此异常被屏蔽,返回不定整数值 (80000000_00000000H)。

如果将其解释为 2 的补码整数,则对应于-2^63 = -9223372036854775808