How TikZ returns "Package PGF Math Error: You've asked me to divide `0' "

I want to know how `TikZ` detects a zero on the denominator and gives this error:

`Package PGF Math Error: You've asked me to divide ... by '0' ...`

I want to use this to write a macro `\undef{f(x)}` that returns the points `x` that the given function is not defined.

• You'll be disappointed, but I'm pretty sure `pgfmath` doesn't do what you think it does. When `\pgfmathparse` is used, all variables used in the expression must be known. When it sees a division, the divisor is known. If this divisor is 0, `\pgfmath` issues the error you've pasted. So, with your notations, for a given `x`, it can tell you whether `f(x)` can be computed; but it can't reasonably give you all values of `x` for which `f(x)` can be computed. Mathematically, it would have to check all of R (assuming we are working on R), which is uncountable. In `pgfmath`, the number of possible Commented Sep 29, 2020 at 11:48
• values is finite but huge, and checking all of them would be completely uninteresting, as you would only get numbers of the form k*(2^n) for some k and n in Z. No sqrt(2), no pi, etc. Another way to see that your expectations aren't very realistic is to think about what the outcome should be for `f(x) = 1/rand()` (`rand()` yields supposedly uniformly distributed numbers between -1 and 1). Commented Sep 29, 2020 at 11:51
• Thanks for comment. anyway I am curious what `pgfmath` do and returns that error. i.e. What conditions does it check for this error?
– user108724
Commented Sep 29, 2020 at 12:14
• I didn't look at the code, but as said, my guess is that when it parses an expression to compute its numerical value (i.e., f(x) for a given, known x) and sees a division, if the divisor is 0, it gives the error (maybe divisors very close to 0 yet different from 0 in the internal representation give the error too, maybe not—I don't know). Commented Sep 29, 2020 at 12:38
• @frougon Do you want to turn your comments into an answer? Commented Nov 22, 2022 at 10:28

When `\pgfmathparse` is used, all variables used in the expression must be known. When it sees a division, the divisor is known. If this divisor is 0, PGFMath issues the error you've pasted.
Another way to see that your expectations aren't very realistic is to think about what the outcome should be for f(x) = `1/rand()` (`rand()` yields supposedly uniformly distributed numbers between −1 and 1).
• The `/` function indeed just checks whether its divisor is exactly 0 and only then issues the error message without any fallback on the result. Commented Dec 13, 2022 at 14:42