Better HN
topnewbestaskshowjobs
Log inSign up

njuffa

1 karma · joined Jun 24, 2024 (2y ago) · 1 submissions

Submissions

comment 2y ago · link · parent

Re: centers on the octave [2/3, 4/3] but the error is actually more balanced across [1/sqrt(2), sqrt(2)]

For the computation of the natural logarithm using binary floating-point arithmetic there is no practical difference, i.e. the overall accuracy that can be achieved is basically the same. Here is a worked example based on minimax core approximations extracted from my personal code collection (design and implementation are my own; no guarantee of fitness for any particular purpose).

  /* natural logarithm */
  float my_logf (float a)
  {
      float m, r, s, t, i, f;
      int32_t e;
  #if LOG_VARIANT == 1
      // max ulp err = 0.86280  
      const float cutoff_f = 0.707106781f;
  #elif LOG_VARIANT == 3
      // max ulp err = 0.85089  
      const float cutoff_f = 0.666666667f;
  #endif // LOG_VARIANT
      if ((a > 0.0f) && (a <= 0x1.fffffep+127f)) { // 3.40282347e+38f
          m = frexpf (a, &e);
          if (m < cutoff_f) {
              m = m + m;
              e = e - 1;
          }
          i = (float)e;
          f = m - 1.0f;
          s = f * f;
  #if LOG_VARIANT == 1
          /* Compute log1p(m) for m in [sqrt(0.5)-1, sqrt(2.0)-1] */
          r =             -0x1.384000p-4f;  // -0.076232910
          t =              0x1.084000p-3f;  //  0.129028320
          r = fmaf (r, s, -0x1.0f218cp-3f); // -0.132388204
          t = fmaf (t, s,  0x1.226b04p-3f); //  0.141805679
          r = fmaf (r, s, -0x1.5427a4p-3f); // -0.166091233
          t = fmaf (t, s,  0x1.99a35ap-3f); //  0.200018600
          r = fmaf (r, s, -0x1.000416p-2f); // -0.250015587
          r = fmaf (t, f, r);
          r = fmaf (r, f,  0x1.55555cp-2f); //  0.333333433
          r = fmaf (r, f, -0x1.fffffap-2f); // -0.499999911
  #elif LOG_VARIANT == 3
          /* Compute log1p(f) for f in [-1/3, 1/3] */
          r =             -0x1.0ae000p-3f;  // -0.130310059
          t =              0x1.208000p-3f;  //  0.140869141
          r = fmaf (r, s, -0x1.f1988ap-4f); // -0.121483363
          t = fmaf (t, s,  0x1.1e5740p-3f); //  0.139814854
          r = fmaf (r, s, -0x1.55b36ep-3f); // -0.166846141
          t = fmaf (t, s,  0x1.99d8b2p-3f); //  0.200120345
          r = fmaf (r, s, -0x1.fffe02p-3f); // -0.249996200
          r = fmaf (t, f, r);
          r = fmaf (r, f,  0x1.5554fap-2f); //  0.333331972
          r = fmaf (r, f, -0x1.000000p-1f); // -0.500000000
  #endif // LOG_VARIANT
          r = fmaf (r, s, f);
          r = fmaf (i, 0x1.62e430p-01f, r); //  0.693147182  // log(2)
      } else {
          r = a + a;  // silence NaNs if necessary
          if (a  < 0.0f) r = 0.0f/0.0f; // QNaN INDEFINITE
          if (a == 0.0f) r = -INFINITY; // -INF
      }
      return r;
  }