When it comes to writing a ternary search, what you usually do is this:
repeat 30/60/100/200 times {
double lm = l + (r - l) / 3;
double rm = r - (r - l) / 3;
if (f(lm) < f(rm))
r = rm;
else
l = lm;
}
Sources (notice the authors): 154861075 154863973
I recommend everyone who still writes like this to read this article . It will improve your ability to fit nested ternary searches. Also, I hope I don't need to explain why it is strictly better than the shown code.
Please, refrain from writing in the comments "but still their code works and is accepted!!1!".