## Function: bcmod version 486

 Function: Description: Get the modulus of the left_operand using modulus.
 Version: 486 Status: obsolete Date: 2012-03-16 21:52 Summary: Rounding to 13 decimal places. Programmer: jj5
Edit function
 Code: /* Note: this function is a hack, and it only supports the use cases I require at the moment, being: arbitrary precision integer left_operand and native precision positive integer modulus (must fit in a native number, i.e. a double). */ function bcmod( left_operand, modulus ) { var i, d; function split( operand ) { var parts = operand.split( '.' ); i = parts[ 0 ]; d = '0.' + ( parts.length > 1 ? parts[ 1 ] : '0' ); } //modulus = Math.round( parseFloat( modulus ) ); modulus = parseFloat( modulus ); modulus = abs( modulus ); if ( modulus === 0 ) { return null; } var take = 4; var modulus_limit = Math.pow( 10, take ); if ( modulus >= modulus_limit ) { throw new Error( "Modulus cannot be " + modulus_limit + " or greater." ); } var result = 0; split( left_operand ); var read_length = 0; function read() { read_length = i.length < take ? i.length : take; var result = i.substr( 0, take ); i = i.substr( take ); return result; } do { var n = parseInt( read() ); result = ( result * Math.pow( 10, read_length ) + n ) % modulus; } while ( i.length > 0 ); result += parseFloat( d ); var test = result - modulus; result = test < 0 ? result : test; function round( value, units ) { if ( units <= 0 ) { throw new Error( "Units must be greater than zero." ); } if ( units > 0.1 ) { throw new Error( "Units must be less than or equal to 0.1." ); } value = value.toString(); split( value ); d = d.substring( 0, units.toString().length ); var a = value; var b = ( parseInt( i ) + parseFloat( d ) + units ).toString(); return a.length > b.length ? b : a; } //0.8999999999999997 //0.09999999999999984 //0.29999999999999993 //0.7999999999999998 //0.8999999999999999 //0.10000000000000009 result = round( result, 0.0000000000001 ); return result.toString(); } You have to be logged in to edit functions.
[top]