- Notifications
You must be signed in to change notification settings - Fork 670
/
Copy pathmath.ts
26 lines (24 loc) · 870 Bytes
/
math.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/**
* @fileoverview Various math utility.
* @license Apache-2.0
*/
/** Tests if `x` is a power of two. */
exportfunctionisPowerOf2(x: i32): bool{
returnx!=0&&(x&(x-1))==0;
}
exportfunctionaccuratePow64(x: f64,y: f64): f64{
if(!ASC_TARGET){// ASC_TARGET == JS
// Engines like V8, WebKit and SpiderMonkey uses powi fast path if exponent is integer
// This speculative optimization leads to loose precisions like 10 ** 208 != 1e208
// or/and 10 ** -5 != 1e-5 anymore. For avoid this behaviour we are forcing exponent
// to fractional form and compensate this afterwards.
if(isFinite(y)&&Math.abs(y)>=2&&Math.trunc(y)==y){
if(y<0){
returnMath.pow(x,y+0.5)/Math.pow(x,0.5);
}else{
returnMath.pow(x,y-0.5)*Math.pow(x,0.5);
}
}
}
returnMath.pow(x,y);
}