cjm2671
cjm2671

Reputation: 19486

RuntimeWarning: overflow encountered in ulong_scalars

I have a thread that goes something like this (shortened for clarity):

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

The important bit is the g1-g0 in the print statement. Actual output follows below:

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

The problem:

The third column should be the difference between the first two columns. Sometimes it is, sometimes it's wildly different. I also get this warning: RuntimeWarning: overflow encountered in ulong_scalars

What am I doing wrong?

Upvotes: 1

Views: 6782

Answers (1)

cjm2671
cjm2671

Reputation: 19486

Problem is caused because result is 'unsigned integer' - i.e. cannot be negative. Casting to normal integer everything works fine.

Upvotes: 5

Related Questions