# function changes upon condition

I have the function

f[x_,y_]:=1/(Sin[x]+Sin[y])


which obviously becomes infinite for $$x=\pi n, \;y=\pi n$$ where $$n\,\epsilon \, \mathbf{N}$$. I want to give f the value 0 for this case, i.e f[x,y]=0

I am trying

While[Mod[x, π] == 0 && Mod[y, π] == 0, f[x, y] == 0]


but it doesn't work.

I also tried

f[x_ /; Mod[x, π] == 0, y_ /; Mod[y, π]] := 0


didn't work either

How can I do it?

• For $y=-x$ your function is also ComplexInfinity. Do you want to similarly treat this case? Jan 4, 2021 at 20:47
• @yarchik I didn't need that for what I had in mind, but now that you mention it why not? Please post your answer
– geom
Jan 4, 2021 at 22:58
• Your solution below is good. In the case you want to make sure that there is no overflow you can do something like f[x_, y_] := If[Sin[x] + Sin[y] == 0, 0, 1/(Sin[x] + Sin[y])] Jan 5, 2021 at 10:18

f[x_, y_] := 0 /; Mod[x, π] == 0 && Mod[y, π] == 0


does the trick. Just figured it.

You can use a Piecewise definition as well:

Clear[f]
f[x_, y_] :=
Piecewise[
{{1/(Sin[x] + Sin[y]), Mod[x, Pi] != 0 || Mod[y, Pi] != 0}},
0
]

f[2, 3.]      (* 0.952002 *)
f[Pi, 3 Pi]   (* 0        *)
f[2, 10 Pi]   (* Csc[2]   *)
f[Pi, 2 Pi]   (* 0        *)


Here I made the default value $$0$$ explicit for future code readability, but technically Piecewise already defaults to $$0$$ if none of the conditions are met, so you could omit it.