# Error "Power::infy: Infinite expression 1/0 encountered". Why do I get this?

Bug introduced in 11.0 or earlier and fixed in 12.3.1 or earlier

I need to write this function piecewise and to make a graph for it, x[-3,3]:

f[x_] = Piecewise[{{1/(Cos[x^2 + 2]), x <= 0}, {Sqrt[x^2/(x + 1)],
0 < x <= 1}, {(x^2 - 2 x)/Abs[2 - x], True}}]
Plot[f[x], {x, -3, 3}]


Error:

Power::infy: Infinite expression 1/0 encountered.

Why does this happen, and how do I fix it?

• People here generally like users to post code as Mathematica code instead of images or TeX, so they can copy-paste it. It makes it convenient for them and more likely you will get someone to help you. You may find this this meta Q&A helpful Jan 22, 2017 at 15:04
• (1) Do you see the function going to infinity anywhere in your graph? And (2) Have you seen Quiet[]? Jan 22, 2017 at 15:07
• (Tip: use @MichaelE2 to make sure I'm notified.) Actually, I think it's more complicated than I first thought. It seems to be the Sqrt expression. I think x = -1. is being plugged into it. Why?, you might ask. Well, it does some symbolic analysis to determine discontinuities in the graph. It's probably a bug, but I haven't been able to track it down yet. You can use Quiet if it's annoying. Jan 22, 2017 at 15:33
• Anyone else agree it's a bug? Jan 22, 2017 at 16:49
• See (a136108) for further confirmation of the bug. Jan 25, 2017 at 13:14

I think it's a bug in the symbolic analysis of function done to compute the discontinuities. It's a minor bug, in that it does not affect the result, but only allows in an internal test to come back to the user-level as an error message.

It is connected with the complexity of analyzing the expression with Sqrt. Here is a slightly simpler working example:

ClearAll[f];
f[x_] := Piecewise[{{1/(x + 2), x <= 0}, {Sqrt[1/(x + 1)], 0 < x <= 1}}, x];
Plot[f[x], {x, -3, 0}]


Power::infy: Infinite expression 1/0. encountered.

ClearAll[g];
g[x_] := Piecewise[{{1/(x + 2), x <= 0}, {1/(x + 1), 0 < x <= 1}}, x];
Plot[g[x], {x, -3, 0}]

(* no error messages *)


?NumericQ protection eliminates both symbolic analysis and error messages:

ClearAll[h];
h[x_?NumericQ] := Piecewise[{{1/(x + 2), x <= 0}, {Sqrt[1/(x + 1)], 0 < x <= 1}}, x];
Plot[h[x], {x, -3, 0}]

(* no error messages *)


FullSimplify the function definition and cancellation will avoid the division by zero

f[x_] = Piecewise[{{1/(Cos[x^2 + 2]), x <= 0}, {Sqrt[x^2/(x + 1)],
0 < x <= 1}, {(x^2 - 2 x)/Abs[2 - x], True}}] // FullSimplify


Plot[f[x], {x, -3, 3}]


Related discussion is here. All of the clauses of conditional expressions are evaluated even if the conditions are false. It is a matter of taste to decide whether this is a bug or not. To me, it seems like a very strange feature, requiring additional processing.

We can see that if we look closer at the Michael E2's example:

ClearAll[f];
f[x_] := Piecewise[{{1/(x + 2), x <= 0}, {Sqrt[1/(x + 1)], 0 < x <= 1}}, x]
Plot[f[x], {x, -3, 0}]


Note the discontinuity of the upper curve at x = -1, which is an indication that MMA is evaluating all expressions of Piecewise before rendering.

We can wrap the function that causes this in Unevaluated to prevent such behavior.

ClearAll[f];
f[x_] := Piecewise[{{1/(x + 2), x <= 0}, {Unevaluated@Sqrt[1/(x + 1)], 0 < x <= 1}}, x]
Plot[f[x], {x, -3, 0}]


No errors

The same fix applies to the OP example:

f[x_] = Piecewise[{{1/(Cos[x^2 + 2]), x <= 0}, {Unevaluated[Sqrt[x^2/(x + 1)]],  0 < x <= 1}, {(x^2 - 2 x)/Abs[2 - x], True}}];
Plot[f[x], {x, -3, 3}]


No errors

What seems much more troublesome is the workaround that Bob Hanlon discovered and which does nothing more than taking 1 in the numerator out of the square root. In other words, there is still a possibility for the denominator to become 0 when evaluated unconditionally (at x == -1), so we should expect the same error, but for some reason there is no error now. This is a bug for sure, as it is at least inconsistent and therefore needs to be fixed.

g[x_] := Piecewise[{{1/(x + 2), x <= 0}, {1/Sqrt[(x + 1)], 0 < x <= 1}}, x]
Plot[g[x], {x, -3, 0}]


No errors!

For the sake of completeness, the mechanism that is probably glitching here is Exclusions, as Michael E2's example can also be fixed by explicitly indicating types of checks we want:

f[x_] := Piecewise[{{1/(x + 2), x <= 0}, {Sqrt[1/(x + 1)], 0 < x <= 1}}, x]
Plot[f[x], {x, -3, 0}, Exclusions -> "Singularities"]


No errors

This, however, wouldn't work nicely for the OP example, as it has more than one type of exclusions, so we need to use one of the alternative solutions in this post or thread.