jacknpoe

Funções Exponenciais e Logarítmicas

Mar 28th, 2026 (edited)
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.62 KB | None | 0 0
  1. // Funções Exponenciais e Logarítmicas (após muita otimização com o ChatGPT)
  2.  
  3. #include <iostream>   // para cout
  4. using namespace std;
  5.  
  6. // constantes:
  7. // MAXLN para laço de ln()
  8. // MAXEXP para laço de exp()
  9. // LN2 que é ln(2) pré-calculado para não precisar chamar a função ln(2)
  10. #define MAXLN 15
  11. #define MAXEXP 20
  12. #define LN2 0.6931471805599453094L
  13.  
  14. // função base ^ expoente para expoentes inteiros (usada por exp)
  15. long double powint(long double base, long long expoente) {
  16.     if (expoente < 0) return powint(1.0L / base, -expoente);
  17.     if (expoente == 0) return 1.0L;
  18.     if (expoente % 2 == 0) return powint(base * base, expoente / 2);
  19.     return base * powint(base * base, (expoente - 1) / 2);
  20. }  
  21.  
  22. // função ln() normalizada, de uso geral
  23. long double ln(long double x) {
  24.     int indice; long long ajuste = 0LL;
  25.     long double termo, soma = 0.0L, yy;
  26.  
  27.     while (x >= 2.0L) { x /= 2.0L; ajuste++; }
  28.     while (x < 1.0L) { x *= 2.0L; ajuste--; }
  29.  
  30.     termo = (x - 1.0L) / (x + 1.0L);
  31.     yy = termo * termo;
  32.  
  33.     for (indice = 1; indice <= MAXLN; indice++) {
  34.         soma += termo / (2.0L * (long double)indice - 1.0L);
  35.         termo *= yy;
  36.         if (soma + termo == soma) break;
  37.     }
  38.  
  39.     return 2.0L * soma + ajuste * LN2;
  40. }
  41.  
  42. // função exp() normalizada, de uso geral
  43. long double exp(long double x) {
  44.     int indice; long long ajuste;
  45.     long double termo = 1.0L, soma = 1.0L;
  46.  
  47.     ajuste = (long long)(x / LN2) - (x < 0.0L ? 1LL : 0LL);
  48.     x = x - ajuste * LN2;
  49.  
  50.     for (indice = 1; indice <= MAXEXP; indice++) {
  51.         termo *= x / (long double)indice;
  52.         soma += termo;
  53.         if (soma + termo == soma) break;
  54.     }
  55.  
  56.     return soma * powint(2.0L, ajuste);
  57. }
  58.  
  59. // pow() é calculado pela clássica exp(ln(base) * expoente)
  60. long double pow(long double base, long double expoente) {
  61.     return exp(ln(base) * expoente);
  62. }
  63.  
  64. int main() {
  65.     // precisão suficiente para exibir os dígitos
  66.     // significativos sem expor ruído numérico
  67.     cout.precision(16);
  68.  
  69.     cout << "testes de ln()" << endl;
  70.     cout << "ln(1.0) = " << ln(1.0L) << endl;
  71.     cout << "ln(1.03) = " << ln(1.03L) << endl;
  72.     cout << "ln(10.0) = " << ln(10.0L) << endl;
  73.     cout << "ln(1000.0) = " << ln(1000.0L) << endl << endl;
  74.  
  75.     cout << "testes positivos de exp()" << endl;
  76.     cout << "exp(0.0) = " << exp(0.0L) << endl;
  77.     cout << "exp(0.5) = " << exp(0.5L) << endl;
  78.     cout << "exp(10) = " << exp(10.0L) << endl << endl;
  79.  
  80.     cout << "testes negativos de exp()" << endl;
  81.     cout << "exp(-1.0) = " << exp(-1.0L) << endl;
  82.     cout << "exp(-3.7) = " << exp(-3.7L) << endl;
  83.     cout << "exp(-10.0) = " << exp(-10.0L) << endl;
  84.     cout << "exp(-2 * LN2) = " << exp(-2.0L * LN2) << endl << endl;
  85.  
  86.     cout << "testes de pow() quando o resultado é 1" << endl;
  87.     cout << "pow(1.0, 35.0) = " << pow(1.0L, 35.0L) << endl;
  88.     cout << "pow(10.0, 0.0) = " << pow(10.0L, 0.0L) << endl << endl;
  89.  
  90.     cout << "testes de quatro formas de fazer 10⁻¹⁵" << endl;
  91.     cout << "powint(0.1, 15) = " << powint(0.1L, 15) << endl;
  92.     cout << "powint(10.0, -15) = " << powint(10.0L, -15) << endl;
  93.     cout << "pow(0.1, 15.0) = " << pow(0.1L, 15.0L) << endl;
  94.     cout << "pow(10.0, -15.0) = " << pow(10.0L, -15.0L) << endl << endl;
  95.  
  96.     cout << "outros testes de pow()" << endl;
  97.     cout << "pow(1.03, 0.5) = " << pow(1.03L, 0.5L) << endl;
  98.     cout << "pow(1.03, 120.0) = " << pow(1.03L, 120.0L) << endl;
  99.     cout << "pow(1.03, 300000.0) = " << pow(1.03L, 300000.0L) << endl;
  100.     cout << "pow(1.1, 120.0) = " << pow(1.1L, 120.0L) << endl;
  101.     cout << "pow(1.1, 300000.0) = " << pow(1.1L, 300000.0L) << endl;
  102. }
Add Comment
Please, Sign In to add comment