| 1 |
/* origin: FreeBSD /usr/src/lib/msun/src/e_pow.c */ |
| 2 |
/* |
| 3 |
* ==================================================== |
| 4 |
* Copyright (C) 2004 by Sun Microsystems, Inc. All rights reserved. |
| 5 |
* |
| 6 |
* Permission to use, copy, modify, and distribute this |
| 7 |
* software is freely granted, provided that this notice |
| 8 |
* is preserved. |
| 9 |
* ==================================================== |
| 10 |
*/ |
| 11 |
|
| 12 |
// pow(x,y) return x**y |
| 13 |
// |
| 14 |
// n |
| 15 |
// Method: Let x = 2 * (1+f) |
| 16 |
// 1. Compute and return log2(x) in two pieces: |
| 17 |
// log2(x) = w1 + w2, |
| 18 |
// where w1 has 53-24 = 29 bit trailing zeros. |
| 19 |
// 2. Perform y*log2(x) = n+y' by simulating muti-precision |
| 20 |
// arithmetic, where |y'|<=0.5. |
| 21 |
// 3. Return x**y = 2**n*exp(y'*log2) |
| 22 |
// |
| 23 |
// Special cases: |
| 24 |
// 1. (anything) ** 0 is 1 |
| 25 |
// 2. 1 ** (anything) is 1 |
| 26 |
// 3. (anything except 1) ** NAN is NAN |
| 27 |
// 4. NAN ** (anything except 0) is NAN |
| 28 |
// 5. +-(|x| > 1) ** +INF is +INF |
| 29 |
// 6. +-(|x| > 1) ** -INF is +0 |
| 30 |
// 7. +-(|x| < 1) ** +INF is +0 |
| 31 |
// 8. +-(|x| < 1) ** -INF is +INF |
| 32 |
// 9. -1 ** +-INF is 1 |
| 33 |
// 10. +0 ** (+anything except 0, NAN) is +0 |
| 34 |
// 11. -0 ** (+anything except 0, NAN, odd integer) is +0 |
| 35 |
// 12. +0 ** (-anything except 0, NAN) is +INF, raise divbyzero |
| 36 |
// 13. -0 ** (-anything except 0, NAN, odd integer) is +INF, raise divbyzero |
| 37 |
// 14. -0 ** (+odd integer) is -0 |
| 38 |
// 15. -0 ** (-odd integer) is -INF, raise divbyzero |
| 39 |
// 16. +INF ** (+anything except 0,NAN) is +INF |
| 40 |
// 17. +INF ** (-anything except 0,NAN) is +0 |
| 41 |
// 18. -INF ** (+odd integer) is -INF |
| 42 |
// 19. -INF ** (anything) = -0 ** (-anything), (anything except odd integer) |
| 43 |
// 20. (anything) ** 1 is (anything) |
| 44 |
// 21. (anything) ** -1 is 1/(anything) |
| 45 |
// 22. (-anything) ** (integer) is (-1)**(integer)*(+anything**integer) |
| 46 |
// 23. (-anything except 0 and inf) ** (non-integer) is NAN |
| 47 |
// |
| 48 |
// Accuracy: |
| 49 |
// pow(x,y) returns x**y nearly rounded. In particular |
| 50 |
// pow(integer,integer) |
| 51 |
// always returns the correct integer provided it is |
| 52 |
// representable. |
| 53 |
// |
| 54 |
// Constants : |
| 55 |
// The hexadecimal values are the intended ones for the following |
| 56 |
// constants. The decimal values may be used, provided that the |
| 57 |
// compiler will convert from decimal to binary accurately enough |
| 58 |
// to produce the hexadecimal values shown. |
| 59 |
// |
| 60 |
use super::{fabs, get_high_word, scalbn, sqrt, with_set_high_word, with_set_low_word}; |
| 61 |
|
| 62 |
const BP: [f64; 2] = [1.0, 1.5]; |
| 63 |
const DP_H: [f64; 2] = [0.0, 5.84962487220764160156e-01]; /* 0x3fe2b803_40000000 */ |
| 64 |
const DP_L: [f64; 2] = [0.0, 1.35003920212974897128e-08]; /* 0x3E4CFDEB, 0x43CFD006 */ |
| 65 |
const TWO53: f64 = 9007199254740992.0; /* 0x43400000_00000000 */ |
| 66 |
const HUGE: f64 = 1.0e300; |
| 67 |
const TINY: f64 = 1.0e-300; |
| 68 |
|
| 69 |
// poly coefs for (3/2)*(log(x)-2s-2/3*s**3: |
| 70 |
const L1: f64 = 5.99999999999994648725e-01; /* 0x3fe33333_33333303 */ |
| 71 |
const L2: f64 = 4.28571428578550184252e-01; /* 0x3fdb6db6_db6fabff */ |
| 72 |
const L3: f64 = 3.33333329818377432918e-01; /* 0x3fd55555_518f264d */ |
| 73 |
const L4: f64 = 2.72728123808534006489e-01; /* 0x3fd17460_a91d4101 */ |
| 74 |
const L5: f64 = 2.30660745775561754067e-01; /* 0x3fcd864a_93c9db65 */ |
| 75 |
const L6: f64 = 2.06975017800338417784e-01; /* 0x3fca7e28_4a454eef */ |
| 76 |
const P1: f64 = 1.66666666666666019037e-01; /* 0x3fc55555_5555553e */ |
| 77 |
const P2: f64 = -2.77777777770155933842e-03; /* 0xbf66c16c_16bebd93 */ |
| 78 |
const P3: f64 = 6.61375632143793436117e-05; /* 0x3f11566a_af25de2c */ |
| 79 |
const P4: f64 = -1.65339022054652515390e-06; /* 0xbebbbd41_c5d26bf1 */ |
| 80 |
const P5: f64 = 4.13813679705723846039e-08; /* 0x3e663769_72bea4d0 */ |
| 81 |
const LG2: f64 = 6.93147180559945286227e-01; /* 0x3fe62e42_fefa39ef */ |
| 82 |
const LG2_H: f64 = 6.93147182464599609375e-01; /* 0x3fe62e43_00000000 */ |
| 83 |
const LG2_L: f64 = -1.90465429995776804525e-09; /* 0xbe205c61_0ca86c39 */ |
| 84 |
const OVT: f64 = 8.0085662595372944372e-017; /* -(1024-log2(ovfl+.5ulp)) */ |
| 85 |
const CP: f64 = 9.61796693925975554329e-01; /* 0x3feec709_dc3a03fd =2/(3ln2) */ |
| 86 |
const CP_H: f64 = 9.61796700954437255859e-01; /* 0x3feec709_e0000000 =(float)cp */ |
| 87 |
const CP_L: f64 = -7.02846165095275826516e-09; /* 0xbe3e2fe0_145b01f5 =tail of cp_h*/ |
| 88 |
const IVLN2: f64 = 1.44269504088896338700e+00; /* 0x3ff71547_652b82fe =1/ln2 */ |
| 89 |
const IVLN2_H: f64 = 1.44269502162933349609e+00; /* 0x3ff71547_60000000 =24b 1/ln2*/ |
| 90 |
const IVLN2_L: f64 = 1.92596299112661746887e-08; /* 0x3e54ae0b_f85ddf44 =1/ln2 tail*/ |
| 91 |
|
| 92 |
#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)] |
| 93 |
pub fn pow(x: f64, y: f64) -> f64 { |
| 94 |
let t1: f64; |
| 95 |
let t2: f64; |
| 96 |
|
| 97 |
let (hx, lx): (i32, u32) = ((x.to_bits() >> 32) as i32, x.to_bits() as u32); |
| 98 |
let (hy, ly): (i32, u32) = ((y.to_bits() >> 32) as i32, y.to_bits() as u32); |
| 99 |
|
| 100 |
let mut ix: i32 = (hx & 0x7fffffff) as i32; |
| 101 |
let iy: i32 = (hy & 0x7fffffff) as i32; |
| 102 |
|
| 103 |
/* x**0 = 1, even if x is NaN */ |
| 104 |
if ((iy as u32) | ly) == 0 { |
| 105 |
return 1.0; |
| 106 |
} |
| 107 |
|
| 108 |
/* 1**y = 1, even if y is NaN */ |
| 109 |
if hx == 0x3ff00000 && lx == 0 { |
| 110 |
return 1.0; |
| 111 |
} |
| 112 |
|
| 113 |
/* NaN if either arg is NaN */ |
| 114 |
if ix > 0x7ff00000 |
| 115 |
|| (ix == 0x7ff00000 && lx != 0) |
| 116 |
|| iy > 0x7ff00000 |
| 117 |
|| (iy == 0x7ff00000 && ly != 0) |
| 118 |
{ |
| 119 |
return x + y; |
| 120 |
} |
| 121 |
|
| 122 |
/* determine if y is an odd int when x < 0 |
| 123 |
* yisint = 0 ... y is not an integer |
| 124 |
* yisint = 1 ... y is an odd int |
| 125 |
* yisint = 2 ... y is an even int |
| 126 |
*/ |
| 127 |
let mut yisint: i32 = 0; |
| 128 |
let mut k: i32; |
| 129 |
let mut j: i32; |
| 130 |
if hx < 0 { |
| 131 |
if iy >= 0x43400000 { |
| 132 |
yisint = 2; /* even integer y */ |
| 133 |
} else if iy >= 0x3ff00000 { |
| 134 |
k = (iy >> 20) - 0x3ff; /* exponent */ |
| 135 |
|
| 136 |
if k > 20 { |
| 137 |
j = (ly >> (52 - k)) as i32; |
| 138 |
|
| 139 |
if (j << (52 - k)) == (ly as i32) { |
| 140 |
yisint = 2 - (j & 1); |
| 141 |
} |
| 142 |
} else if ly == 0 { |
| 143 |
j = iy >> (20 - k); |
| 144 |
|
| 145 |
if (j << (20 - k)) == iy { |
| 146 |
yisint = 2 - (j & 1); |
| 147 |
} |
| 148 |
} |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
if ly == 0 { |
| 153 |
/* special value of y */ |
| 154 |
if iy == 0x7ff00000 { |
| 155 |
/* y is +-inf */ |
| 156 |
|
| 157 |
return if ((ix - 0x3ff00000) | (lx as i32)) == 0 { |
| 158 |
/* (-1)**+-inf is 1 */ |
| 159 |
1.0 |
| 160 |
} else if ix >= 0x3ff00000 { |
| 161 |
/* (|x|>1)**+-inf = inf,0 */ |
| 162 |
if hy >= 0 { |
| 163 |
y |
| 164 |
} else { |
| 165 |
0.0 |
| 166 |
} |
| 167 |
} else { |
| 168 |
/* (|x|<1)**+-inf = 0,inf */ |
| 169 |
if hy >= 0 { |
| 170 |
0.0 |
| 171 |
} else { |
| 172 |
-y |
| 173 |
} |
| 174 |
}; |
| 175 |
} |
| 176 |
|
| 177 |
if iy == 0x3ff00000 { |
| 178 |
/* y is +-1 */ |
| 179 |
return if hy >= 0 { x } else { 1.0 / x }; |
| 180 |
} |
| 181 |
|
| 182 |
if hy == 0x40000000 { |
| 183 |
/* y is 2 */ |
| 184 |
return x * x; |
| 185 |
} |
| 186 |
|
| 187 |
if hy == 0x3fe00000 { |
| 188 |
/* y is 0.5 */ |
| 189 |
if hx >= 0 { |
| 190 |
/* x >= +0 */ |
| 191 |
return sqrt(x); |
| 192 |
} |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
let mut ax: f64 = fabs(x); |
| 197 |
if lx == 0 { |
| 198 |
/* special value of x */ |
| 199 |
if ix == 0x7ff00000 || ix == 0 || ix == 0x3ff00000 { |
| 200 |
/* x is +-0,+-inf,+-1 */ |
| 201 |
let mut z: f64 = ax; |
| 202 |
|
| 203 |
if hy < 0 { |
| 204 |
/* z = (1/|x|) */ |
| 205 |
z = 1.0 / z; |
| 206 |
} |
| 207 |
|
| 208 |
if hx < 0 { |
| 209 |
if ((ix - 0x3ff00000) | yisint) == 0 { |
| 210 |
z = (z - z) / (z - z); /* (-1)**non-int is NaN */ |
| 211 |
} else if yisint == 1 { |
| 212 |
z = -z; /* (x<0)**odd = -(|x|**odd) */ |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
return z; |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
let mut s: f64 = 1.0; /* sign of result */ |
| 221 |
if hx < 0 { |
| 222 |
if yisint == 0 { |
| 223 |
/* (x<0)**(non-int) is NaN */ |
| 224 |
return (x - x) / (x - x); |
| 225 |
} |
| 226 |
|
| 227 |
if yisint == 1 { |
| 228 |
/* (x<0)**(odd int) */ |
| 229 |
s = -1.0; |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
/* |y| is HUGE */ |
| 234 |
if iy > 0x41e00000 { |
| 235 |
/* if |y| > 2**31 */ |
| 236 |
if iy > 0x43f00000 { |
| 237 |
/* if |y| > 2**64, must o/uflow */ |
| 238 |
if ix <= 0x3fefffff { |
| 239 |
return if hy < 0 { HUGE * HUGE } else { TINY * TINY }; |
| 240 |
} |
| 241 |
|
| 242 |
if ix >= 0x3ff00000 { |
| 243 |
return if hy > 0 { HUGE * HUGE } else { TINY * TINY }; |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
/* over/underflow if x is not close to one */ |
| 248 |
if ix < 0x3fefffff { |
| 249 |
return if hy < 0 { |
| 250 |
s * HUGE * HUGE |
| 251 |
} else { |
| 252 |
s * TINY * TINY |
| 253 |
}; |
| 254 |
} |
| 255 |
if ix > 0x3ff00000 { |
| 256 |
return if hy > 0 { |
| 257 |
s * HUGE * HUGE |
| 258 |
} else { |
| 259 |
s * TINY * TINY |
| 260 |
}; |
| 261 |
} |
| 262 |
|
| 263 |
/* now |1-x| is TINY <= 2**-20, suffice to compute |
| 264 |
log(x) by x-x^2/2+x^3/3-x^4/4 */ |
| 265 |
let t: f64 = ax - 1.0; /* t has 20 trailing zeros */ |
| 266 |
let w: f64 = (t * t) * (0.5 - t * (0.3333333333333333333333 - t * 0.25)); |
| 267 |
let u: f64 = IVLN2_H * t; /* ivln2_h has 21 sig. bits */ |
| 268 |
let v: f64 = t * IVLN2_L - w * IVLN2; |
| 269 |
t1 = with_set_low_word(u + v, 0); |
| 270 |
t2 = v - (t1 - u); |
| 271 |
} else { |
| 272 |
// double ss,s2,s_h,s_l,t_h,t_l; |
| 273 |
let mut n: i32 = 0; |
| 274 |
|
| 275 |
if ix < 0x00100000 { |
| 276 |
/* take care subnormal number */ |
| 277 |
ax *= TWO53; |
| 278 |
n -= 53; |
| 279 |
ix = get_high_word(ax) as i32; |
| 280 |
} |
| 281 |
|
| 282 |
n += (ix >> 20) - 0x3ff; |
| 283 |
j = ix & 0x000fffff; |
| 284 |
|
| 285 |
/* determine interval */ |
| 286 |
let k: i32; |
| 287 |
ix = j | 0x3ff00000; /* normalize ix */ |
| 288 |
if j <= 0x3988E { |
| 289 |
/* |x|<sqrt(3/2) */ |
| 290 |
k = 0; |
| 291 |
} else if j < 0xBB67A { |
| 292 |
/* |x|<sqrt(3) */ |
| 293 |
k = 1; |
| 294 |
} else { |
| 295 |
k = 0; |
| 296 |
n += 1; |
| 297 |
ix -= 0x00100000; |
| 298 |
} |
| 299 |
ax = with_set_high_word(ax, ix as u32); |
| 300 |
|
| 301 |
/* compute ss = s_h+s_l = (x-1)/(x+1) or (x-1.5)/(x+1.5) */ |
| 302 |
let u: f64 = ax - i!(BP, k as usize); /* bp[0]=1.0, bp[1]=1.5 */ |
| 303 |
let v: f64 = 1.0 / (ax + i!(BP, k as usize)); |
| 304 |
let ss: f64 = u * v; |
| 305 |
let s_h = with_set_low_word(ss, 0); |
| 306 |
|
| 307 |
/* t_h=ax+bp[k] High */ |
| 308 |
let t_h: f64 = with_set_high_word( |
| 309 |
0.0, |
| 310 |
((ix as u32 >> 1) | 0x20000000) + 0x00080000 + ((k as u32) << 18), |
| 311 |
); |
| 312 |
let t_l: f64 = ax - (t_h - i!(BP, k as usize)); |
| 313 |
let s_l: f64 = v * ((u - s_h * t_h) - s_h * t_l); |
| 314 |
|
| 315 |
/* compute log(ax) */ |
| 316 |
let s2: f64 = ss * ss; |
| 317 |
let mut r: f64 = s2 * s2 * (L1 + s2 * (L2 + s2 * (L3 + s2 * (L4 + s2 * (L5 + s2 * L6))))); |
| 318 |
r += s_l * (s_h + ss); |
| 319 |
let s2: f64 = s_h * s_h; |
| 320 |
let t_h: f64 = with_set_low_word(3.0 + s2 + r, 0); |
| 321 |
let t_l: f64 = r - ((t_h - 3.0) - s2); |
| 322 |
|
| 323 |
/* u+v = ss*(1+...) */ |
| 324 |
let u: f64 = s_h * t_h; |
| 325 |
let v: f64 = s_l * t_h + t_l * ss; |
| 326 |
|
| 327 |
/* 2/(3log2)*(ss+...) */ |
| 328 |
let p_h: f64 = with_set_low_word(u + v, 0); |
| 329 |
let p_l = v - (p_h - u); |
| 330 |
let z_h: f64 = CP_H * p_h; /* cp_h+cp_l = 2/(3*log2) */ |
| 331 |
let z_l: f64 = CP_L * p_h + p_l * CP + i!(DP_L, k as usize); |
| 332 |
|
| 333 |
/* log2(ax) = (ss+..)*2/(3*log2) = n + dp_h + z_h + z_l */ |
| 334 |
let t: f64 = n as f64; |
| 335 |
t1 = with_set_low_word(((z_h + z_l) + i!(DP_H, k as usize)) + t, 0); |
| 336 |
t2 = z_l - (((t1 - t) - i!(DP_H, k as usize)) - z_h); |
| 337 |
} |
| 338 |
|
| 339 |
/* split up y into y1+y2 and compute (y1+y2)*(t1+t2) */ |
| 340 |
let y1: f64 = with_set_low_word(y, 0); |
| 341 |
let p_l: f64 = (y - y1) * t1 + y * t2; |
| 342 |
let mut p_h: f64 = y1 * t1; |
| 343 |
let z: f64 = p_l + p_h; |
| 344 |
let mut j: i32 = (z.to_bits() >> 32) as i32; |
| 345 |
let i: i32 = z.to_bits() as i32; |
| 346 |
// let (j, i): (i32, i32) = ((z.to_bits() >> 32) as i32, z.to_bits() as i32); |
| 347 |
|
| 348 |
if j >= 0x40900000 { |
| 349 |
/* z >= 1024 */ |
| 350 |
if (j - 0x40900000) | i != 0 { |
| 351 |
/* if z > 1024 */ |
| 352 |
return s * HUGE * HUGE; /* overflow */ |
| 353 |
} |
| 354 |
|
| 355 |
if p_l + OVT > z - p_h { |
| 356 |
return s * HUGE * HUGE; /* overflow */ |
| 357 |
} |
| 358 |
} else if (j & 0x7fffffff) >= 0x4090cc00 { |
| 359 |
/* z <= -1075 */ |
| 360 |
// FIXME: instead of abs(j) use unsigned j |
| 361 |
|
| 362 |
if (((j as u32) - 0xc090cc00) | (i as u32)) != 0 { |
| 363 |
/* z < -1075 */ |
| 364 |
return s * TINY * TINY; /* underflow */ |
| 365 |
} |
| 366 |
|
| 367 |
if p_l <= z - p_h { |
| 368 |
return s * TINY * TINY; /* underflow */ |
| 369 |
} |
| 370 |
} |
| 371 |
|
| 372 |
/* compute 2**(p_h+p_l) */ |
| 373 |
let i: i32 = j & (0x7fffffff as i32); |
| 374 |
k = (i >> 20) - 0x3ff; |
| 375 |
let mut n: i32 = 0; |
| 376 |
|
| 377 |
if i > 0x3fe00000 { |
| 378 |
/* if |z| > 0.5, set n = [z+0.5] */ |
| 379 |
n = j + (0x00100000 >> (k + 1)); |
| 380 |
k = ((n & 0x7fffffff) >> 20) - 0x3ff; /* new k for n */ |
| 381 |
let t: f64 = with_set_high_word(0.0, (n & !(0x000fffff >> k)) as u32); |
| 382 |
n = ((n & 0x000fffff) | 0x00100000) >> (20 - k); |
| 383 |
if j < 0 { |
| 384 |
n = -n; |
| 385 |
} |
| 386 |
p_h -= t; |
| 387 |
} |
| 388 |
|
| 389 |
let t: f64 = with_set_low_word(p_l + p_h, 0); |
| 390 |
let u: f64 = t * LG2_H; |
| 391 |
let v: f64 = (p_l - (t - p_h)) * LG2 + t * LG2_L; |
| 392 |
let mut z: f64 = u + v; |
| 393 |
let w: f64 = v - (z - u); |
| 394 |
let t: f64 = z * z; |
| 395 |
let t1: f64 = z - t * (P1 + t * (P2 + t * (P3 + t * (P4 + t * P5)))); |
| 396 |
let r: f64 = (z * t1) / (t1 - 2.0) - (w + z * w); |
| 397 |
z = 1.0 - (r - z); |
| 398 |
j = get_high_word(z) as i32; |
| 399 |
j += n << 20; |
| 400 |
|
| 401 |
if (j >> 20) <= 0 { |
| 402 |
/* subnormal output */ |
| 403 |
z = scalbn(z, n); |
| 404 |
} else { |
| 405 |
z = with_set_high_word(z, j as u32); |
| 406 |
} |
| 407 |
|
| 408 |
s * z |
| 409 |
} |
| 410 |
|
| 411 |
#[cfg(test)] |
| 412 |
mod tests { |
| 413 |
extern crate core; |
| 414 |
|
| 415 |
use self::core::f64::consts::{E, PI}; |
| 416 |
use self::core::f64::{EPSILON, INFINITY, MAX, MIN, MIN_POSITIVE, NAN, NEG_INFINITY}; |
| 417 |
use super::pow; |
| 418 |
|
| 419 |
const POS_ZERO: &[f64] = &[0.0]; |
| 420 |
const NEG_ZERO: &[f64] = &[-0.0]; |
| 421 |
const POS_ONE: &[f64] = &[1.0]; |
| 422 |
const NEG_ONE: &[f64] = &[-1.0]; |
| 423 |
const POS_FLOATS: &[f64] = &[99.0 / 70.0, E, PI]; |
| 424 |
const NEG_FLOATS: &[f64] = &[-99.0 / 70.0, -E, -PI]; |
| 425 |
const POS_SMALL_FLOATS: &[f64] = &[(1.0 / 2.0), MIN_POSITIVE, EPSILON]; |
| 426 |
const NEG_SMALL_FLOATS: &[f64] = &[-(1.0 / 2.0), -MIN_POSITIVE, -EPSILON]; |
| 427 |
const POS_EVENS: &[f64] = &[2.0, 6.0, 8.0, 10.0, 22.0, 100.0, MAX]; |
| 428 |
const NEG_EVENS: &[f64] = &[MIN, -100.0, -22.0, -10.0, -8.0, -6.0, -2.0]; |
| 429 |
const POS_ODDS: &[f64] = &[3.0, 7.0]; |
| 430 |
const NEG_ODDS: &[f64] = &[-7.0, -3.0]; |
| 431 |
const NANS: &[f64] = &[NAN]; |
| 432 |
const POS_INF: &[f64] = &[INFINITY]; |
| 433 |
const NEG_INF: &[f64] = &[NEG_INFINITY]; |
| 434 |
|
| 435 |
const ALL: &[&[f64]] = &[ |
| 436 |
POS_ZERO, |
| 437 |
NEG_ZERO, |
| 438 |
NANS, |
| 439 |
NEG_SMALL_FLOATS, |
| 440 |
POS_SMALL_FLOATS, |
| 441 |
NEG_FLOATS, |
| 442 |
POS_FLOATS, |
| 443 |
NEG_EVENS, |
| 444 |
POS_EVENS, |
| 445 |
NEG_ODDS, |
| 446 |
POS_ODDS, |
| 447 |
NEG_INF, |
| 448 |
POS_INF, |
| 449 |
NEG_ONE, |
| 450 |
POS_ONE, |
| 451 |
]; |
| 452 |
const POS: &[&[f64]] = &[POS_ZERO, POS_ODDS, POS_ONE, POS_FLOATS, POS_EVENS, POS_INF]; |
| 453 |
const NEG: &[&[f64]] = &[NEG_ZERO, NEG_ODDS, NEG_ONE, NEG_FLOATS, NEG_EVENS, NEG_INF]; |
| 454 |
|
| 455 |
fn pow_test(base: f64, exponent: f64, expected: f64) { |
| 456 |
let res = pow(base, exponent); |
| 457 |
assert!( |
| 458 |
if expected.is_nan() { |
| 459 |
res.is_nan() |
| 460 |
} else { |
| 461 |
pow(base, exponent) == expected |
| 462 |
}, |
| 463 |
"{} ** {} was {} instead of {}", |
| 464 |
base, |
| 465 |
exponent, |
| 466 |
res, |
| 467 |
expected |
| 468 |
); |
| 469 |
} |
| 470 |
|
| 471 |
fn test_sets_as_base(sets: &[&[f64]], exponent: f64, expected: f64) { |
| 472 |
sets.iter() |
| 473 |
.for_each(|s| s.iter().for_each(|val| pow_test(*val, exponent, expected))); |
| 474 |
} |
| 475 |
|
| 476 |
fn test_sets_as_exponent(base: f64, sets: &[&[f64]], expected: f64) { |
| 477 |
sets.iter() |
| 478 |
.for_each(|s| s.iter().for_each(|val| pow_test(base, *val, expected))); |
| 479 |
} |
| 480 |
|
| 481 |
fn test_sets(sets: &[&[f64]], computed: &dyn Fn(f64) -> f64, expected: &dyn Fn(f64) -> f64) { |
| 482 |
sets.iter().for_each(|s| { |
| 483 |
s.iter().for_each(|val| { |
| 484 |
let exp = expected(*val); |
| 485 |
let res = computed(*val); |
| 486 |
|
| 487 |
#[cfg(all(target_arch = "x86", not(target_feature = "sse2")))] |
| 488 |
let exp = force_eval!(exp); |
| 489 |
#[cfg(all(target_arch = "x86", not(target_feature = "sse2")))] |
| 490 |
let res = force_eval!(res); |
| 491 |
assert!( |
| 492 |
if exp.is_nan() { |
| 493 |
res.is_nan() |
| 494 |
} else { |
| 495 |
exp == res |
| 496 |
}, |
| 497 |
"test for {} was {} instead of {}", |
| 498 |
val, |
| 499 |
res, |
| 500 |
exp |
| 501 |
); |
| 502 |
}) |
| 503 |
}); |
| 504 |
} |
| 505 |
|
| 506 |
#[test] |
| 507 |
fn zero_as_exponent() { |
| 508 |
test_sets_as_base(ALL, 0.0, 1.0); |
| 509 |
test_sets_as_base(ALL, -0.0, 1.0); |
| 510 |
} |
| 511 |
|
| 512 |
#[test] |
| 513 |
fn one_as_base() { |
| 514 |
test_sets_as_exponent(1.0, ALL, 1.0); |
| 515 |
} |
| 516 |
|
| 517 |
#[test] |
| 518 |
fn nan_inputs() { |
| 519 |
// NAN as the base: |
| 520 |
// (NAN ^ anything *but 0* should be NAN) |
| 521 |
test_sets_as_exponent(NAN, &ALL[2..], NAN); |
| 522 |
|
| 523 |
// NAN as the exponent: |
| 524 |
// (anything *but 1* ^ NAN should be NAN) |
| 525 |
test_sets_as_base(&ALL[..(ALL.len() - 2)], NAN, NAN); |
| 526 |
} |
| 527 |
|
| 528 |
#[test] |
| 529 |
fn infinity_as_base() { |
| 530 |
// Positive Infinity as the base: |
| 531 |
// (+Infinity ^ positive anything but 0 and NAN should be +Infinity) |
| 532 |
test_sets_as_exponent(INFINITY, &POS[1..], INFINITY); |
| 533 |
|
| 534 |
// (+Infinity ^ negative anything except 0 and NAN should be 0.0) |
| 535 |
test_sets_as_exponent(INFINITY, &NEG[1..], 0.0); |
| 536 |
|
| 537 |
// Negative Infinity as the base: |
| 538 |
// (-Infinity ^ positive odd ints should be -Infinity) |
| 539 |
test_sets_as_exponent(NEG_INFINITY, &[POS_ODDS], NEG_INFINITY); |
| 540 |
|
| 541 |
// (-Infinity ^ anything but odd ints should be == -0 ^ (-anything)) |
| 542 |
// We can lump in pos/neg odd ints here because they don't seem to |
| 543 |
// cause panics (div by zero) in release mode (I think). |
| 544 |
test_sets(ALL, &|v: f64| pow(NEG_INFINITY, v), &|v: f64| pow(-0.0, -v)); |
| 545 |
} |
| 546 |
|
| 547 |
#[test] |
| 548 |
fn infinity_as_exponent() { |
| 549 |
// Positive/Negative base greater than 1: |
| 550 |
// (pos/neg > 1 ^ Infinity should be Infinity - note this excludes NAN as the base) |
| 551 |
test_sets_as_base(&ALL[5..(ALL.len() - 2)], INFINITY, INFINITY); |
| 552 |
|
| 553 |
// (pos/neg > 1 ^ -Infinity should be 0.0) |
| 554 |
test_sets_as_base(&ALL[5..ALL.len() - 2], NEG_INFINITY, 0.0); |
| 555 |
|
| 556 |
// Positive/Negative base less than 1: |
| 557 |
let base_below_one = &[POS_ZERO, NEG_ZERO, NEG_SMALL_FLOATS, POS_SMALL_FLOATS]; |
| 558 |
|
| 559 |
// (pos/neg < 1 ^ Infinity should be 0.0 - this also excludes NAN as the base) |
| 560 |
test_sets_as_base(base_below_one, INFINITY, 0.0); |
| 561 |
|
| 562 |
// (pos/neg < 1 ^ -Infinity should be Infinity) |
| 563 |
test_sets_as_base(base_below_one, NEG_INFINITY, INFINITY); |
| 564 |
|
| 565 |
// Positive/Negative 1 as the base: |
| 566 |
// (pos/neg 1 ^ Infinity should be 1) |
| 567 |
test_sets_as_base(&[NEG_ONE, POS_ONE], INFINITY, 1.0); |
| 568 |
|
| 569 |
// (pos/neg 1 ^ -Infinity should be 1) |
| 570 |
test_sets_as_base(&[NEG_ONE, POS_ONE], NEG_INFINITY, 1.0); |
| 571 |
} |
| 572 |
|
| 573 |
#[test] |
| 574 |
fn zero_as_base() { |
| 575 |
// Positive Zero as the base: |
| 576 |
// (+0 ^ anything positive but 0 and NAN should be +0) |
| 577 |
test_sets_as_exponent(0.0, &POS[1..], 0.0); |
| 578 |
|
| 579 |
// (+0 ^ anything negative but 0 and NAN should be Infinity) |
| 580 |
// (this should panic because we're dividing by zero) |
| 581 |
test_sets_as_exponent(0.0, &NEG[1..], INFINITY); |
| 582 |
|
| 583 |
// Negative Zero as the base: |
| 584 |
// (-0 ^ anything positive but 0, NAN, and odd ints should be +0) |
| 585 |
test_sets_as_exponent(-0.0, &POS[3..], 0.0); |
| 586 |
|
| 587 |
// (-0 ^ anything negative but 0, NAN, and odd ints should be Infinity) |
| 588 |
// (should panic because of divide by zero) |
| 589 |
test_sets_as_exponent(-0.0, &NEG[3..], INFINITY); |
| 590 |
|
| 591 |
// (-0 ^ positive odd ints should be -0) |
| 592 |
test_sets_as_exponent(-0.0, &[POS_ODDS], -0.0); |
| 593 |
|
| 594 |
// (-0 ^ negative odd ints should be -Infinity) |
| 595 |
// (should panic because of divide by zero) |
| 596 |
test_sets_as_exponent(-0.0, &[NEG_ODDS], NEG_INFINITY); |
| 597 |
} |
| 598 |
|
| 599 |
#[test] |
| 600 |
fn special_cases() { |
| 601 |
// One as the exponent: |
| 602 |
// (anything ^ 1 should be anything - i.e. the base) |
| 603 |
test_sets(ALL, &|v: f64| pow(v, 1.0), &|v: f64| v); |
| 604 |
|
| 605 |
// Negative One as the exponent: |
| 606 |
// (anything ^ -1 should be 1/anything) |
| 607 |
test_sets(ALL, &|v: f64| pow(v, -1.0), &|v: f64| 1.0 / v); |
| 608 |
|
| 609 |
// Factoring -1 out: |
| 610 |
// (negative anything ^ integer should be (-1 ^ integer) * (positive anything ^ integer)) |
| 611 |
(&[POS_ZERO, NEG_ZERO, POS_ONE, NEG_ONE, POS_EVENS, NEG_EVENS]) |
| 612 |
.iter() |
| 613 |
.for_each(|int_set| { |
| 614 |
int_set.iter().for_each(|int| { |
| 615 |
test_sets(ALL, &|v: f64| pow(-v, *int), &|v: f64| { |
| 616 |
pow(-1.0, *int) * pow(v, *int) |
| 617 |
}); |
| 618 |
}) |
| 619 |
}); |
| 620 |
|
| 621 |
// Negative base (imaginary results): |
| 622 |
// (-anything except 0 and Infinity ^ non-integer should be NAN) |
| 623 |
(&NEG[1..(NEG.len() - 1)]).iter().for_each(|set| { |
| 624 |
set.iter().for_each(|val| { |
| 625 |
test_sets(&ALL[3..7], &|v: f64| pow(*val, v), &|_| NAN); |
| 626 |
}) |
| 627 |
}); |
| 628 |
} |
| 629 |
|
| 630 |
#[test] |
| 631 |
fn normal_cases() { |
| 632 |
assert_eq!(pow(2.0, 20.0), (1 << 20) as f64); |
| 633 |
assert_eq!(pow(-1.0, 9.0), -1.0); |
| 634 |
assert!(pow(-1.0, 2.2).is_nan()); |
| 635 |
assert!(pow(-1.0, -1.14).is_nan()); |
| 636 |
} |
| 637 |
} |