运行时警告:ulong_scalars中遇到溢出

时间:2014-04-19 11:06:00

标签: python python-2.7

我有一个类似这样的帖子(为了清晰起见缩短了):

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))

重要的一点是print语句中的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 个答案:

答案 0 :(得分:2)

问题是由于结果是'无符号整数'引起的 - 即不能为负数。转换为正常整数一切正常。