1

I included an unreachable condition in this function. The problem is that it was just reached. I don't know how to troubleshoot it.

def bcirrus_func1(Qn):
    if Qn <= -1:
        bcirrus = 0
    elif Qn > -1 and Qn <= 0:
        bcirrus = 0.5*(1-Qn)**2
    elif Qn > 0 and Qn < 1:
        bcirrus = 1 - 0.5*(1-Qn)**2
    elif Qn >= 1:
        bcirrus = 1
    else:
        print('Something has gone very wrong')
    return(bcirrus)

How could 'Something has gone very wrong' have been triggered?

Here is the error:

/.local/lib/python3.6/site-packages/pint/numpy_func.py:289: RuntimeWarning: overflow encountered in exp
  result_magnitude = func(*stripped_args, **stripped_kwargs)
/.local/lib/python3.6/site-packages/pint/quantity.py:1160: RuntimeWarning: invalid value encountered in double_scalars
  magnitude = magnitude_op(new_self._magnitude, other._magnitude)
Something has gone very wrong
Traceback (most recent call last):
  File "./make_pcc_layer.py", line 122, in <module>
    pcc1, pcc2 = cc.PCC(layer_pressure,layer[i][j].tmp-273.15,layer[i][j].rh,layer[i][j].icmr)
  File "//weather_box/earth/clouds_and_contrails.py", line 119, in PCC
    PCC1 = bcirrus_func1(Qnstar)-bcirrus_func1(Qn)
  File "//weather_box/earth/clouds_and_contrails.py", line 39, in bcirrus_func1
    return(bcirrus)
UnboundLocalError: local variable 'bcirrus' referenced before assignment

EDIT: I added Qn to the "unreachable" print statement and it is NaN as was suggested. Here is the output:

Something has gone very wrong: Qn nan dimensionless

It says "dimensionless" because it is using Pint.

Craeft
  • 227
  • 1
  • 11

2 Answers2

3

Wild guess here, but is it possible that Qn is nan? If so it has strange (read: possibly intuitive) comparison behavior

>>> import math
>>> x = math.nan
>>> x < -1
False
>>> x > 1
False
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
2

Question: How could 'Something has gone very wrong' have been triggered?

import math
bcirrus_func1(math.nan)

Not A Number is tricky. It does not behave like other numbers. For one thing comparisons with it is always false. Even math.nan == math.nan is False.

Alex Telon
  • 1,107
  • 1
  • 14
  • 30
  • "It does not behave like other numbers"—it's _not_ a number. That's right in the name. – ChrisGPT was on strike Apr 01 '20 at 15:12
  • To clarify my comment: saying "other numbers" is misleading. It's like saying "zebras don't behave like other insects". – ChrisGPT was on strike Apr 01 '20 at 15:18
  • 2
    I understand and agree to some point. However `type(math.nan)` gives ``. `isinstance(math.nan, float)` is `True`. Furthermore `import numbers; print(isinstance(math.nan, numbers.Number))` is `True`. So in python it actually seems to be a number. :P – Alex Telon Apr 01 '20 at 15:25
  • So I would say that it is in fact NaN itself that is misleading here. It is a number that claims not to be one! Good comment as I think this was actually an interesting tidbit. There is more info about this in this javascript [question](https://stackoverflow.com/q/2801601/1723886). The official python [documentation](https://docs.python.org/3/library/math.html#math.nan) does not say much but there is a little bit more info there too – Alex Telon Apr 01 '20 at 15:38
  • The best reference is probably [IEEE 754](https://en.wikipedia.org/wiki/IEEE_754), the floating point standard. – ChrisGPT was on strike Apr 01 '20 at 15:41
  • In general I agree. But I would like to add that it really depends on what question on has and to what degree python chooses to follow that standard. For example the last sentence in the math [documentation](https://docs.python.org/3/library/math.html) explains how "Note that Python makes no effort to distinguish signaling NaNs from quiet NaNs, and behavior for signaling NaNs remains unspecified. Typical behavior is to treat all NaNs as though they were quiet." Here the python documentation stated that they only use qNaN even if IEEE 754 describes a sNaN. – Alex Telon Apr 01 '20 at 15:58