I was trying to evaluate a sum over a piecewise function, not unlike this example. However, my piecewise function needed to be defined differently for even and odd k. This is a simpler version of my function, just so we can all agree that the sum exists:

f[k_]:=Piecewise[{{1, k==0}, {x^k/k!, OddQ[k]}, {x^k/k!, EvenQ[k]}}]

(I keep x and k undefined throughout. I know that this could be simplified, but this is just for the purposes of having a minimal working example.)

If, for example, I evaluate f[3], I get x^3/6, as expected. Or if I do

Sum[f[k], {k,0,5}]

I get the expected answer -- a nice little sum of terms involving powers of x. I can also do

Sum[x^k/k!, {k, 0, Infinity}]

And get E^x, as expected.

But if I evaluate

Sum[f[k], {k, 0, Infinity}]

I get 1. And, when I evaluate f[k], I get

Piecewise[{{1, k == 0}}, 0]

And yet, it still knows that f[k] is not just this when I pass an actual integer (e.g., 3), or ask for a finite Sum; it actually does remember more about f than these results suggest.

What is going on?


As J.M. pointed out in the comments, apparently OddQ and EvenQ evaluate on a symbol, before it is known whether it represents an integer -- never mind an even or odd one. The solution (adding IntegerPart) to the question george2079 linked does not help. But J.M. did suggest a workaround: change OddQ[k] to Mod[k,2]==1, and EvenQ[k] to Mod[k,2]==0. (I maintain that mathematica should be a little more clever about when it evaluates things, rather than silently given the right answer to a question I didn't mean to ask. But I guess it's legacy code by now.)

  • 3
    $\begingroup$ In brief: EvenQ[] and OddQ[] return False for any input that is manifestly not an integer. Use checks based on Mod[] instead. $\endgroup$ Mar 4, 2016 at 19:16
  • 1
    $\begingroup$ @Mike I have encountered the same problem ... that OddQ[x] returns False, without even knowing whether x is odd or not. I find it most 'unnatural', and most un-Mathematica like, and wish it were otherwise. $\endgroup$
    – wolfies
    Mar 5, 2016 at 6:02
  • 1
    $\begingroup$ @wolfies: The documentation states that OddQ[expr] returns True only if expr is an odd integer. $\endgroup$
    – Berg
    Mar 5, 2016 at 9:39
  • 1
    $\begingroup$ @Berg We use Mathematica because we are interested in a world that does not simply consist of numerics, but rather one of symbols that may or may not be positive, even, odd etc. Positive[x] does NOT return False, because it understands that x might be positive, so it returns Positive[x], unless we tell it something more about x. The same symbolic behaviour would be desirable for the ___Q funcs, such as EvenQ, OddQ etc ... which currently are basically numeric functions, and which, in my view, operate in a manner that appears inconsistent with the symbolic underpinnings of Mma. $\endgroup$
    – wolfies
    Mar 5, 2016 at 11:06
  • 1
    $\begingroup$ As I said in an earlier comment, @wolfies, the *Q[] functions are not intended to remain inert; they must return either True or False. Granted, a version of OddQ[]/EvenQ[] that remains inert for ambiguous arguments would be useful… $\endgroup$ Mar 5, 2016 at 11:33

3 Answers 3


As suggested by J.M., the solution is to use Mod:

f[k_] := Piecewise[{{1, k==0}, {x^k/k!, Mod[k, 2]==1}, {x^k/k!, Mod[k, 2]==0}}]

This returns the expected values for both

Sum[f[k], {k,0,5}]

and for

Sum[f[k], {k, 0, Infinity}]

As an interface-design issue, the choice to make *Q functions return a value even for generic symbols seems like a very poor and inconsistent one. But at least now I know to be wary...


It has already been pointed out that the problem originates from the fact that OddQ[x] evaluates to False if x is not a numerical value. Note that it is not hard to define test function that returns unevaluated for symbolic arguments:

CEvenQ[n_Integer] := EvenQ[n]
COddQ[n_Integer] := OddQ[n]

Now, COddQ[2] gives False and COddQ[x] is returned unevaluated. So,

f[k_] := Piecewise[{{1, k == 0}, {x^k/k!, COddQ[k]}, {x^k/k!,CEvenQ[k]}}]
Sum[f[k], {k, 0, 4}]
Sum[f[k], {k, 0, Infinity}]

gives the same finite sum as before and returns the second, infinite, sum unevaluated.

It is also possible to add some smartness to this function:

CEvenQ[z_Integer x_] := EvenQ[z] || CEvenQ[x]

gives True if any expression x is multiplied by an even integer. (Whether this rule is desirable depends, of course, on assumptions about the expressions handed to CEvenQ.)

  • $\begingroup$ The second (infinite) sum should return E^x. $\endgroup$
    – Mike
    Mar 6, 2016 at 20:28

For unknown reasons, I had difficulty getting the above solutions to work out. What worked for me was to use the Mod function with multiplication, as in:

f[k_]:=(1-Mod[k,2]) EvenFunc[k] + Mod[k,2] OddFunc[k]

Kinda hacky but got the job done.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.