As a matter of curiosity, why are std::sqrt and std::pow overloaded just for a single type of parameter? Why are they not implemented as function/functor templates?
As a matter of curiosity, why are std::sqrt and std::pow overloaded just for a single type of parameter? Why are they not implemented as function/functor templates?
They are overloaded for types float, double, and long double.
There are no other floating-point types, so there would be no advantage in a more general solution using templates.
The C++ <cmath> header is almost, but not quite, a copy of C's <math.h> header.
C doesn't have overloading, so it provides three different square root functions:
float sqrtf(float arg);
double sqrt(double arg);
long double sqrtl(long double arg);
and similarly for the other floating-point functions.
C++'s <cmath> provides those functions and overloaded versions of sqrt for all three floating-point types.
Here's an example that demonstrates this:
#include <iostream>
#include <iomanip>
#include <cmath>
int main() {
auto f = std::sqrt(2.0F);
auto d = std::sqrt(2.0);
auto ld = std::sqrt(2.0L);
std::cout << std::setprecision(64);
std::cout << ' ' << sizeof f << ' ' << f << '\n';
std::cout << ' ' << sizeof d << ' ' << d << '\n';
std::cout << sizeof ld << ' ' << ld << '\n';
}
The output on my system, demonstrating different size and precision for each call:
4 1.41421353816986083984375
8 1.4142135623730951454746218587388284504413604736328125
16 1.4142135623730950487637880730318329369765706360340118408203125
The 'c' in <cmath> literally means that it is a collection of C library functions. By analogy, <stdio.h> can be included in C++ as <cstdio> etc. and since C doesn't have templates, then neither does anything in <cmath>.
Aside from that, what would parameterizing the type get you? These functions take and return doubles, which you can always covert to a plain int or a float if that's what you need.
I stand corrected on the second point: as M.A. and Francois Andrieux point out, there are situations where this would be useful. However, I believe the answer still stands as far as <cmath> is concerned.