Diff code: bcmod

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