@@ -16,7 +16,10 @@ const _pow10Cache: Map<number, bigint> = new Map();
1616export function pow10 ( n : number ) : bigint {
1717 if ( n <= 100 ) {
1818 let v = _pow10Cache . get ( n ) ;
19- if ( v === undefined ) { v = 10n ** BigInt ( n ) ; _pow10Cache . set ( n , v ) ; }
19+ if ( v === undefined ) {
20+ v = 10n ** BigInt ( n ) ;
21+ _pow10Cache . set ( n , v ) ;
22+ }
2023 return v ;
2124 }
2225 return 10n ** BigInt ( n ) ;
@@ -152,7 +155,10 @@ export function bigintDigits(n: bigint): number {
152155 while ( tmp >> BigInt ( high ) > 0n ) high *= 2 ;
153156 // Binary search within [0, high]
154157 for ( let shift = high >> 1 ; shift >= 1 ; shift >>= 1 ) {
155- if ( tmp >> BigInt ( shift ) > 0n ) { bits += shift ; tmp >>= BigInt ( shift ) ; }
158+ if ( tmp >> BigInt ( shift ) > 0n ) {
159+ bits += shift ;
160+ tmp >>= BigInt ( shift ) ;
161+ }
156162 }
157163 bits += 1 ;
158164 const approx = Math . ceil ( bits * 0.30102999566398 ) ;
@@ -193,14 +199,14 @@ export function fpexp(x: bigint, scale: bigint): bigint {
193199 sum += term ;
194200
195201 for ( let n = 2 ; ; n ++ ) {
196- term = term * r / ( BigInt ( n ) * scale ) ;
202+ term = ( term * r ) / ( BigInt ( n ) * scale ) ;
197203 if ( bigintAbs ( term ) === 0n ) break ;
198204 sum += term ;
199205 }
200206
201207 // Squaring phase: exp(x/scale) = exp(r/scale)^(2^k)
202208 for ( let i = 0 ; i < k ; i ++ ) {
203- sum = sum * sum / scale ;
209+ sum = ( sum * sum ) / scale ;
204210 }
205211
206212 return sum ;
@@ -229,7 +235,12 @@ export function fpln(x: bigint, scale: bigint): bigint {
229235 let target = x ; // the value we compute ln of (may be reduced)
230236 let k = 0 ; // number of sqrt halvings applied
231237
232- if ( Number . isFinite ( xNum ) && Number . isFinite ( scaleNum ) && xNum > 0 && scaleNum > 0 ) {
238+ if (
239+ Number . isFinite ( xNum ) &&
240+ Number . isFinite ( scaleNum ) &&
241+ xNum > 0 &&
242+ scaleNum > 0
243+ ) {
233244 const ratio = xNum / scaleNum ;
234245 if ( Number . isFinite ( ratio ) && ratio > 0 ) {
235246 const approx = Math . log ( ratio ) ;
@@ -288,8 +299,13 @@ export function fpln(x: bigint, scale: bigint): bigint {
288299 const absDelta = bigintAbs ( yn - y ) ;
289300 if ( absDelta <= 1n ) break ;
290301 // Detect limit cycle: both deltas are small and convergence stalled
291- if ( absDelta < 100000n && prevAbsDelta > 0n && prevAbsDelta < 100000n
292- && absDelta * 4n >= prevAbsDelta ) break ;
302+ if (
303+ absDelta < 100000n &&
304+ prevAbsDelta > 0n &&
305+ prevAbsDelta < 100000n &&
306+ absDelta * 4n >= prevAbsDelta
307+ )
308+ break ;
293309 prevAbsDelta = absDelta ;
294310 y = yn ;
295311 }
@@ -341,7 +357,8 @@ export const PI_DIGITS =
341357let _fppiCache : { scale : bigint ; value : bigint } | null = null ;
342358
343359function fppi ( scale : bigint ) : bigint {
344- if ( _fppiCache !== null && _fppiCache . scale === scale ) return _fppiCache . value ;
360+ if ( _fppiCache !== null && _fppiCache . scale === scale )
361+ return _fppiCache . value ;
345362
346363 // Compute PI * scale using the shared PI_DIGITS constant.
347364 // scale = 10^p, so we need ~p+10 digits of PI.
@@ -475,10 +492,10 @@ export function fpsincos(x: bigint, scale: bigint): [bigint, bigint] {
475492 for ( let n = 2 ; ; n += 2 ) {
476493 // cos term: cosTerm = cosTerm * (-r²) / (n*(n-1)*scale²)
477494 // But we compute sign explicitly
478- cosTerm = cosTerm * r2 / ( BigInt ( n ) * BigInt ( n - 1 ) * scale2 ) ;
495+ cosTerm = ( cosTerm * r2 ) / ( BigInt ( n ) * BigInt ( n - 1 ) * scale2 ) ;
479496 if ( cosTerm === 0n ) {
480497 // Also check sin at next step
481- sinTerm = sinTerm * r2 / ( BigInt ( n + 1 ) * BigInt ( n ) * scale2 ) ;
498+ sinTerm = ( sinTerm * r2 ) / ( BigInt ( n + 1 ) * BigInt ( n ) * scale2 ) ;
482499 if ( sinTerm !== 0n ) {
483500 if ( n % 4 === 2 ) {
484501 cosVal -= cosTerm ;
@@ -492,7 +509,7 @@ export function fpsincos(x: bigint, scale: bigint): [bigint, bigint] {
492509 }
493510
494511 // sin term at n+1: sinTerm = sinTerm * r² / ((n+1)*n*scale²)
495- sinTerm = sinTerm * r2 / ( BigInt ( n + 1 ) * BigInt ( n ) * scale2 ) ;
512+ sinTerm = ( sinTerm * r2 ) / ( BigInt ( n + 1 ) * BigInt ( n ) * scale2 ) ;
496513
497514 if ( n % 4 === 2 ) {
498515 // n=2: subtract for cos (term 2: -r²/2!), subtract for sin (term 3: -r³/3!)
@@ -511,8 +528,8 @@ export function fpsincos(x: bigint, scale: bigint): [bigint, bigint] {
511528 // sin(2θ) = 2·sin(θ)·cos(θ)
512529 // cos(2θ) = 2·cos²(θ) - 1
513530 for ( let i = 0 ; i < k ; i ++ ) {
514- const newSin = 2n * sinVal * cosVal / scale ;
515- const newCos = 2n * cosVal * cosVal / scale - scale ;
531+ const newSin = ( 2n * sinVal * cosVal ) / scale ;
532+ const newCos = ( 2n * cosVal * cosVal ) / scale - scale ;
516533 sinVal = newSin ;
517534 cosVal = newCos ;
518535 }
@@ -549,13 +566,13 @@ export function fpatan(x: bigint, scale: bigint): bigint {
549566 // If x/scale > 1, use atan(x/scale) = π/2 - atan(scale/x)
550567 // In fixed-point: atan(x, scale) = halfPi - atan(scale² / x, scale)
551568 if ( x > scale ) {
552- const reciprocal = scale * scale / x ; // scale²/x represents scale/x in fp
569+ const reciprocal = ( scale * scale ) / x ; // scale²/x represents scale/x in fp
553570 return halfPi - fpatan ( reciprocal , scale ) ;
554571 }
555572
556573 // Halving: if x > 0.4 * scale, use atan(x) = 2*atan(x / (1 + sqrt(1 + x²)))
557574 // In fixed-point: threshold = 4*scale/10
558- const threshold = 4n * scale / 10n ;
575+ const threshold = ( 4n * scale ) / 10n ;
559576 let halvings = 0 ;
560577 let r = x ;
561578
@@ -568,7 +585,7 @@ export function fpatan(x: bigint, scale: bigint): bigint {
568585 const r2 = r * r ;
569586 const val = ( scale * scale + r2 ) / scale ;
570587 const sqrtVal = fpsqrt ( val , scale ) ; // sqrt(1 + t²) * scale
571- r = r * scale / ( scale + sqrtVal ) ;
588+ r = ( r * scale ) / ( scale + sqrtVal ) ;
572589 halvings ++ ;
573590 }
574591
@@ -581,7 +598,7 @@ export function fpatan(x: bigint, scale: bigint): bigint {
581598 const scale2 = scale * scale ; // hoisted: saves one bigint multiply per term
582599
583600 for ( let n = 3 ; ; n += 2 ) {
584- term = term * r2 / scale2 ;
601+ term = ( term * r2 ) / scale2 ;
585602 if ( term === 0n ) break ;
586603 // Late division by n: the per-term truncation error is < 1 ULP each,
587604 // so total error is bounded by ~nTerms/2 ULP — well within the 15 guard digits.
0 commit comments