Consider the function $$f: \mathbb R\to\mathbb R, x\mapsto\begin{cases}\frac{\sin^2(x)}x, & x\neq 0, \\ 0, & x=0.\end{cases}$$

It is true that $f\in\mathcal C^1(\mathbb R)$ and that $$f'(x)=\begin{cases}\frac{2 \sin (x) \cos (x)}{x}-\frac{\sin ^2(x)}{x^2}, & x\neq 0, \\ 1, & x=0.\end{cases}$$

(The fact that $f'(0)=1$ can be derived directly from the definition of the derivative.)

However, when I enter $f'(0)$ into Mathematica (in this case Wolfram Alpha but it makes no difference), I get $f'(0)=0$. Even more astonishingly, if I define $$g:\mathbb R\to\mathbb R, x\mapsto\begin{cases} \frac{\sin^2(x)}x, & x\neq0, \\ x, & x=0,\end{cases}$$

then $f=g$. However, Mathematica gives $g'(0)=1\color{red}\neq f'(0)$. It seems that Mathematica has a massive programming error when it comes to differentiating piecewise functions!

Is this a known error?

Note: This bug was originally discovered by Micah Windsor in a discussion on $f'(0)$ here.

  • 2
    $\begingroup$ if you do Limit[D[f[x], x], x -> 0] it gives 1 I do not know why it does not work on the Piecewise function itself. $\endgroup$ – Nasser Apr 10 '20 at 20:49
  • 2
    $\begingroup$ SeriesCoefficient[f[x], {x, 0, 1}] works (12.1.0), but SeriesCoefficient[f[$x], {$x, x, 1}, Assumptions -> x \[Element] Reals] gives a rather interesting answer. $\endgroup$ – Michael E2 Apr 11 '20 at 3:49
  • 1
    $\begingroup$ It's not a bug (or an error). Substituting 0 for x in the derivative gives an indeterminate form that happens to be at a removable singularity. Series can be quite useful in such cases (and so can Limit, but that's a heavier hammer). $\endgroup$ – Daniel Lichtblau Apr 11 '20 at 17:18
  • $\begingroup$ This is a task usually given in the introductory course of mathematical analysis. It is for proving considerations an example working for small values around a point, x=0. The students are requested to make use of the Taylor expansion Series and an idea of limit introduced in the lessons. It is not an example of Piesewise or D or f'. It is an easy hand or head task. The function is not in need for Piecewise at all. Taylor series multiplication may be another context. $\endgroup$ – Steffen Jaeschke Apr 12 '20 at 10:02
  • 1
    $\begingroup$ Limit[D[Sinc[x] Sin[x], x], x -> 0] works perfectly fine. $\endgroup$ – J. M.'s ennui Jan 30 at 10:11

See the possible issues section of the documentation for Piecewise. The proper method for defining f is


f[x_] := Piecewise[{{Sin[x]^2/x, x < 0 || x > 0}}]


(* 0 *)


(* 1 *)
  • 5
    $\begingroup$ Side note: in v9.0.1 f'[x] still gives the undesired result Piecewise[{{(2*Cos[x]*Sin[x])/x - Sin[x]^2/x^2, x > 0 || x < 0}}, 0], but D[f[x], x] gives the desired Piecewise[{{(2*Cos[x]*Sin[x])/x - Sin[x]^2/x^2, x < 0}, {1, x == 0}}, (2*Cos[x]*Sin[x])/x - Sin[x]^2/x^2], seems like a bug in v9. $\endgroup$ – xzczd Apr 11 '20 at 3:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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