When you’re looking at it, it just stays there, constant and still. But if you turn your back for just an instant at a point in the Cantor set, the function grows impossibly quickly. It’s not a Weeping Angel, it’s the Devil’s staircase, or, if you’re a little less whimsical, the Cantor function. One of the first times I saw the Cantor set deployed in all its glory was to define this function, a common counterexample in analysis or calculus rather than topology. (If you haven't seen the Cantor set before, check out my post about it from last week.)

Like the Cantor set itself, there are a few different ways to think about the Cantor function. The first is to think about building it iteratively, just like you’d build the Cantor set. In the end, we want to define the function on the whole interval [0,1]. We’ll start by taking the first interval we remove when we construct the Cantor set and saying that the function takes the value of 1/2 on that interval. So f(x)=1/2 if x is between 1/3 and 2/3. Next, we’ll define what it does on the next two intervals we remove. We’ll send the interval (1/9,2/9) to 1/4 and (7/9,8/9) to 3/4. We keep following this pattern: every interval that’s removed to make the Cantor set gets sent to some fraction with a denominator that’s a power of 2. The rest of the points just "fill in the gaps."

This gif shows the process nicely.

There’s another pretty slick way of describing the Cantor function if you’re willing to think in different bases. First, you represent all the numbers between 0 and 1 in base 3, just like you do to describe the Cantor set. If a number contains a 1, truncate it at the first 1, or equivalently, replace all the following digits with 0s. If the number doesn’t contain a 1, leave it alone for now. Either way, the next step is to replace all the 2s in your numbers with 1s and interpret the result in base 2 instead of base 3. For example, the number 1/2 is written 0.111... in base 3. We truncate it at the first 1 to get 0.1 or 0.100..., and then we interpret that number base 2. It happens that we get 1/2 again.

If you remember that the Cantor set is the set of numbers that can be written in base 3 without a 1, it seems reasonable that numbers with and without 1s in them should be treated differently. When we truncate at the first 1, we're making sure that the function takes the same value for every number between 1/3 and 2/3, the first interval we remove when we're building the Cantor set iteratively.

When we defined the Cantor set itself, we had to be a little bit careful about rational numbers that ended in a 1. Surprisingly enough, with the Cantor function, we don’t have to be careful! Rational numbers that end in a one can be rewritten to end in an infinite string of twos instead: 1/3, or 0.1, is the same as 0.02222…, and so on. (If this seems strange, remember that 0.9999…=1. The number 2 in base 3 functions like the number 9 in base 10.)

You might think that the ambiguity would be a problem because the value of the Cantor function at a point depends on whether that point contains a 1 or not. Let’s look at 1/3, or 0.1, or 0.02222…. If we think of this number as 0.1, the definition of the Cantor function tell us that the function value should be 0.1 base 2, or 1/2. If we think of the number as 0.02222…, we should change all the twos to ones and get 0.01111…. When we interpret that number base 2, we get 1/2 as well. This works in general. So we can write the endpoints of the Cantor set any way we want and get the same number.

The function is continuous everywhere, meaning it’s defined on the whole interval [0,1] and doesn’t jump. (I usually describe continuity intuitively by saying that a function is continuous if you can draw its graph without lifting your pencil up from the paper, but that doesn’t work too well if we’re thinking about building this function iteratively.) The continuity isn’t too miraculous. The derivative is where things get interesting and the Cantor function becomes a good counterexample.

In calculus, a function is differentiable, or has a derivative, at a specific point if you can draw a tangent line to its graph at that point. If you’ve taken calculus, you might remember your teacher getting a little misty-eyed about the beauty of the fundamental theorem of calculus. (Or is that just me?) The theorem says that with certain conditions, finding the area under a curve (integration) and finding the slope of a curve (differentiation) are “inverse” operations. Specifically, one part of the fundamental theorem of calculus states that for “nice enough” functions, the integral of the derivative of a function tells you the difference between the function’s value at two endpoints. The Cantor function helps us understand what "nice enough" means.

At every point not in the Cantor set, the Cantor function is flat, so it’s easy to draw a tangent line to the graph at that point. (The derivative there is 0.) As we saw last week, the set of points not in the Cantor set add up to length 1, so in a technical sense, the function has derivative 0 “almost everywhere.” In terms of the Weeping Angel analogy, this means that if we happen to look over at it at any point in time, it will probably be standing perfectly still because we'll probably choose a point that isn't in the Cantor set. At the points of the Cantor set, it’s another story. The best-behaved Cantor set points have an “infinite” derivative, or a vertical tangent line. The more pathological ones don’t even have that. (For a more technical discussion of exactly how non-differentiable the Cantor function is, check out this 1993 paper by Richard Darst (pdf).)

In math, “almost everywhere” is good enough for a lot of stuff. It would be tempting to think that if a function is continuous everywhere and differentiable almost everywhere, it should be nice enough for the integral of the derivative of a function to tell us something meaningful about the function. But the Cantor function shows us that those criteria aren’t sufficient. The derivative is 0 almost everywhere, so the integral of the derivative 0, but the starting value is 0 and the ending value is 1. While we weren't looking, it crept up on us. If the Devil’s staircase and Weeping Angels aren't scary enough for you, try Weierstrass’smonsters.”