运行时警告:ulong_scalars溢出

1 投票
1 回答
6680 浏览
提问于 2025-04-18 03:23

我有一个线程,内容大致是这样的(为了清晰简洁,做了简化):

from __future__ import absolute_import, division, generators, unicode_literals, print_function, nested_scopes, with_statement
import numpy as np

g = []
def run(self, faces):
  total = some_stuff_thats_correct()
  g.append(total)
    i = 1
    if len(g) > 1:
      i += 1
      g1 = g[len(g)-1]
      g0 = g[len(g)-2]
      print(g1, g0, g1 - g0, len(g), type(g1), type(g0))

这里面最重要的是打印语句中的 g1-g0。下面是实际输出:

376313 378765 18446744073709549164 2 <type 'numpy.uint64'> <type 'numpy.uint64'>
374107 376313 18446744073709549410 3 <type 'numpy.uint64'> <type 'numpy.uint64'>
374661 374107 554 4 <type 'numpy.uint64'> <type 'numpy.uint64'>
374405 374661 18446744073709551360 5 <type 'numpy.uint64'> <type 'numpy.uint64'>
375109 374405 704 6 <type 'numpy.uint64'> <type 'numpy.uint64'>
376084 375109 975 7 <type 'numpy.uint64'> <type 'numpy.uint64'>
378705 376084 2621 8 <type 'numpy.uint64'> <type 'numpy.uint64'>
380195 378705 1490 9 <type 'numpy.uint64'> <type 'numpy.uint64'>
382308 380195 2113 10 <type 'numpy.uint64'> <type 'numpy.uint64'>
383803 382308 1495 11 <type 'numpy.uint64'> <type 'numpy.uint64'>
383652 383803 18446744073709551465 12 <type 'numpy.uint64'> <type 'numpy.uint64'>
384519 383652 867 13 <type 'numpy.uint64'> <type 'numpy.uint64'>
382326 384519 18446744073709549423 14 <type 'numpy.uint64'> <type 'numpy.uint64'>
381366 382326 18446744073709550656 15 <type 'numpy.uint64'> <type 'numpy.uint64'>
378263 381366 18446744073709548513 16 <type 'numpy.uint64'> <type 'numpy.uint64'>
378094 378263 18446744073709551447 17 <type 'numpy.uint64'> <type 'numpy.uint64'>

问题

第三列应该是前两列的差值。有时候它确实是这样,但有时候却差得很远。我还收到了这个警告:RuntimeWarning: overflow encountered in ulong_scalars

我哪里做错了?

1 个回答

5

这个问题是因为结果是“无符号整数”,也就是说它不能是负数。把它转换成普通的整数后,一切就正常了。

撰写回答