2

I have a vector layer in which I have added some fields of type 'float' using arcpy.AddField_management. I'm performing simple calculations between those fields using update cursors but the problem is that when I divide the values by 0, I get a run-time error even though I'm using Inf with the float function.

This is my code:

with arcpy.da.UpdateCursor('flurstcuke_cropped5', ['FID','totresgeb', 'totresgbpt','difresarea','scaledif']) as cursor6:

for row6 in cursor6:

   row6[3] = row6[2] - row6[1]

   try: 

       row6[4] = ((row6[2] - row6[1])/row6[1]) * 100

   except ZeroDivisionError:

      row6[4] = float('INF')


   cursor6.updateRow([row6[0], row6[1], row6[2], row6[3], row6[4]])

The message I get is that 'the value type is incompatible with the field type. [scaledif].' And I know it's referring to Inf since I have also set up a counter in my script to determine where the program is. But I don't understand that since 'Inf' is a floating number here and the field's type is also float

1 Answer 1

3

float("Inf") is not a true float. You can't do normal math with it. Feature class fields can only store actual numeric values, within specific ranges. From the help:

Data type     |  Storable range
---------------------------------------------------
Short integer |  -32,768 to 32,767
Long integer  |  -2,147,483,648 to 2,147,483,647
Float         |  Approximately -3.4E38 to 1.2E38
Double        |  Approximately -2.2E308 to 1.8E308

If you want the largest possible value, you should be able to import sys and use row6[4] = sys.float_info.max (although this is the size of the largest double in ArcGIS, not the largest float; the distinction between floats and doubles is blurred in Python).

1
  • OK. Thanks. I just added a garbage value like 99999 into those rows to solve that problem
    – Usman
    Dec 17, 2014 at 17:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.