Reste de la division d'un nombre à 25 chiffres

Résolu
cs_GigaCool Messages postés 43 Date d'inscription mardi 10 avril 2007 Statut Membre Dernière intervention 15 décembre 2008 - 20 avril 2007 à 19:23
cs_GigaCool Messages postés 43 Date d'inscription mardi 10 avril 2007 Statut Membre Dernière intervention 15 décembre 2008 - 24 avril 2007 à 09:48
Je n'arrive pas à trouver contourner le problème de dépassement de capacité en recherchant le mod d'un nombre de 25 chiffres! Les fonctions MOD et algo de division euclidienne ne marchent pas ! Please help !!!

8 réponses

jmfmarques Messages postés 7666 Date d'inscription samedi 5 novembre 2005 Statut Membre Dernière intervention 22 août 2014 27
20 avril 2007 à 19:37
Ouille !

Je dois me préparer à sortir et n'ai pas le temps de fouiller, mais toi oui ....
En cherchant avec "dépassement de capacité" (si ma mémoire est bonne, tu vas trouver une longue discussion à l'intérieur de laquelle tu trouveras la division et la soustraction.
Ces deux opérations doivent te suffire puisque

a mod b
c'est finalement
a - la partie entière de a divisé par b

Bonne chance dans ta recherche (la solution est au bout)
3
Gobillot Messages postés 3140 Date d'inscription vendredi 14 mai 2004 Statut Membre Dernière intervention 11 mars 2019 34
24 avril 2007 à 00:53
Salut,
la question de départ était: Reste de la division d'un nombre à 25 chiffres
voilà un code pour calculer le modulo (entier) pour un nombre à 29 chiffres maxi:

    Dim term1 As Variant
    Dim term2 As Variant
       
    term1 = CDec("12345678901234567890123456789")
    term2 = CDec("19")
   
'   term1 mod term2 = 1
    MsgBox term1 - Int(term1 / term2) * term2

Daniel
3
Mikaels35 Messages postés 146 Date d'inscription dimanche 23 janvier 2005 Statut Membre Dernière intervention 17 novembre 2009 2
20 avril 2007 à 19:43
Attention jmfmarques !

a mod b
c'est finalement
a - ( la partie entière de a divisé par b)*b

@+
0
jmfmarques Messages postés 7666 Date d'inscription samedi 5 novembre 2005 Statut Membre Dernière intervention 22 août 2014 27
20 avril 2007 à 20:12
Juste (frappé trop vite) ... Merci d'avoir corrigé !
Bon appétit.
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
Gobillot Messages postés 3140 Date d'inscription vendredi 14 mai 2004 Statut Membre Dernière intervention 11 mars 2019 34
20 avril 2007 à 21:05
c'est pas vraiment ça
Vb ne calcule le modulo que sur des entiers
les valeurs décimales sont arrondies et non pas tronquées
on retrouve la même chose dans la division entière:
   19.3\1.9 --> 19\2 = 9   (et non pas 19/1 19 ni même encore 19/1.9 10)

modulo entier:
1930/190  =>  1930/190 = 10 reste 30
193/19    =>  193/19   = 10 reste 3
19.3/1.9  =>  19/2     =  9 reste 1
1.93/0.19 =>  1/0      = division par zéro




modulo réel:
1930/190  = 10 reste 30

193/19    = 10 reste 3

19.3/1.9  = 10 reste 0.3

1.93/0.19 = 10 reste 0.03







Daniel
0
Gobillot Messages postés 3140 Date d'inscription vendredi 14 mai 2004 Statut Membre Dernière intervention 11 mars 2019 34
20 avril 2007 à 21:16
il y a une erreur dans mon exemple:
    1.93 mod 0.19 =>  2 mod 0  --> division par zéro   (même résultat)
comment être sûr ???
en prenant d'autres valeurs
    1.93 mod 2.5  => 2 mod 2  --> 0    ( se méfier des arrondis quand même)
    1.93 mod 2.6  => 2 mod 3  --> 2

Daniel
0
cs_GigaCool Messages postés 43 Date d'inscription mardi 10 avril 2007 Statut Membre Dernière intervention 15 décembre 2008
23 avril 2007 à 12:09
Je vous fais une copie des codes retrouvés sous dans le forume.
Merci JmfMarques et bien de choz à tout le monde.

Private Function multi(term1 As String, term2 As String)
    Dim total2 As String
    Dim i As Integer, a As Integer
    Dim total As String
    Dim nbr As Integer
    Dim nbr2 As Integer
   
    Dim nb1 As Integer
   
    total2 = "0"
    Dim nbd, nbt, nbu As Integer
   
    Dim L1 As Integer, L2 As Integer    L1 Len(term1): L2 Len(term2)
    For i = 0 To L2 - 1
        nbd = 0
        total = vbNullString
        nbr2 = Mid$(term2, L2 - i, 1)
        For a = 0 To L1 - 1
            nb1 = Mid$(term1, L1 - a, 1)
            nbt = nb1 * nbr2 + nbd            nbd nbt \ 10: nbu nbt Mod 10
            total = nbu & total
        Next a
        total2 = addition(nbd & total & String$(i, "0"), total2)
    Next i
   
    multi = zero(total2)
End Function


Function zero(term1 As String) As String
    Dim i As Integer
   
    For i = 1 To Len(term1) - 1
        If Mid$(term1, i, 1) <> "0" Then Exit For
    Next i


    zero = Mid$(term1, i)
End Function


Private Function addition(term1 As String, term2 As String)
    Dim nbd, nbt, nbu As Integer
    Dim L1 As Integer, L2 As Integer
    Dim i As Integer
    Dim nb1 As Integer, nb2 As Integer
    Dim total As String


    L1 Len(term1): L2 Len(term2)
    If L1 < L2 Then
        term1 = String$(L2 - L1, "0") & term1
        L1 = L2
    ElseIf L1 > L2 Then
        term2 = String$(L1 - L2, "0") & term2
    End If
      
    For i = 0 To L1 - 1
        nb1 = Mid$(term1, L1 - i, 1)
        nb2 = Mid$(term2, L1 - i, 1)
       
        nbt = nb1 + nb2 + nbd        nbd nbt \ 10: nbu nbt Mod 10
        total = nbu & total
    Next i
    addition = zero(nbd & total)
End Function


Private Function soustraction(term1 As String, term2 As String)
    Dim mPgdd As Integer:    mPgdd = pgdd(term1, term2)
   
    If mPgdd = 0 Then
        soustraction = "0"
        Exit Function
    End If
   
    If mPgdd = 2 Then
        soustraction = "-" & soustraction(term2, term1)
        Exit Function
    End If
   
    Dim nbd As Integer, nbt As Integer, nbu As Integer
   
    Dim L1 As Integer, L2 As Integer    L1 Len(term1): L2 Len(term2)
   
    If L1 < L2 Then
        term1 = String$(L2 - L1, "0") & term1
        L1 = L2
    ElseIf L1 > L2 Then
        term2 = String$(L1 - L2, "0") & term2
    End If
   
    Dim nb1 As Integer, nb2 As Integer, nbd2 As Integer
    Dim i As Integer
    Dim total As String
   
    For i = 0 To L1 - 1
        nb1 = Mid$(term1, L1 - i, 1)
        nb2 = Mid$(term2, L1 - i, 1)
        If nb1 - nb2 - nbd2 < 0 Then
            nbd = Abs(nb1 - nb2) \ 10 + 1
            nb1 = nb1 + nbd * 10
        End If
        nbu = nb1 - nb2 - nbd2        nbd2 nbd: nbd 0
        total = nbu & total
    Next i
    soustraction = zero(total)
   
End Function


Private Function divis(term1 As String, term2 As String, Optional ByRef rest As Boolean)
    Dim total As String:    total = "0"
   
    Dim L1 As Integer, L2 As Integer    L1 Len(term1): L2 Len(term2)
   
    Dim nbz As Integer, nbr1 As String
   
    Do While L1 > L2
        nbz = L1 - L2 - 1
        nbr1 = term2 & String$(nbz, "0")
        term1 = soustraction(term1, nbr1)
        L1 = Len(term1)
        total = addition(total, "1" & String$(nbz, "0"))
    Loop
   
    Dim i As Integer
    Dim reste As String
    For i = 1 To 20
        Select Case pgdd(term1, term2)
            Case 0
                total = addition(total, "1")
                reste = "0"
                Exit For
            Case 1
                term1 = soustraction(term1, term2)
                total = addition(total, "1")
            Case 2
                reste = term1
                Exit For
        End Select
    Next i
   
    rest = (0 <> LenB(reste))
    If rest Then total = total & "r" & reste
  
    divis = zero(total)
End Function


Private Function pgdd(term1 As String, term2 As String) As Integer
    Dim L1 As Integer, L2 As Integer    L1 Len(term1): L2 Len(term2)
   
    If L1 > L2 Then
        pgdd = 1
        Exit Function
    ElseIf L1 < L2 Then
        pgdd = 2
        Exit Function
    End If
   
    Dim i As Integer
    For i = 1 To Len(term1)
        L1 = Mid$(term1, i, 1)
        L2 = Mid$(term2, i, 1)
       
        If L1 > L2 Then
            pgdd = 1
            Exit Function
        ElseIf L1 < L2 Then
            pgdd = 2
            Exit Function
        End If
Next
End Function
0
cs_GigaCool Messages postés 43 Date d'inscription mardi 10 avril 2007 Statut Membre Dernière intervention 15 décembre 2008
24 avril 2007 à 09:48
Je viens de tester ton code Daniel et ça marche cool  ! J'avais utilisé la fonction CDec mais l'avait plûtot appliqué à un longint.
Merci beaucoup !
Le long code que j'ai trouvé marche pour n'importe quel longueur de chiffre et ça peut servir aussi ! C'est pourquoi j'accepte les deux réponse !

Bonne journée
0
Rejoignez-nous