#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)] pubfn pow(x: f64, y: f64) -> f64 { let t1: f64; let t2: f64;
let (hx, lx): (i32, u32) = ((x.to_bits() >> 32) as i32, x.to_bits() as u32); let (hy, ly): (i32, u32) = ((y.to_bits() >> 32) as i32, y.to_bits() as u32);
letmut ix: i32 = (hx & 0x7fffffff) as i32; let iy: i32 = (hy & 0x7fffffff) as i32;
/* x**0 = 1, even if x is NaN */ if ((iy as u32) | ly) == 0 { return1.0;
}
/* 1**y = 1, even if y is NaN */ if hx == 0x3ff00000 && lx == 0 { return1.0;
}
/* NaN if either arg is NaN */ if ix > 0x7ff00000
|| (ix == 0x7ff00000 && lx != 0)
|| iy > 0x7ff00000
|| (iy == 0x7ff00000 && ly != 0)
{ return x + y;
}
/* determine if y is an odd int when x < 0 *yisint=0...yisnotaninteger *yisint=1...yisanoddint *yisint=2...yisanevenint
*/ letmut yisint: i32 = 0; letmut k: i32; letmut j: i32; if hx < 0 { if iy >= 0x43400000 {
yisint = 2; /* even integer y */
} elseif iy >= 0x3ff00000 {
k = (iy >> 20) - 0x3ff; /* exponent */
if ly == 0 { /* special value of y */ if iy == 0x7ff00000 { /* y is +-inf */
returnif ((ix - 0x3ff00000) | (lx as i32)) == 0 { /* (-1)**+-inf is 1 */ 1.0
} elseif ix >= 0x3ff00000 { /* (|x|>1)**+-inf = inf,0 */ if hy >= 0 {
y
} else { 0.0
}
} else { /* (|x|<1)**+-inf = 0,inf */ if hy >= 0 { 0.0
} else {
-y
}
};
}
if iy == 0x3ff00000 { /* y is +-1 */ returnif hy >= 0 { x } else { 1.0 / x };
}
if hy == 0x40000000 { /* y is 2 */ return x * x;
}
if hy == 0x3fe00000 { /* y is 0.5 */ if hx >= 0 { /* x >= +0 */ return sqrt(x);
}
}
}
letmut ax: f64 = fabs(x); if lx == 0 { /* special value of x */ if ix == 0x7ff00000 || ix == 0 || ix == 0x3ff00000 { /* x is +-0,+-inf,+-1 */ letmut z: f64 = ax;
if hy < 0 { /* z = (1/|x|) */
z = 1.0 / z;
}
if hx < 0 { if ((ix - 0x3ff00000) | yisint) == 0 {
z = (z - z) / (z - z); /* (-1)**non-int is NaN */
} elseif yisint == 1 {
z = -z; /* (x<0)**odd = -(|x|**odd) */
}
}
return z;
}
}
letmut s: f64 = 1.0; /* sign of result */ if hx < 0 { if yisint == 0 { /* (x<0)**(non-int) is NaN */ return (x - x) / (x - x);
}
if yisint == 1 { /* (x<0)**(odd int) */
s = -1.0;
}
}
/* |y| is HUGE */ if iy > 0x41e00000 { /* if |y| > 2**31 */ if iy > 0x43f00000 { /* if |y| > 2**64, must o/uflow */ if ix <= 0x3fefffff { returnif hy < 0 { HUGE * HUGE } else { TINY * TINY };
}
if ix >= 0x3ff00000 { returnif hy > 0 { HUGE * HUGE } else { TINY * TINY };
}
}
/* over/underflow if x is not close to one */ if ix < 0x3fefffff { returnif hy < 0 {
s * HUGE * HUGE
} else {
s * TINY * TINY
};
} if ix > 0x3ff00000 { returnif hy > 0 {
s * HUGE * HUGE
} else {
s * TINY * TINY
};
}
/* now |1-x| is TINY <= 2**-20, suffice to compute
log(x) by x-x^2/2+x^3/3-x^4/4 */ let t: f64 = ax - 1.0; /* t has 20 trailing zeros */ let w: f64 = (t * t) * (0.5 - t * (0.3333333333333333333333 - t * 0.25)); let u: f64 = IVLN2_H * t; /* ivln2_h has 21 sig. bits */ let v: f64 = t * IVLN2_L - w * IVLN2;
t1 = with_set_low_word(u + v, 0);
t2 = v - (t1 - u);
} else { // double ss,s2,s_h,s_l,t_h,t_l; letmut n: i32 = 0;
if ix < 0x00100000 { /* take care subnormal number */
ax *= TWO53;
n -= 53;
ix = get_high_word(ax) as i32;
}
n += (ix >> 20) - 0x3ff;
j = ix & 0x000fffff;
/* determine interval */ let k: i32;
ix = j | 0x3ff00000; /* normalize ix */ if j <= 0x3988E { /* |x|<sqrt(3/2) */
k = 0;
} elseif j < 0xBB67A { /* |x|<sqrt(3) */
k = 1;
} else {
k = 0;
n += 1;
ix -= 0x00100000;
}
ax = with_set_high_word(ax, ix as u32);
/* compute ss = s_h+s_l = (x-1)/(x+1) or (x-1.5)/(x+1.5) */ let u: f64 = ax - i!(BP, k as usize); /* bp[0]=1.0, bp[1]=1.5 */ let v: f64 = 1.0 / (ax + i!(BP, k as usize)); let ss: f64 = u * v; let s_h = with_set_low_word(ss, 0);
/* t_h=ax+bp[k] High */ let t_h: f64 = with_set_high_word( 0.0,
((ix as u32 >> 1) | 0x20000000) + 0x00080000 + ((k as u32) << 18),
); let t_l: f64 = ax - (t_h - i!(BP, k as usize)); let s_l: f64 = v * ((u - s_h * t_h) - s_h * t_l);
/* 2/(3log2)*(ss+...) */ let p_h: f64 = with_set_low_word(u + v, 0); let p_l = v - (p_h - u); let z_h: f64 = CP_H * p_h; /* cp_h+cp_l = 2/(3*log2) */ let z_l: f64 = CP_L * p_h + p_l * CP + i!(DP_L, k as usize);
/* log2(ax) = (ss+..)*2/(3*log2) = n + dp_h + z_h + z_l */ let t: f64 = n as f64;
t1 = with_set_low_word(((z_h + z_l) + i!(DP_H, k as usize)) + t, 0);
t2 = z_l - (((t1 - t) - i!(DP_H, k as usize)) - z_h);
}
/* split up y into y1+y2 and compute (y1+y2)*(t1+t2) */ let y1: f64 = with_set_low_word(y, 0); let p_l: f64 = (y - y1) * t1 + y * t2; letmut p_h: f64 = y1 * t1; let z: f64 = p_l + p_h; letmut j: i32 = (z.to_bits() >> 32) as i32; let i: i32 = z.to_bits() as i32; // let (j, i): (i32, i32) = ((z.to_bits() >> 32) as i32, z.to_bits() as i32);
if j >= 0x40900000 { /* z >= 1024 */ if (j - 0x40900000) | i != 0 { /* if z > 1024 */ return s * HUGE * HUGE; /* overflow */
}
if p_l + OVT > z - p_h { return s * HUGE * HUGE; /* overflow */
}
} elseif (j & 0x7fffffff) >= 0x4090cc00 { /* z <= -1075 */ // FIXME: instead of abs(j) use unsigned j
if (((j as u32) - 0xc090cc00) | (i as u32)) != 0 { /* z < -1075 */ return s * TINY * TINY; /* underflow */
}
if p_l <= z - p_h { return s * TINY * TINY; /* underflow */
}
}
/* compute 2**(p_h+p_l) */ let i: i32 = j & (0x7fffffff as i32);
k = (i >> 20) - 0x3ff; letmut n: i32 = 0;
if i > 0x3fe00000 { /* if |z| > 0.5, set n = [z+0.5] */
n = j + (0x00100000 >> (k + 1));
k = ((n & 0x7fffffff) >> 20) - 0x3ff; /* new k for n */ let t: f64 = with_set_high_word(0.0, (n & !(0x000fffff >> k)) as u32);
n = ((n & 0x000fffff) | 0x00100000) >> (20 - k); if j < 0 {
n = -n;
}
p_h -= t;
}
let t: f64 = with_set_low_word(p_l + p_h, 0); let u: f64 = t * LG2_H; let v: f64 = (p_l - (t - p_h)) * LG2 + t * LG2_L; letmut z: f64 = u + v; let w: f64 = v - (z - u); let t: f64 = z * z; let t1: f64 = z - t * (P1 + t * (P2 + t * (P3 + t * (P4 + t * P5)))); let r: f64 = (z * t1) / (t1 - 2.0) - (w + z * w);
z = 1.0 - (r - z);
j = get_high_word(z) as i32;
j += n << 20;
if (j >> 20) <= 0 { /* subnormal output */
z = scalbn(z, n);
} else {
z = with_set_high_word(z, j as u32);
}
#[test] fn one_as_base() {
test_sets_as_exponent(1.0, ALL, 1.0);
}
#[test] fn nan_inputs() { // NAN as the base: // (NAN ^ anything *but 0* should be NAN)
test_sets_as_exponent(NAN, &ALL[2..], NAN);
// NAN as the exponent: // (anything *but 1* ^ NAN should be NAN)
test_sets_as_base(&ALL[..(ALL.len() - 2)], NAN, NAN);
}
#[test] fn infinity_as_base() { // Positive Infinity as the base: // (+Infinity ^ positive anything but 0 and NAN should be +Infinity)
test_sets_as_exponent(INFINITY, &POS[1..], INFINITY);
// (+Infinity ^ negative anything except 0 and NAN should be 0.0)
test_sets_as_exponent(INFINITY, &NEG[1..], 0.0);
// Negative Infinity as the base: // (-Infinity ^ positive odd ints should be -Infinity)
test_sets_as_exponent(NEG_INFINITY, &[POS_ODDS], NEG_INFINITY);
// (-Infinity ^ anything but odd ints should be == -0 ^ (-anything)) // We can lump in pos/neg odd ints here because they don't seem to // cause panics (div by zero) in release mode (I think).
test_sets(ALL, &|v: f64| pow(NEG_INFINITY, v), &|v: f64| pow(-0.0, -v));
}
#[test] fn infinity_as_exponent() { // Positive/Negative base greater than 1: // (pos/neg > 1 ^ Infinity should be Infinity - note this excludes NAN as the base)
test_sets_as_base(&ALL[5..(ALL.len() - 2)], INFINITY, INFINITY);
// (pos/neg > 1 ^ -Infinity should be 0.0)
test_sets_as_base(&ALL[5..ALL.len() - 2], NEG_INFINITY, 0.0);
// Positive/Negative base less than 1: let base_below_one = &[POS_ZERO, NEG_ZERO, NEG_SMALL_FLOATS, POS_SMALL_FLOATS];
// (pos/neg < 1 ^ Infinity should be 0.0 - this also excludes NAN as the base)
test_sets_as_base(base_below_one, INFINITY, 0.0);
// (pos/neg < 1 ^ -Infinity should be Infinity)
test_sets_as_base(base_below_one, NEG_INFINITY, INFINITY);
// Positive/Negative 1 as the base: // (pos/neg 1 ^ Infinity should be 1)
test_sets_as_base(&[NEG_ONE, POS_ONE], INFINITY, 1.0);
// (pos/neg 1 ^ -Infinity should be 1)
test_sets_as_base(&[NEG_ONE, POS_ONE], NEG_INFINITY, 1.0);
}
#[test] fn zero_as_base() { // Positive Zero as the base: // (+0 ^ anything positive but 0 and NAN should be +0)
test_sets_as_exponent(0.0, &POS[1..], 0.0);
// (+0 ^ anything negative but 0 and NAN should be Infinity) // (this should panic because we're dividing by zero)
test_sets_as_exponent(0.0, &NEG[1..], INFINITY);
// Negative Zero as the base: // (-0 ^ anything positive but 0, NAN, and odd ints should be +0)
test_sets_as_exponent(-0.0, &POS[3..], 0.0);
// (-0 ^ anything negative but 0, NAN, and odd ints should be Infinity) // (should panic because of divide by zero)
test_sets_as_exponent(-0.0, &NEG[3..], INFINITY);
// (-0 ^ positive odd ints should be -0)
test_sets_as_exponent(-0.0, &[POS_ODDS], -0.0);
// (-0 ^ negative odd ints should be -Infinity) // (should panic because of divide by zero)
test_sets_as_exponent(-0.0, &[NEG_ODDS], NEG_INFINITY);
}
#[test] fn special_cases() { // One as the exponent: // (anything ^ 1 should be anything - i.e. the base)
test_sets(ALL, &|v: f64| pow(v, 1.0), &|v: f64| v);
// Negative One as the exponent: // (anything ^ -1 should be 1/anything)
test_sets(ALL, &|v: f64| pow(v, -1.0), &|v: f64| 1.0 / v);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.