RE:Como hacer un exponente parametrizado???
Hola, a ver si esto te sirve:
template<typename T>
T Exponente(T tNumero, int iExponente)
{
T tValor;
T tResultado;
int iExp;
tResultado = 0;
if (iExponente < 0) {
tValor = 1 / tNumero;
iExp = -iExponente;
} else if (iExponente == 0) {
tValor = 1;
iExp = 0;
} else if (iExponente > 0) {
tValor = tNumero;
iExp = iExponente;
}
tResultado = tValor;
for (int i = 1; i < iExp; i++)
{
tResultado *= tValor;
}
return tResultado;
}
La función anterior la podrías emplear como en el siguiente programa:
int main(int argc, char* argv[])
{
cout << Exponente<int>(5, 2) << endl;
cout << Exponente<long>(10L, 3) << endl;
cout << Exponente<float>(1.1F, 2) << endl;
cout << Exponente<double>(-4, 4) << endl;
cout << Exponente<long>(2, 30) << endl;
cout << Exponente<long>(5, 0) << endl;
cout << Exponente<double>(5.0, -1) << endl;
cout << Exponente<double>(10, -4) << endl;
system("pause");
return 0;
}
que tiene la siguiente salida:
25
1000
1.21
256
1073741824
1
0.2
0.0001
Presione una tecla para continuar . . .
Saludos.