Skip to content

Instantly share code, notes, and snippets.

@enzolovesbacon
Created September 23, 2015 19:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save enzolovesbacon/5933d4dc698d9d7b4bd9 to your computer and use it in GitHub Desktop.
Save enzolovesbacon/5933d4dc698d9d7b4bd9 to your computer and use it in GitHub Desktop.
/*
* Potencias mapeadas fixadamente
*/
uint[] _P = new uint[] { 0, 1, 5, 10, 15, 20, 25, 30, 35, 38, 45, 50 };
/*
* Fatores fixos entre as variacoes das potencias em _P
*/
double[] _F = new double[] { 0.0, 0.4192505896, 2.200277792, 4.271982912, 1.543209877, 1.167113087, 1.055373328, 0.9624752296, 0.9188272963, 0.87106949, 0.8175748308, 0.8109861592 };
/*
* Retorna um fator, dado uma potencia (power)
*/
double calcFactor(uint power)
{
uint p_prev, p_next;
double f_prev, f_next;
/* fator 0 se a potencia for 0 */
if (power == 0)
{
return 0.0;
}
if (power == 1)
{
return _F[1];
}
/* Utiliza a mesma formula (extrapola) para todas as potencias acima de 50 */
if (power >= (uint)50)
{
double retv = _F[10] + (((_F[11] - _F[10]) / (_P[11] - _P[10])) * (power - _P[10]));
return retv;
}
/* Potencias entre multiplos de 5, interpola para conseguir o fator */
for (int i = 2; i < _P.Length; i++)
{
if (power >= _P[i - 1] && power <= _P[i])
{
p_prev = _P[i - 1];
p_next = _P[i];
f_prev = _F[i - 1];
f_next = _F[i];
double retv = f_prev + (((f_next - f_prev) / (p_next - p_prev)) * (power - p_prev));
return retv;
}
continue;
}
/* Nao deve ser atingido nunca */
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment