Skip to content

Commit

Permalink
✨ feat: Add parsing methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
make-github-pseudonymous-again committed May 5, 2020
1 parent 4864223 commit d218211
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 5 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
},
"devDependencies": {
"@aureooms/js-integer": "6.0.0",
"@aureooms/js-integer-big-endian": "^8.0.0",
"@aureooms/js-number": "3.1.0",
"@aureooms/js-prime": "4.0.0",
"@babel/cli": "7.8.4",
Expand Down
4 changes: 4 additions & 0 deletions src/_constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

export const DECIMAL_PREFIX = '.' ;
export const REPETEND_PREFIX = '|' ;
export const FRACTION_SEP = '/' ;
33 changes: 33 additions & 0 deletions src/_parse_fixed_point.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

import { DECIMAL_PREFIX , REPETEND_PREFIX } from './_constants' ;

export function _parse_fixed_point ( { _chr , reg , sub } ) {

return function ( base , s ) {

const [ integral , decimal ] = s.split(DECIMAL_PREFIX) ;
const [ transient , repetend ] = decimal.split(REPETEND_PREFIX) ;

const _integral = integral === '0' ? '' : integral ;
const _repetend = repetend || '' ;

const _denominator = ( !_repetend ?
_chr(1) :
(new Array(repetend.length+1)).join( _chr(base-1) )
) +
(new Array(transient.length+1)).join( _chr(0) ) ;

const _big = _integral + transient + _repetend ;
const _small = _integral + transient ;

const _bign = reg(_big, base) ;
const _smalln = reg(_small, base) ;

const numerator = _repetend ? sub(_bign, _smalln) : _smalln ;
const denominator = reg(_denominator, base) ;

return [ numerator , denominator ] ;

}

}
14 changes: 14 additions & 0 deletions src/_parse_fraction.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

import { FRACTION_SEP } from './_constants' ;

export function _parse_fraction ( { reg } ) {

return function ( base , s ) {

const [ _numerator , _denominator ] = s.split(FRACTION_SEP) ;

return [ reg(_numerator, base), reg(_denominator, base) ] ;

}

}
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ export * from './cmp_no_bounds' ;
export * from './simplify' ;
export * from './digits' ;
export * from './_stringify_digits' ;
export * from './_parse_fixed_point' ;
export * from './_parse_fraction' ;
11 changes: 9 additions & 2 deletions test/src/_fixtures.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import int from 'int' ;
import BN from 'bn.js' ;
import { ZZ } from '@aureooms/js-integer' ;
import { _chr } from '@aureooms/js-integer-big-endian' ;

export const ALU = [
{
name : 'int',
anybase: false,
add : (a, b) => a.add(b),
sub : (a, b) => a.sub(b),
mul : (a, b) => a.mul(b),
Expand All @@ -23,15 +25,17 @@ export const ALU = [
divmod : (a,b) => [a.div(b), a.mod(b)],
divmodn : (a,b) => [a.div(b), a.mod(b)],
pown : (x,n) => x.pow(n),
_chr,
},
{
name : 'bn.js',
anybase: true,
add : (a, b) => a.add(b),
sub : (a, b) => a.sub(b),
mul : (a, b) => a.mul(b),
muln : (a, b) => a.muln(b),
div : (a, b) => a.div(b),
reg : x => new BN(x),
reg : (x, base) => new BN(x, base),
str : (x, base) => x.toString(base),
jz : x => x.eqn(0),
lt0 : x => x.ltn(0),
Expand All @@ -54,15 +58,17 @@ export const ALU = [
return { u: b.div(gcd), v: a.div(gcd) } ;
} ,
pown : (x,n) => x.pow(new BN(n)),
_chr,
},
{
name : '@aureooms/js-integer',
anybase: true,
add : (a, b) => a.add(b),
sub : (a, b) => a.sub(b),
mul : (a, b) => a.mul(b),
muln : (a, b) => a.muln(b),
div : (a, b) => a.div(b),
reg : x => ZZ.from(x),
reg : (x, base) => ZZ.from(x, base),
str : (x, base) => x.toString(base),
jz : x => x.iszero(),
lt0 : x => x.sign() < 0,
Expand All @@ -82,6 +88,7 @@ export const ALU = [
} ;
},
pown : (x,n) => x.pown(n),
_chr,
}
];

48 changes: 45 additions & 3 deletions test/src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { $2, iadd1, eq0, gt1, divmod } from "@aureooms/js-number" ;
import {
_add , _sub , _mul , _div , _pow ,
_cmp , _cmp_no_bounds ,
_simplify , _digits , _stringify_digits
_simplify , _digits , _stringify_digits ,
_parse_fraction , _parse_fixed_point
} from '../../src';

const GOOGOL = '10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000' ;
Expand Down Expand Up @@ -70,8 +71,22 @@ function unary ( t , alu , [ [ _x , _y , factory ] , a , b , e ] ) {

}

unary.title = ( _ , alu , [ [ name , impl ] , a , b , e ] ) => {
return `${name}<${impl.name}, ${alu.name}> ${name}(${a}/${b}) = ${e}` ;
unary.title = ( _ , alu , [ [ name , op , impl ] , a , b , e ] ) => {
return `${name}<${impl.name}, ${alu.name}> ${name}(${a}/${b}) ${op} ${e}` ;
} ;

function macro_parse ( t , alu , [ [ _x , _y , factory ] , base , s , e ] ) {

const apply = factory( alu );

const z = apply(base, s)

t.is(e, z) ;

}

macro_parse.title = ( _ , alu , [ [ name , op , impl ] , base , s , e ] ) => {
return `${name}<${impl.name}, ${alu.name}> ${name}(${base},${s}) ${op} ${e}` ;
} ;

const add = [ 'add' , '+' , [ _add ] , binary ] ;
Expand All @@ -95,10 +110,28 @@ const stringify_n = b => alu => {
return ( x , d ) => _stringify_digits( alu.str , b , digits(x, d) ) ;
} ;


const stringify_10 = [ 'stringify_10' , '=' , [ stringify_n(10) ] , unary , alu => alu.egcd ] ;
const stringify_2 = [ 'stringify_2' , '=' , [ stringify_n(2) ] , unary , alu => alu.egcd ] ;
const stringify_19 = [ 'stringify_19' , '=' , [ stringify_n(19) ] , unary , alu => alu.egcd ] ;

const parse_fraction = [ 'parse_fraction' , '=' , [
alu => {
const repr = x => `${alu.str(x[0])}/${alu.str(x[1])}` ;
const simp = _simplify(alu) ;
const parse = _parse_fraction(alu);
return (base, s) => repr(simp(...parse(base, s))) ;
}
] , macro_parse , alu => alu.anybase ] ;
const parse_fixed_point = [ 'parse_fixed_point' , '=' , [
alu => {
const repr = x => `${alu.str(x[0])}/${alu.str(x[1])}` ;
const simp = _simplify(alu) ;
const parse = _parse_fixed_point(alu);
return (base, s) => repr(simp(...parse(base, s))) ;
}
] , macro_parse , alu => alu.egcd && alu.anybase ] ;

const PARAMS = [

[ add , '3', '4', '1', '4', 1] ,
Expand Down Expand Up @@ -214,6 +247,15 @@ const PARAMS = [

[ stringify_19 , '14' , '13', '1.|18ebd2ha475g'] , // HOHO

[ parse_fraction , 10 , '1/2' , '1/2' ] ,
[ parse_fraction , 10 , '10/20' , '1/2' ] ,

[ parse_fixed_point , 19 , '1.|18ebd2ha475g' , '14/13' ] ,
[ parse_fixed_point , 2 , '0.1' , '1/2' ] ,
[ parse_fixed_point , 10 , '3.|1415929203539823008849557522123893805309734513274336283185840707964601769911504424778761061946902654867256637168' , '355/113' ] ,

[ parse_fixed_point , 10 , '0.0|2' , '1/45' ] ,

] ;

for (const alu of ALU)
Expand Down

0 comments on commit d218211

Please sign in to comment.