www.webdeveloper.com
Results 1 to 9 of 9

Thread: modulo on big numbers

  1. #1
    Join Date
    Aug 2013
    Posts
    2

    modulo on big numbers

    Hi,

    I've got an PDF form with formfields.
    Some of the need to calculate a code, therefore I need a Javascript code...

    The code that i've got does work for small numbers but not for big numbers...
    I'v got this code:

    =====
    if(getField("TestIBAN").value == "")
    {
    event.value = 0;
    }
    else
    {
    event.value = (getField("TestIBAN").value % 97);
    }
    =====

    I've found a snippet on the web for big numbers but I don't know how to use them in the code above... Can someone help me?

    The snippet:

    =====
    Max value for Javascript numbers is: 9007199254740992 This function performs a modulo on numbers bigger than that.

    "aNumStr" is the number (as string) you want to devide. "aDiv" is the number you want to devide by.



    function modulo( aNumStr, aDiv)
    {
    var tmp = "";
    var i, r;
    for ( i=0; i<aNumStr.length ; i++)
    {
    tmp += aNumStr.charAt( i);
    r = tmp % aDiv;
    tmp = r.toString( 10);
    }
    return tmp / 1;
    }
    =====

    Thnx

  2. #2
    Join Date
    Aug 2013
    Posts
    2
    And the next question I need to make the big number, it's a combination of some fields...

    The fields: Bankname (4 characters), Country (2 characters), bankaccount (10 numbers, if less add zero's before number)

    First we need to make a number of the 'bankname' (we have to do that by changing all the letters by there position in the Latin alphabet, A=11, B=12, C=13, D=14 .... Z=35

    Behind that number we have to put the 10 numbers of the bankaccount
    Behind that we have to put the Country in numers (same way as 'bankname')
    And at least we need to add 00 (two zero's)

    The long number that we've got there is the 'aNumStr' as in the script above... and the 'aDiv' is 97.
    Finally we have to calculate '98 - the result of modulo' (1 - 9 need to be shown as 01 - 09).

    (as you can see, I need an IBAN calculator)

  3. #3
    Join Date
    Sep 2008
    Location
    Jackson MS
    Posts
    373
    Remember how you were taught long division as a youngster? Try writing a program to do the same thing. If it will work on smaller numbers you can verify with the modulo operator, it should work on the really big ones.

  4. #4
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,264
    With Javascript, Numbers are effectively stored as floating point with a 52 bit mantissa 2^53-1 == 9 007 199 254 740 991 with a 64-bit processor (The bitwise operators and shift operators operate only on 32-bit integers, but % operate always on Numbers with 15 numbers).

    If I understand right, You have to work with Strings (representative of Numbers) of : 8 (4 double bank name characters) + 4 (2 country double characters) + 10 (bank account) + 2 (00) = 24 characters.

    Then you have only to cut your String in 2 pieces of 12 characters and write :

    'aNumStr' = '12firstNumStrChars' * 1,000,000,000,000 + '12lastNumStrChars';

    'aNumStr'%97 = ( ('12firstNumStrChars'%97)* (1,000,000,000,000%97) + '12lastNumStrChars'%97 ) % 97;

    But 1,000,000,000,000 % 97 is 50. Then your function is simply
    Code:
    String.prototype.bankAccountKey=function(){return ((this.substr(0,12)%97)*50+this.substr(12)%97)%97;}
    
    // Test 
    alert(1000000000000%97); // => 50 
    
    // Use 
    var aNumStr='012345678901234567890123';
    // Use 
    alert(aNumStr.bankAccountKey()) // => 19
    </script>
    Last edited by 007Julien; 08-07-2013 at 04:23 PM.

  5. #5
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,264
    Obviously (n*97+r)*(m*97+s) is egal to (n*m+r*m+s*n)*97 + (r*s) then is rest % 97 is (r*s) % 97 !
    Then (A*B)%97 is ((A%97)*(B%97))%97

    The function is to complete with 98 - the result and a leading zero
    String.prototype.bankAccountKey=function(){
    var m=98-((this.substr(0,12)%97)*50+this.substr(12)%97)%97;
    return m<10?'0'+m:''+m;
    }
    Last edited by 007Julien; 08-07-2013 at 05:40 PM.

  6. #6
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,264
    But the order of terms seems not the same as yours see wikipedia

    A new code for any string length with Wikipedia example

    Code:
    var aNumStr='3214282912345698765432161182';
     
    String.prototype.bankAccountKey=function(){
    	var str=this.toString(),deb,rst;
    	while (deb=str.substr(0,12),str=str.substr(12)) {str=(deb%97).toString()+str;}
    	rst=98-deb%97;
    	return rst<10?'0'+rst:''+rst;
    }
    alert(aNumStr.bankAccountKey())
    Last edited by 007Julien; 08-07-2013 at 07:32 PM.

  7. #7
    Join Date
    Jul 2013
    Posts
    66
    Quote Originally Posted by 007Julien View Post
    Obviously (n*97+r)*(m*97+s) is egal to (n*m+r*m+s*n)*97 + (r*s)
    No, it is not. Luckily the argument still works with the correct result.

  8. #8
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,264
    You are rigth Airblader. I forgott a 97 with the term n*m (which is to replace by n*m*97), this do'not change the result which is k*97+(r*s). But this formula is not the best to calculate a modulo for a number in form of a big string.

    The rule is that, it's possible to replace a n length « string number » from left by is result %97.
    With 32 bit systems and a largest integer 10 digits length 2 147 483 647), it's probably secure to take 9 length strings and to use this prototype :

    Code:
    String.prototype.bankAccountKey=function(){var s=this;
        //Some transformations of the chain
        //...
        while (2<s.length) s=''+(s.substr(0,9)%97)+s.substr(9)
        s=98-s;
        return s<10?'0'+s:''+s;
    }
    
    // to replace spaces and letters  (A=>10, B=>11 etc z=>36)
    var str='W E S T12345698765432 GB82';
    var rsp=str.replace(/\s/g,'').toUpperCase().replace(/[A-Z]/g,function(a){return a.charCodeAt(0)-55})
    alert(str+'\n'+rsp)
    It is sometimes complicated to make it simple !
    Last edited by 007Julien; 08-08-2013 at 03:39 AM.

  9. #9
    Join Date
    Sep 2007
    Posts
    315
    Code:
       
    <script type="text/javascript">
    
    var s = '3214282912345698765432161182';  // 1
    //var s='012345678901234567890123';      // 19
    //var s = '1000000000000';               // 50
    
    var N = [ s.slice(0, 9) % 97 ]; 
    
    for(var i=9; i< s.length; i+=7) {
    
    // alert( '\n  N[0] + s.slice(i, i+7) =  '+  N[0] + s.slice(i, i+7) );
    
    N[0] = ( N[0] + s.slice(i, i+7) ) % 97;
    
    // alert('i nin değeri = '+i+ ' \nDöngüdeki N = '+ N );
    }
    
    alert('N = '+ N);
    
    </script>
    Last edited by Ayşe; 08-12-2013 at 04:53 PM.
    The Time Through Ages
    1. By the Time,
    2. Verily Man is in loss,
    3. Except such as have Faith, and do righteous deeds, and (join together) in the mutual enjoining of Truth, and of Patience and Constancy.

Thread Information

Users Browsing this Thread

There are currently 2 users browsing this thread. (0 members and 2 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles