## Diff code: bcmod

 Function: Description: Get the modulus of the left_operand using modulus.
bcmod version 497bcmod version 521
1/*
1/*
2Note: this function is a hack, and it only supports the use cases I require at
2Note: this function is a hack, and it only supports the use cases I require at
3the moment, being: arbitrary precision integer left_operand and native precision positive integer modulus (must fit in a native number, i.e. a double).
3the moment, being: an arbitrary precision decimal left_operand and native precision positive decimal modulus (must fit in a native number, i.e. a double).
4*/
4*/
5function bcmod( left_operand, modulus ) {
5function bcmod( left_operand, modulus ) {
6  // John was here!
7  // Key was here!
6  var i, d;
8  var i, d;
7  function split( operand ) {
9  function split( operand ) {
8    var parts = operand.split( '.' );
10    var parts = operand.split( '.' );
9    i = parts[ 0 ];
11    var i = parts[ 0 ];
10    d = '0.' + ( parts.length > 1 ? parts[ 1 ] : '0' );
12    var d = '0.' + ( parts.length > 1 ? parts[ 1 ] : '0' );
13    return { "i": i, "d": d };
11  }
14  }
12  //modulus = Math.round( parseFloat( modulus ) );
15  //modulus = Math.round( parseFloat( modulus ) );
13  modulus = parseFloat( modulus );
16  modulus = parseFloat( modulus );
14  modulus = abs( modulus );
17  modulus = abs( modulus );
15  if ( modulus === 0 ) { return null; }
18  if ( modulus === 0 ) { return null; }
16  var take = 4;
19  var take = 4;
17  var modulus_limit = Math.pow( 10, take );
20  var modulus_limit = Math.pow( 10, take );
18  if ( modulus >= modulus_limit ) {
21  if ( modulus >= modulus_limit ) {
19    throw new Error( "Modulus cannot be " + modulus_limit + " or greater." );
22    throw new Error( "Modulus cannot be " + modulus_limit + " or greater." );
20  }
23  }
21  var result = 0;
24  var result = 0;
22  split( left_operand );
25  var r = split( left_operand );
26  i = r.i; d = r.d;
23  var read_length = 0;
27  var read_length = 0;
24  function read() {
28  function read() {
25    read_length = i.length < take ? i.length : ( take + 1 );
29    read_length = i.length < take ? i.length : ( take );
26    var result = i.substr( 0, take );
30    var result = i.substr( 0, take );
27    i = i.substr( take );
31    i = i.substr( take );
28    return result;
32    return result;
29  }
33  }
30  do {
34  function round( value ) {
31    var n = parseInt( read() );
35    //console.log( "V:" + value );
32    result = ( result * Math.pow( 10, read_length - 1 ) + n ) % modulus;
36               //0.8999999999999997
33  }
37               //0.09999999999999984
34  while ( i.length > 0 );
38               //0.29999999999999993
35  result += parseFloat( d );
39               //0.7999999999999998
36  var test = result - modulus;
40               //0.8999999999999999
37  result = test < 0 ? result : test;
41               //0.10000000000000009
38  function round( value, units ) {
42               //0.029999999999999985
43    var units = '0.0000000000000000';
39    function get_chars( s ) {
44    function get_chars( s ) {
40      var result = [];
45      var result = [];
41      for ( var i = 0, il = s.length; i < il; i++ ) {
46      for ( var j = 0, jl = s.length; j < jl; j++ ) {
42        result.unshift( s.charAt( i ) );
47        result.unshift( s.charAt( j ) );
43      }
48      }
44      return result;
49      return result;
45    }
50    }
46    value = value.toString();
51    value = value.toString();
47    split( value );
52    var r = split( value );
48    d = d.substring( 0, units.length );
53    var d_chars = get_chars( r.d.substring( 0, units.length ) );
49    var d_chars = get_chars( d );
54    //console.log( "D:" + d_chars.toString() );
50    var carry;
55    var carry = 0;
51    for ( var ci = 0, cl = d_chars.length; ci < cl; ci++ ) {
56    for ( var ci = 0, cl = d_chars.length; ci < cl; ci++ ) {
52      var c = d_chars[ ci ];
57      var c = d_chars[ ci ];
53      if ( ! /^[0-9]\$/.test( c ) ) { break; }
58      if ( ! /^[0-9]\$/.test( c ) ) { break; }
54      if ( ci === 0 ) {
59      if ( ci === 0 && cl >= units.length ) {
55        carry = Math.round( parseFloat( '0.' + c ) );
60        carry = Math.round( parseFloat( '0.' + c ) );
56        d_chars[ ci ] = '0';
61        d_chars[ ci ] = '0';
57      }
62      }
58      else {
63      else {
59        var v = parseInt( c ) + carry;
64        var v = parseInt( c ) + carry;
60        if ( v >= 10 ) {
65        if ( v >= 10 ) {
61          carry = 1;
66          carry = 1;
62          v -= 10;
67          v -= 10;
63        }
68        }
64        else {
69        else {
65          carry = 0;
70          carry = 0;
66        }
71        }
67        d_chars[ ci ] = v.toString();
72        d_chars[ ci ] = v.toString();
68      }
73      }
74    }
75    var result = ( parseInt( r.i ) + carry ).toString() +
76      array_reverse( d_chars.slice( 0, -1 ) ).join( '' ).replace( /[0.]*\$/, '' );
77    //console.log( "N:" + result );
78    return result;
79  }
80  do {
81    var n = parseInt( read() );
82    //console.log( result );
83    result = ( result * Math.pow( 10, read_length ) + n ) % modulus;
84    var temp = parseFloat( round( result ) );
85    //console.log( "R:" + result );
86    //console.log( "T:" + temp );
87    if ( ( temp - modulus ) >= 0 ) {
88      temp -= modulus;
89      //console.log( "T:" + temp );
90      result = temp;
69    }
91    }
70    return ( parseInt( i ) + carry ).toString() + array_reverse( d_chars.slice( 0, -1 ) ).join( '' ).replace( /[0.]*\$/, '' );
71  }
92  }
72                         //0.8999999999999997
93  while ( i.length > 0 );
73                         //0.09999999999999984
94  result += parseFloat( d );
74                         //0.29999999999999993
95  var test = result - modulus;
75                         //0.7999999999999998
96  result = test < 0 ? result : test;
76                         //0.8999999999999999
97  result = round( result );
77                         //0.10000000000000009
98  //console.log( result );
78  result = round( result, '0.0000000000000000' );
99  //console.log();
79  return result.toString();
100  return result;