A bug in our PI?

A couple days ago, we got a bug report that left us shaking our heads in collective confusion. The trouble? This graph:


Notice that f(π) is returning “undefined.” Trace doesn’t fare any better. But tan(π) = 0, so √tan(π) should also equal 0. What gives?

Well, it turns out that the problem lies deep in the heart of the javascript. Look at what the chrome console is giving us:

Aha! We should be getting zero when we take tan(π), but we’re actually getting a very small negative number. And you can’t take the square root of a negative number. This actually makes perfect sense: javascript only uses an approximation for π, and an approximation for tangent. Combine them, and you can’t expect exact results! But it’s giving the wrong answer here, and we hate giving the wrong answer.

How would you solve this, javascript coders? (send us your solution at jobs@desmos.com….). As of tonight, we have a solution that we think is pretty clever. Here it is in action:

… and we can all enjoy our bug-free pi.