# Calculate Field, SyntaxError invalid syntax partially working

522
4
09-01-2021 06:48 AM
by
New Contributor II

I'm attempting to run a script to calculate a field that is conditional.  If float2 is available I want to calculate the ratio of float1/float2, if it is null I want to use the ratio of float1/float3, and then leave null if both float2 and float3 are null.

The script doesn't immediately fail when running, but does error out after a few thousand records with the following:

Expression:

`calc(!float1!, !text1!, !float2!, !float3!)`

Code Block:

`def calc(f1, t1, f2, f3):    if f1 > 0 and t1 == "H":        if f2:            return f1 / f2        elif f3:            return f1 / f3        else:            return None    else:        return None`

Any ideas on what is happening here or how to address it?  !float1! is indeed a float, so my first thought that maybe there was some hard coded scientific notation in there doesn't seem to be an issue.  Thanks!

4 Replies
by
New Contributor II

It seems to be an error associated with that particular record.  When I select on the record and do a simple calculation of float1 / float2 I get the same error.  Any idea what this could be or how to fix?

by
New Contributor II

OK, I'm convinced it has something to do with the scientific notation.  I have some records with float1 = 10,000,000 ; 20,000,000 ; 50,000,000 and they are the only ones that fail.  It seems as long as the value doesn't result in a number divisible by 10 million it works.  Other records have values 10 million plus.

What's going on here?

MVP Esteemed Contributor

You should use DOUBLE datatype for floating values of more than six-digits. Create a new DOUBLE field and assign the values of Field1 to it.

Could you test the following code block (Test with Field1 or the new field)?

``````def calc(f1, t1, f2, f3):
if f1 > 0 and t1 == "H":
if f2 > 0:
return (f1 / f2)
elif f3 > 0:
return (f1 / f3)
else:
return None
else:
return None``````

Think Location
by
New Contributor II