一、源碼
這段代碼實現了一個高精度的正弦函數計算核心(kernel sin function),用于計算在區間約[-π/4, π/4]內的正弦值。
// origin: FreeBSD /usr/src/lib/msun/src/k_sin.c
//
// ====================================================
// Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
//
// Developed at SunSoft, a Sun Microsystems, Inc. business.
// Permission to use, copy, modify, and distribute this
// software is freely granted, provided that this notice
// is preserved.
// ====================================================const S1: f64 = -1.66666666666666324348e-01; /* 0xBFC55555, 0x55555549 */
const S2: f64 = 8.33333333332248946124e-03; /* 0x3F811111, 0x1110F8A6 */
const S3: f64 = -1.98412698298579493134e-04; /* 0xBF2A01A0, 0x19C161D5 */
const S4: f64 = 2.75573137070700676789e-06; /* 0x3EC71DE3, 0x57B1FE7D */
const S5: f64 = -2.50507602534068634195e-08; /* 0xBE5AE5E6, 0x8A2B9CEB */
const S6: f64 = 1.58969099521155010221e-10; /* 0x3DE5D93A, 0x5ACFD57C */// kernel sin function on ~[-pi/4, pi/4] (except on -0), pi/4 ~ 0.7854
// Input x is assumed to be bounded by ~pi/4 in magnitude.
// Input y is the tail of x.
// Input iy indicates whether y is 0. (if iy=0, y assume to be 0).
//
// Algorithm
// 1. Since sin(-x) = -sin(x), we need only to consider positive x.
// 2. Callers must return sin(-0) = -0 without calling here since our
// odd polynomial is not evaluated in a way that preserves -0.
// Callers may do the optimization sin(x) ~ x for tiny x.
// 3. sin(x) is approximated by a polynomial of degree 13 on
// [0,pi/4]
// 3 13
// sin(x) ~ x + S1*x + ... + S6*x
// where
//
// |sin(x) 2 4 6 8 10 12 | -58
// |----- - (1+S1*x +S2*x +S3*x +S4*x +S5*x +S6*x )| <= 2
// | x |
//
// 4. sin(x+y) = sin(x) + sin'(x')*y
// ~ sin(x) + (1-x*x/2)*y
// For better accuracy, let
// 3 2 2 2 2
// r = x *(S2+x *(S3+x *(S4+x *(S5+x *S6))))
// then 3 2
// sin(x) = x + (S1*x + (x *(r-y/2)+y))
#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
pub(crate) fn k_sin(x: f64, y: f64, iy: i32) -> f64 {let z = x * x;let w = z * z;let r = S2 + z * (S3 + z * S4) + z * w * (S5 + z * S6);let v = z * x;if iy == 0 { x + v * (S1 + z * r) } else { x - ((z * (0.5 * y - v * r) - y) - v * S1) }
}
二、源碼分析
常量定義
代碼開頭定義了一系列常量S1到S6,這些是泰勒級數展開的系數,用于多項式近似計算正弦函數。每個常量后面都有十六進制表示,這是IEEE 754浮點數的二進制表示。
函數參數
-
x: 輸入值,假定其絕對值小于約π/4
-
y: x的尾部(即x的高精度表示中,除去主要部分后的剩余部分)
-
iy: 標志位,指示y是否為0
算法說明
-
對稱性處理:利用sin(-x) = -sin(x)的性質,只需處理正數x的情況。
-
特殊值處理:調用者需要自己處理sin(-0) = -0的情況,以及非常小的x(sin(x) ≈ x)。
-
多項式近似:在[0, π/4]區間內,使用13次多項式近似sin(x),形式為:
sin(x) ≈ x + S1*x3 + ... + S6*x13
近似精度非常高,誤差不超過2???。
- 尾部修正:當y非零時,使用sin(x+y) ≈ sin(x) + (1 - x2/2)*y進行修正,但為了更高精度,使用了更復雜的修正公式。
代碼實現
-
計算z = x2和w = z2(即x?)。
-
計算多項式r,這是高階項的組合
r = S2 + z*(S3 + z*S4) + z*w*(S5 + z*S6)
-
計算v = z*x(即x3)。
-
根據iy的值選擇不同的計算路徑:
- 如果iy == 0(y為零),則計算:
x + v*(S1 + z*r)
這對應于多項式近似的主要部分。
- 如果iy != 0(y非零),則計算更復雜的表達式,包括對y的修正:
x - ((z*(0.5*y - v*r) - y) - v*S1)
精度保證
這個實現使用了高度優化的多項式近似,保證了在目標區間內極高的計算精度(誤差不超過2???),同時保持了良好的性能。
注意事項
-
調用者需要確保輸入x在[-π/4, π/4]范圍內。
-
特殊值(如-0.0)需要由調用者處理。
-
對于非常小的x,調用者可以直接使用x近似sin(x)。
這段代碼是數學庫中的底層函數,通常不會被直接調用,而是由更上層的正弦函數封裝后提供給用戶。