簡體   English   中英

僅使用單精度浮點數逼近 [0,pi] 上的余弦

[英]Approximating cosine on [0,pi] using only single precision floating point

我目前正在研究余弦的近似值。 由於最終目標設備是使用 32 位浮點 ALU / LU 的自行開發,並且有專門的 C 編譯器,因此我無法使用 C 庫數學函數(cosf,...)。 我的目標是編寫在准確性和指令/周期數量方面不同的各種方法。

我已經嘗試了很多不同的近似算法,從 fdlibm、泰勒展開、pade 近似、使用楓樹的 remez 算法等開始......

但是,一旦我僅使用浮點精度來實現它們,就會顯着降低精度。 並且可以肯定:我知道雙精度,更高的精度完全沒有問題......

現在,我有一些近似值,在 pi/2(出現最大誤差的范圍)附近精確到幾千 ulp,我覺得我受到單精度轉換的限制。

為了解決主題參數減少:輸入是弧度。 我認為參數減少會由於除法/乘法而導致更多的精度損失......因為我的整體輸入范圍只有 0..pi,我決定將參數減少到 0..pi/2。

因此我的問題是:有沒有人知道余弦函數的單精度逼近精度高(在最好的情況下效率高)? 是否有任何算法可以優化單精度的近似值? 你知道內置的 cosf 函數是否在內部計算單精度雙精度的值嗎? ~

float ua_cos_v2(float x)
{
    float output;
    float myPi = 3.1415927410125732421875f;
    if (x < 0) x = -x;
    int quad = (int32_t)(x*0.63661977236f);//quad = x/(pi/2) = x*2/pi
    if (x<1.58f && x> 1.57f) //exclude approximation around pi/2
    {
        output = -(x - 1.57079637050628662109375f) - 2.0e-12f*(x - 1.57079637050628662109375f)*(x - 1.57079637050628662109375f) + 0.16666667163372039794921875f*(x - 1.57079637050628662109375f)*(x - 1.57079637050628662109375f)*(x - 1.57079637050628662109375f) + 2.0e-13f*(x - 1.57079637050628662109375f)*(x - 1.57079637050628662109375f)*(x - 1.57079637050628662109375f)*(x - 1.57079637050628662109375f)+ 0.000198412701138295233249664306640625f*(x - 1.57079637050628662109375f)*(x - 1.57079637050628662109375f)*(x - 1.57079637050628662109375f)*(x - 1.57079637050628662109375f)*(x - 1.57079637050628662109375f)*(x - 1.57079637050628662109375f)*(x - 1.57079637050628662109375f);
        output -= 4.37E-08f;
    }
    else {
        float param_x;
        int param_quad = -1;
        switch (quad)
        {
        case 0:
            param_x = x;
            break;
        case 1:
            param_x = myPi - x;
            param_quad = 1;
            break;
        case 2:
            param_x = x - myPi;
            break;
        case 3:
            param_x = 2 * myPi - x;
            break;
        }
        float c1 = 1.0f,
            c2 = -0.5f,
            c3 = 0.0416666679084300994873046875f,
            c4 = -0.001388888922519981861114501953125f,
            c5 = 0.00002480158218531869351863861083984375f,
            c6 = -2.75569362884198199026286602020263671875E-7f,
            c7 = 2.08583283978214240050874650478363037109375E-9f,
            c8 = -1.10807162057025010426514199934899806976318359375E-11f;
        float _x2 = param_x * param_x;
        output = c1 + _x2*(c2 + _x2*(c3 + _x2*(c4 + _x2*(c5 + _x2*(c6 + _x2*(c7 
        + _x2* c8))))));
        if (param_quad == 1 || param_quad == 0)
            output = -output;
    }
    return output;
}

~

如果我忘記了任何信息,請不要猶豫,問!

提前致謝

當然可以僅使用本機精度操作來計算 [0, π] 上的余弦,並且具有任何所需的誤差界限 >= 0.5 ulp。 然而,目標越接近正確舍入的函數,運行時需要的前期設計工作和計算工作就越多。

先驗函數的實現通常包括參數縮減、核心近似、最終修復以抵消參數縮減。 在參數減少涉及減法的情況下,需要通過顯式或隱式使用更高的精度來避免災難性取消。 隱式技術可以設計為僅依賴於本機精度計算,例如,在使用 IEEE-754 binary32 (單精度)時,將像 π 這樣的常數拆分為未計算的和,例如1.57079637e+0f - 4.37113883e-8f

當硬件提供融合乘加 (FMA) 運算時,通過本機精度計算實現高精度要容易得多。 OP 沒有指定他們的目標平台是否提供此操作,因此我將首先展示一種非常簡單的方法,該方法僅依靠乘法和加法來提供中等精度(最大誤差 < 5 ulps)。 我假設硬件符合 IEEE-754 標准,並假設float映射到 IEEE-754 binary32格式。

以下內容基於 Colin Wallace 題為“使用切比雪夫多項式逼近 sin(x) 到 5 ULP”的博客文章,在撰寫本文時該文章無法在線獲取。 我最初檢索它在這里和谷歌目前保留緩存副本這里 他們建議通過使用 sin(x)/(x*(x²-π²)) 的 x² 中的多項式,然后將其乘以 x*(x²-π²) 來逼近 [-π, π] 上的正弦。 更准確地計算 a²-b² 的標准技巧是將其重寫為 (ab) * (a+b)。 將 π 表示為兩個浮點數 pi_high 和 pi_low 的未計算總和避免了減法過程中的災難性取消,這將計算 x²-π² 變成((x - pi_hi) - pi_lo) * ((x + pi_hi) + pi_lo)

理想情況下,多項式核心近似應使用最小最大近似,該近似最小最大imum 誤差。 我在這里已經這樣做了。 為此可以使用各種標准工具,例如 Maple 或 Mathematics,或者根據 Remez 算法創建自己的代碼。

對於 [0, PI] 上的余弦計算,我們可以利用 cos (t) = sin (π/2 - t) 這一事實。 將 x = (π/2 - t) 代入 x * (x - π/2) * (x + π/2) 產生 (π/2 - t) * (3π/2 - t) * (-π/2) - 噸)。 常量可以像以前一樣分為高低部分(或頭和尾,使用另一種常見的習語)。

/* Approximate cosine on [0, PI] with maximum error of 4.704174 ulp */
float cosine (float x)
{
    const float half_pi_hi       =  1.57079637e+0f; //  0x1.921fb6p+0
    const float half_pi_lo       = -4.37113883e-8f; // -0x1.777a5cp-25
    const float three_half_pi_hi =  4.71238899e+0f; //  0x1.2d97c8p+2
    const float three_half_pi_lo = -1.19248806e-8f; // -0x1.99bc5cp-27
    float p, s, hpmx, thpmx, nhpmx;

    /* cos(x) = sin (pi/2 - x) = sin (hpmx) */
    hpmx = (half_pi_hi - x) + half_pi_lo;               // pi/2-x
    thpmx = (three_half_pi_hi - x) + three_half_pi_lo;  // 3*pi/2 - x
    nhpmx = (-half_pi_hi - x) - half_pi_lo;             // -pi/2 - x

    /* P(hpmx*hpmx) ~= sin (hpmx) / (hpmx * (hpmx * hpmx - pi * pi)) */
    s = hpmx * hpmx;
    p =         1.32729383e-10f;
    p = p * s - 2.33177868e-8f;
    p = p * s + 2.52223435e-6f;
    p = p * s - 1.73503853e-4f;
    p = p * s + 6.62087463e-3f;
    p = p * s - 1.01321176e-1f;
    return hpmx * nhpmx * thpmx * p;
}

下面我展示了一種經典方法,它首先在記錄象限時將參數減少到 [-π/4, π/4]。 然后象限告訴我們是否需要在這個主要近似區間上計算正弦或余弦的多項式近似,以及我們是否需要翻轉最終結果的符號。 此代碼假定目標平台支持 IEEE-754 指定的 FMA 操作,並且它通過標准 C 函數fmaf()映射為單精度。

代碼很簡單,除了用於計算象限的舍入模式為最近或偶數的 float-to-int 轉換,這是通過“幻數加法”方法執行的,並結合 2/ 的乘法π(相當於除以 π/2)。 最大誤差小於 1.5 ulps。

/* compute cosine on [0, PI] with maximum error of 1.429027 ulp */
float my_cosf (float a)
{
    const float half_pi_hi =  1.57079637e+0f; //  0x1.921fb6p+0
    const float half_pi_lo = -4.37113883e-8f; // -0x1.777a5cp-25
    float c, j, r, s, sa, t;
    int i;

    /* subtract closest multiple of pi/2 giving reduced argument and quadrant */
    j = fmaf (a, 6.36619747e-1f, 12582912.f) - 12582912.f; // 2/pi, 1.5 * 2**23
    a = fmaf (j, -half_pi_hi, a);
    a = fmaf (j, -half_pi_lo, a);

    /* phase shift of pi/2 (one quadrant) for cosine */
    i = (int)j;
    i = i + 1;

    sa = a * a;
    /* Approximate cosine on [-PI/4,+PI/4] with maximum error of 0.87444 ulp */
    c =               2.44677067e-5f;  //  0x1.9a8000p-16
    c = fmaf (c, sa, -1.38877297e-3f); // -0x1.6c0efap-10
    c = fmaf (c, sa,  4.16666567e-2f); //  0x1.555550p-5
    c = fmaf (c, sa, -5.00000000e-1f); // -0x1.000000p-1
    c = fmaf (c, sa,  1.00000000e+0f); //  1.00000000p+0
    /* Approximate sine on [-PI/4,+PI/4] with maximum error of 0.64196 ulp */
    s =               2.86567956e-6f;  //  0x1.80a000p-19
    s = fmaf (s, sa, -1.98559923e-4f); // -0x1.a0690cp-13
    s = fmaf (s, sa,  8.33338592e-3f); //  0x1.111182p-7
    s = fmaf (s, sa, -1.66666672e-1f); // -0x1.555556p-3
    t = a * sa;
    s = fmaf (s, t, a);

    /* select sine approximation or cosine approximation based on quadrant */
    r = (i & 1) ? c : s;
    /* adjust sign based on quadrant */
    r = (i & 2) ? (0.0f - r) : r;

    return r;
}

事實證明,在這種特殊情況下,使用 FMA 在准確性方面只提供了很小的好處。 如果我用((a)*(b)+(c))替換對fmaf(a,b,c)調用,最大誤差會最小地增加到 1.451367 ulps,也就是說,它保持在 1.5 ulps 以下。

我看到@njuffa 有一個很好的方法,但想提出另一種方法:

  • 角度最初可能是度數,而不是弧度,並利用它。
  • 不依賴於float是 IEEE。
  • fma 可能很弱,所以不要使用它。

使用整數數學執行范圍縮減,然后通過自我調整泰勒級數找到答案。

#include <assert.h>

static float my_sinf_helper(float xx, float term, unsigned n) {
  if (term + 1.0f == 1.0f) {
    return term;
  }
  return term - my_sinf_helper(xx, xx * term / ((n + 1) * (n + 2)), n + 2);
}

static float my_cosf_helper(float xx, float term, unsigned n) {
  if (term + 1.0f == 1.0f) {
    return term;
  }
  return term - xx * my_cosf_helper(xx, term / ((n + 1) * (n + 2)), n + 2);
}

// valid for [-pi/4 + pi/4]
static float my_sinf_primary(float x) {
  return x * my_sinf_helper(x * x, 1.0, 1);
}

// valid for [-pi/4 + pi/4]
static float my_cosf_primary(float x) {
  return my_cosf_helper(x * x, 1.0, 0);
}

#define MY_PIf 3.1415926535897932384626433832795f
#define D2Rf(d) ((d)*(MY_PIf/180))

float my_cosdf(float x) {
  if (x < 0) {x = -x;}
  unsigned long long ux = (unsigned long long) x;
  x -= (float) ux;
  unsigned ux_primary = ux % 360u;
  int uxq = ux_primary%90;
  if (uxq >= 45) uxq -= 90;
  x += uxq;
  switch (ux_primary/45) {
    case 7: //
    case 0: return my_cosf_primary(D2Rf(x));
    case 1: //
    case 2: return -my_sinf_primary(D2Rf(x));
    case 3: //
    case 4: return -my_cosf_primary(D2Rf(x));
    case 5: //
    case 6: return my_sinf_primary(D2Rf(x));
  }
  assert(0);
  return 0;
}

測試代碼

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#define DBL_FMT "%+24.17e"

typedef struct {
  double x, y0, y1, adiff;
  unsigned n;
} test;

test worst = {0};

int my_cosd_test(float x) {
  test t;
  t.x = x;
  t.y0 = cos(x*acos(-1)/180);
  t.y1 = my_cosdf(x);
  t.adiff = fabs(t.y1 - t.y0);
  if (t.adiff > worst.adiff) {
    t.n = worst.n + 1;
    printf("n:%3u x:" DBL_FMT " y0:" DBL_FMT " y1:" DBL_FMT " d:" DBL_FMT "\n", //
        t.n, t.x, t.y0, t.y1, t.adiff);
    fflush(stdout);
    worst = t;
    if (t.n > 100)
      exit(-1);
  }
  return t.adiff != 0.0;
}

float rand_float_finite(void) {
  union {
    float f;
    unsigned char uc[sizeof(float)];
  } u;
  do {
    for (size_t i = 0; i < sizeof u.uc / sizeof u.uc[0]; i++) {
      u.uc[i] = (unsigned char) rand();
    }
  } while (!isfinite(u.f) || fabs(u.f) > 5000);
  return u.f;
}

int my_cosd_tests(unsigned n) {
  my_cosd_test(0.0);
  for (unsigned i = 0; i < n; i++) {
    my_cosd_test(rand_float_finite());
  }
  return 0;
}

int main(void) {
  my_cosd_tests(1000000);
}

最壞的施法錯誤:+8.2e-08。 最大遞歸深度注:6。

n: 14 x:+3.64442993164062500e+03 y0:+7.14107074054115110e-01 y1:+7.14107155799865723e-01 d:+8.17457506130381262e-08

稍后我會更多地回顧。 我確實看到更廣泛的測試達到了大約 9e-08 最壞情況錯誤和一些 TBD 問題, x > about 1e10

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM