-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/feature-stringify'
* origin/feature-stringify: Test with bases other than 10. Finish implementation of stringify. Update dependency @aureooms/js-integer to v6 Inital draft for stringify. 🔍 test: Extract fixtures.
- Loading branch information
Showing
9 changed files
with
238 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
export function _stringify_digits ( str , base , { sign , left , transient , repetend } ) { | ||
|
||
const toStr = x => str(x, base); | ||
|
||
let repr = '' ; | ||
|
||
if (sign < 0) repr += '-' ; | ||
|
||
repr += toStr(left) ; | ||
|
||
if (transient.length || repetend.length) repr += '.' ; | ||
repr += transient.map(toStr).join('') ; | ||
|
||
if (repetend.length) repr += '|' ; | ||
repr += repetend.map(toStr).join('') ; | ||
|
||
return repr ; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// TODO instead allocate enough space M < log_2(d) + d | ||
// for transient + repetend by multiplying x by b**M and do a single division ? | ||
// That may be too much space in most cases. Though necessary when d is prime. | ||
|
||
export function _decimals ( { b , eq , muln , divmod } ) { | ||
|
||
return function* ( d , n , hasrepetend , x ) { | ||
|
||
// Computes the length of the repetend of x/d (1 <= x < d) in base b | ||
// with transient part of size n. | ||
|
||
while ( n-- ) { | ||
|
||
x = muln(x, b) ; | ||
const [q, r] = divmod(x, d) ; | ||
yield q ; | ||
x = r ; | ||
|
||
} | ||
|
||
if ( !hasrepetend ) return ; | ||
|
||
const first = x ; | ||
|
||
x = muln(x, b) ; | ||
const [q, r] = divmod(x, d) ; | ||
yield q ; | ||
x = r ; | ||
|
||
while (!eq(first, x)) { | ||
|
||
x = muln(x, b) ; | ||
const [q, r] = divmod(x, d) ; | ||
yield q ; | ||
x = r ; | ||
|
||
} | ||
|
||
} ; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { take } from '@aureooms/js-itertools' ; | ||
import { _decimals } from './decimals' ; | ||
import { _transient } from './transient' ; | ||
|
||
export function _digits ( { b , bfactors , jz , gt1 , eq , muln , divmodn , divmod , egcd , sgn , abs } ) { | ||
|
||
const tr = _transient( { bfactors , jz , gt1 , divmodn } ) ; | ||
const dec = _decimals( { b , eq , muln , divmod } ) ; | ||
|
||
return function ( x , d ) { | ||
|
||
const [ left , r ] = divmod(abs(x), d) ; | ||
|
||
const { u , v } = egcd(d, r) ; | ||
|
||
const [ transient_length , has_repetend ] = tr( v ) ; | ||
|
||
const decimals = dec(v, transient_length, has_repetend , u) ; | ||
|
||
const transient = [ ...take(decimals, transient_length) ] ; | ||
|
||
const repetend = [ ...decimals ] ; | ||
|
||
return { sign: sgn(x) , left , transient , repetend } ; | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// credits https://github.com/aureooms-research/repeating-decimal | ||
|
||
export function _transient ( { bfactors , jz , gt1 , divmodn } ) { | ||
|
||
return function ( d ) { | ||
|
||
// Computes the length of the non repeating part in x / d | ||
// ( for any 1 <= x < d with x and d co-prime ) decimals in | ||
// base b whose prime factors are given. Returns tuple ( n , hasrepetend ) | ||
// where n is the length of the non repeating part and hasrepetend | ||
// determines if 1 / d repeats or not. | ||
|
||
let n = 0 ; | ||
|
||
for ( const f of bfactors ) { | ||
|
||
let m = 0 ; | ||
|
||
while ( true ) { | ||
|
||
const [ q , r ] = divmodn(d, f) ; | ||
|
||
if (!jz(r)) break ; | ||
|
||
++m ; | ||
d = q ; | ||
|
||
} | ||
|
||
n = Math.max(n, m) ; | ||
|
||
} | ||
|
||
return [ n , gt1(d) ] ; | ||
|
||
} ; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters