Condition variable

Description of problem
I have a condition variable that goes to infinity and I am not finding a way to solve it using condition with “int()”.

image

Minimal code to reproduce problem

rho_PC = 100*Hz
for thresh_M in [249,250,251]*Hz:
      ((0*Hz)*int((thresh_M)==250*Hz) + (1/(thresh_M))*(rho_PC)*((rho_PC)-((thresh_M)))*exp(-(((thresh_M)-500*Hz)/(250*Hz*((thresh_M)-250*Hz)))*rho_PC)*int((thresh_M)!=250*Hz)))

I still get NaN when thresh_M = 250, is there a way to resolve this?

Thanks in advance

Hi @eliasmateo95, I agree that this is a bit annoying, but unfortunately 0*NaN + something will always result in NaN, so multiplying the NaN with a 0 is not enough, you need to make sure that it doesn’t occur in the first place. Since the NaN appears in this division: /(250*Hz*(thresh_M-250*Hz)) , you can use something like /(250*Hz*(int(thresh_M == 250*Hz)*Hz + thresh_M-250*Hz)) , which will use the value 250*1*Hz as the denominator, instead of the “correct” 250*0*Hz – the actual value does not matter, since you multiply the result with 0 later. So the total code of your example could look like this (I removed the 0*Hz*... in the beginning and a few unnecessary parentheses for readability):

rho_PC = 100*Hz
for thresh_M in [249,250,251]*Hz:
      print((1/thresh_M)*rho_PC*(rho_PC-thresh_M)*exp(-((thresh_M-500*Hz)/(250*Hz*(int(thresh_M==250*Hz)*Hz+thresh_M-250*Hz)))*rho_PC)*int(thresh_M!=250*Hz))

Output:

-1.49217905e-18 yHz
-0. Hz
-1.08401084e+21 YHz