Results 1 to 6 of 6

Thread: Mod function

  1. #1
    Join Date
    Aug 2006
    Posts
    1

    Unanswered: Mod function

    I have been looking at the mod function in access and it doesn't like it when processing very large numbers.
    Does anyone know how I can calculate the mod of a very large number easily in access?
    Thanks for your help

  2. #2
    Join Date
    Jun 2005
    Location
    Richmond, Virginia USA
    Posts
    2,763
    Provided Answers: 19
    What are you calling large numbers?
    Hope this helps!

    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  3. #3
    Join Date
    Apr 2003
    Location
    Alabama, USA
    Posts
    154
    MOD only handles LONG numbers.

    Try this
    Code:
    Instead of 
    num MOD div 
    
    Use
    num - div * INT(num/div)
    http://AccessDB.Info

    You live and learn. At any rate, you live. - Douglas Adams

  4. #4
    Join Date
    Jun 2005
    Location
    Richmond, Virginia USA
    Posts
    2,763
    Provided Answers: 19
    Actually MOD works with any numeric expression.
    Hope this helps!

    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  5. #5
    Join Date
    Apr 2003
    Location
    Alabama, USA
    Posts
    154
    Quote Originally Posted by Missinglinq
    Actually MOD works with any numeric expression.
    Yes, you are quite right. I should have been more specific. MOD only handles numbers from –2,147,483,648 to 2,147,483,647, which arre the limits for a LONG number.

    Also MOD treats the number as a whole number (no decimals).
    Code:
    2147483646.5 MOD 2 = 0
    
    2147483646.5 - 2 * INT(2147483646.5/2) =  0.5
    Last edited by Cosmos75; 08-16-06 at 13:06.
    http://AccessDB.Info

    You live and learn. At any rate, you live. - Douglas Adams

  6. #6
    Join Date
    Dec 2002
    Location
    Préverenges, Switzerland
    Posts
    3,740
    interesting!

    here's an outline attempt to explode the long-limit.
    the code below should achieve X = Y Mod Z for
    Y = 922,337,203,685,477
    Z = 30,000,000

    most likely you could modify the outline to use Double datatypes and extend the range to
    Y = something x 10^308
    Z = something x 10^154

    the outline code is full of debug lines and is not bullet-proofed.
    very small Y, Z (e.g. 0, 1) have not been investigated or guarded against
    ...but you did say 'very large numbers'!

    check the results from the algo before using it for real. i don't have an independent Mod tool to compare my results with but the algo seems reasonable.

    izy

    Code:
    Private Sub GO_Click()
    Dim X As Currency                           '       X = Y mod Z
    Dim Y As Currency
    Dim Z As Currency
    Dim i As Integer                            'max power
    Dim j As Currency                           'holder for current Z^k
    Dim k As Integer                            'actual power
    Const lMax As Long = 2147483647             'longinteger limit
    Const cMax As Currency = 922337203685477#   'Currency limit
    Const zMax As Long = 30000000               'max allowed Z in Y Mod Z (set to 922337203685477^0.5)
    Dim a As Long                               'a counter for debug - not used in calcs
    'get test data from a form
    Y = Me.numY
    Z = Me.numZ
    Debug.Print "----------------------------------------"
    Debug.Print "Calculate X = Y mod Z"
    Debug.Print "Y:"; Y, "Z:"; Z
    'boundary
    If Z > zMax Then
        MsgBox "Z is too large for this algorithm", vbCritical, "Overflow risk!"
        Me.numX = "Illegal input"
        Exit Sub
    End If
    'plan is to reduce Y by successive subtractions of
    'Z^i, Z^(i-1), Z^(i-2)... etc
    'until residual-Y is <= lMax
    'Z^i must not overflow datatype
    i = 1
    Do While Z ^ i < cMax / Z
        i = i + 1
    Loop
    Debug.Print "BEGIN powers subtraction"
    For k = i To 1 Step -1
        j = Z ^ k
        If ((j < Z) Or (Y < lMax)) Then Exit For 'do not kill the remainder or loop too much
        a = 0
        Do While Y > j
            Y = Y - j
            a = a + 1
        Loop
        Debug.Print "Loops:"; a, "k:"; k, "j:"; j, "Y:"; Y
    Next
    Debug.Print "END powers subtraction loop with Y:"; Y
    'Y is now significantly smaller
    'should be small enough (j<Z and lMax) '<<< LATER: and not zMax as i said the first time round
    'but just to make sure...
    Do While Y > lMax
        Y = Y - Z
        Debug.Print "brute force subtraction:", Y
    Loop
    'calculate mod
    X = Y Mod Z
    Debug.Print "Custom algo result X:", X
    'post the result
    Me.numX = X
    End Sub
    Debug output for some cases:

    Code:
    ----------------------------------------
    Calculate X = Y mod Z
    Y: 922337203685477          Z: 30000000 
    BEGIN powers subtraction
    Loops: 1      k: 2          j: 900000000000000          Y: 22337203685477 
    Loops: 744573               k: 1          j: 30000000   Y: 13685477 
    END powers subtraction loop with Y: 13685477 
    Custom algo result X:        13685477 
    ----------------------------------------
    Calculate X = Y mod Z
    Y: 922337203685477          Z: 30000 
    BEGIN powers subtraction
    Loops: 34     k: 3          j: 27000000000000           Y: 4337203685477 
    Loops: 4819   k: 2          j: 900000000  Y: 103685477 
    END powers subtraction loop with Y: 103685477 
    Custom algo result X:        5477 
    ----------------------------------------
    Calculate X = Y mod Z
    Y: 922337203685477          Z: 30 
    BEGIN powers subtraction
    Loops: 1      k: 10         j: 590490000000000          Y: 331847203685477 
    Loops: 16     k: 9          j: 19683000000000           Y: 16919203685477 
    Loops: 25     k: 8          j: 656100000000             Y: 516703685477 
    Loops: 23     k: 7          j: 21870000000              Y: 13693685477 
    Loops: 18     k: 6          j: 729000000  Y: 571685477 
    END powers subtraction loop with Y: 571685477 
    Custom algo result X:        17 
    ----------------------------------------
    Calculate X = Y mod Z
    Y: 922337203685             Z: 30000000 
    BEGIN powers subtraction
    Loops: 0      k: 2          j: 900000000000000          Y: 922337203685 
    Loops: 30744  k: 1          j: 30000000   Y: 17203685 
    END powers subtraction loop with Y: 17203685 
    Custom algo result X:        17203685 
    ----------------------------------------
    Calculate X = Y mod Z
    Y: 20000000000001           Z: 10 
    BEGIN powers subtraction
    Loops: 0      k: 14         j: 100000000000000          Y: 20000000000001 
    Loops: 2      k: 13         j: 10000000000000           Y: 1 
    END powers subtraction loop with Y: 1 
    Custom algo result X:        1
    Last edited by izyrider; 08-16-06 at 14:45.
    currently using SS 2008R2

Posting Permissions

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