Changing the formula and taking the square root only once has already been mentioned as a tip. In addition, you should take care that no division by zero occurs.
double mycalc(double x)
{
double sin_x = std::sin(x);
if (sin_x == 0.0) {
return std::numeric_limits<double>::quiet_NaN();
}
double temp = 2.0 * x * std::cos(x) / sin_x;
if (temp < 0.0) {
return std::numeric_limits<double>::quiet_NaN();
}
return std::sqrt(temp);
}
Perhaps you can write it with tan(x) = sin(x)/cos(x) more performant.