[Tex/LaTex] How to draw this piecewise graph using PGF/TikZ


enter image description here

Is there an easy way to define the condition as "rational" and "irrational" using PGF/TikZ? A minimal example would be greatly appreciated.

This is one of the rational/irrational graphs in Calculus by Michael Spivak book, page 97.
enter image description here

Best Answer

There is no way to represent graphically this function. Your drawing tool has a thickness. If you try representing the point (1/2,0) that belongs to the graph of the Dirichlet function and is the thickness of the pencil, you'll be covering infinitely many points of the form (t,0), with t irrational that don't belong to the graph: there are infinitely many irrational numbers in the interval (-ε+1/2,ε+1/2), for any ε>0. The same if you want to draw a point of the graph with irrational x-coordinate.

Apart from this, for this kind of drawings you need numbers in floating point representation, which are all rational; but not even all rational numbers in the interval [0,1] are representable in the computer as floating point numbers.

Thus the best representation of this function you'd get would be two segments, which is useless.

Related Question