# Derivative of mod gives unacceptable results

I was wondering what will happen if I differentiate mod[x,1.0]. So, I tried few values:

In:= Mod[x, 1.0] /. x -> 0.0

Mod[x, 1.0] /. x -> 0.3

Mod[x, 1.0] /. x -> 1.0

Mod[x, 1.0] /. x -> 1.1

Out= 0.

Out= 0.3

Out= 0.

Out= 0.1

above looks fine. and I differentiated:

In:= D[Mod[x, 1.0], x] /. x -> 0.0

D[Mod[x, 1.0], x] /. x -> 0.1

D[Mod[x, 1.0], x] /. x -> 0.2

D[Mod[x, 1.0], x] /. x -> 0.3

Out= -35.2121

Out= 2.39809

Out= 0.973623

Out= 1.00005

where these values came from? I expected x is undefined where x=0,1,2,... otherwise 1.0. And results are even more unacceptable for me if I plot it!

In:= Plot[Evaluate[D[Mod[x, 1.0], x]], {x, -3, 3}] Can you explain what's wrong with my expectation(or formulation, perhaps)?

• These values makes no sense at all. M is doing this: !Mathematica graphics looks like floating point thingy. Try with D[Mod[x, 1], x] /. x -> 0 instead. In theory, Mod[x, anyNumber] should return $x$. and so derivative of x is 1. So you should get 1 for everything. Aug 28 '16 at 8:00
• Re Plot. This is one of those cases where the Mathematica chooses an absurd default range for the plot. Plot[Evaluate[D[Mod[x, 1.0], x]], {x, -3, 3}, PlotRange -> All] gives a much clearer picture. Aug 28 '16 at 11:13
• Mod is basically a discrete math function, with numeric extension (documented) into the world of continuous functions. That extension is not carried through to the world of calculus, at least not in any deep way. I would advocate that one use newer functionality intended for such purposes. SawToothWave would be the function to use in this instance. Aug 28 '16 at 16:50

Mathematica does not attempt to evaluate the derivative when working with exact values: the problem is one of inexact arithmetic.

D[Mod[x, 1], x] /. x -> 11/10 // InputForm
(* Derivative[1, 0][Mod][11/10, 1] *)


You can get a clue as to what is happening by defining

nmod[x_?NumericQ, y_?NumericQ] := (Sow[x]; Mod[x, y])


This allows us to see the numeric values at which Mod is evaluated. As expected, we have

nmod[1.1, 1]
(* 0.1 *)


Now evaluating the derivative, we see that Mod is called with a variety of values.

Reap[D[nmod[x, 1], x] /. x -> 1.1]
(* {2.39809, {{1.1, 1.11887, 1.13774, 1.1566, 1.17547, 1.19434, 1.21321,
1.23208, 1.25094, 1.26981, 1.28868, 1.30755, 1.32642, 1.34528,
1.36415, 1.38302, 1.40189, 1.42075, 1.43962, 1.45849, 1.47736,
1.49623, 1.51509, 1.53396, 1.55283, 1.5717, 1.08113, 1.06226,
1.0434, 1.02453, 1.00566, 0.986792, 0.967925, 0.949057, 0.930189,
0.911321, 0.892453, 0.873585, 0.854717, 0.835849, 0.816981,
0.798113, 0.779245, 0.760377, 0.741509, 0.722642, 0.703774,
0.684906, 0.666038, 0.64717, 0.628302}}} *)


It appears that D evaluates Mod at a number of points, perhaps fitting some function to the result and differentiating that. I couldn't see anything in the documentation explaining the procedure used.

While it is understandable that a discontinuous function should cause problems, it is strange that it should be unable to differentiate it at points well away from the discontinuity.

• I have reported the issue to Wolfram. Even now I can't believe it due to its severity.
– Laie
Aug 28 '16 at 11:07