Gobillot
Messages postés
3140
Date d'inscription
vendredi 14 mai 2004
Statut
Membre
Dernière intervention
11 mars 2019
34
8 mars 2007 à 22:39
puisqu'on est là, voilà une occasion
je met la multiplication, il y a le signe, la virgule, l'arrondi
8 ms pour 2 nombres de 1000 chiffres en entrée et 2000 en sortie
Const chrz = "0"
Const sep = ","
Dim parm As Integer
Dim puis As Double
Dim frmt As String
Dim a1 As Integer
Dim a2 As Integer
Dim b1 As Integer
Dim b2 As Integer
Dim k As Integer
Public preci As Integer
Public pos As Integer
Public Modulo As String
Public Quotient As String
Public Tronq As Boolean
Public Function bMultiplication(n1 As String, n2 As String) As String
Dim a As String
Dim b As String
Dim d As String
Dim sg As String
Dim i As Long
Dim j As Long
Dim ka As Integer
Dim kb As Integer
Dim x As Integer
Dim y As Integer
Dim z As Integer
Dim r As Long
Dim tt As Double
Dim v1 As Double
Dim v2 As Double
Dim W1() As Double
Dim W2() As Double
parm = 7
puis = 10 ^ parm
frmt = String(parm, chrz)
i = InStr(n1, sep)
j = InStr(n2, sep) If i > 0 Then a1 i - 2: b1 Len(n1) - i Else a1 = Len(n1) - 1: b1 = 0 If j > 0 Then a2 j - 2: b2 Len(n2) - j Else a2 = Len(n2) - 1: b2 = 0
pos = b1 + b2 If Asc(n1) Asc(n2) Then sg "+" Else sg = "-"
i a1 + b1: x parm - (i Mod parm): If x = parm Then x = 0 j a2 + b2: y parm - (j Mod parm): If y = parm Then y = 0
a = String$(y, chrz) & Mid$(n2, 2, a2) & Mid$(n2, a2 + 3, b2)
b = String$(x, chrz) & Mid$(n1, 2, a1) & Mid$(n1, a1 + 3, b1) ka y + j: kb x + i
x kb \ parm: k ka + kb: z = k \ parm
ReDim W1(x), W2(z)
i = 1
For j = kb + 1 - parm To 1 Step -parm W1(i) CDbl(Mid$(b, j, parm)): i i + 1
Next
y = 0
For i = ka + 1 - parm To 1 Step -parm
v1 = CDbl(Mid$(a, i, parm))
r = 0
For j = 1 To x
tt = W1(j) * v1 + W2(j + y) + r
r = Int(tt / puis)
W2(j + y) = tt - r * puis
Next
W2(j + y) = r
y = y + 1
Next
d$ = String(k, chrz)
j = 1
For i = z To 1 Step -1 Mid$(d$, j, parm) Format$(W2(i), frmt): j j + parm
Next
bMultiplication = Arrondi(d$, sg)
End Function
Private Function Arrondi(a As String, sg As String) As String
Dim i As Integer
Dim j As Integer
Dim x As Integer
Dim r As Long
Dim t As Double
Dim z As Integer
x = k + 1 - pos
' suppression des zéros devant
For i = 1 To x - 2
If Mid$(a$, i, 1) <> chrz Then Exit For
Next
' suppression des zéros derrière
For j = k To x Step -1
If Mid$(a$, j, 1) <> chrz Then Exit For
Next
a1 = x - i
b1 = j + 1 - x
If Tronq Then b2 preci - a1: If b2 < 0 Then b2 0
If b2 < b1 Then
If Mid$(a$, x + b2, 1) > "4" Then
z% = x + b2 - 1 j z% Mod parm: If j 0 Then j = parm
j = z% - j + 1
r = 10 ^ ((k - z%) Mod parm)
While r > 0
t = CDbl(Mid$(a$, j, parm)) + r
r = 0 If t >puis Then r 1: t = t - puis
Mid$(a$, j, parm) = Format$(t, frmt)
j = j - parm
Wend
End If If Mid$(a$, i - 1, 1) <> chrz Then i i - 1: a1 a1 + 1
For j = x + b2 - 1 To x Step -1
If Mid$(a$, j, 1) <> chrz Then Exit For
Next
b1 = j + 1 - x
End If
End If
Arrondi = sg & Mid$(a$, i, a1)
If b1 > 0 Then Arrondi = Arrondi & sep & Mid$(a$, x, b1)
End Function
Daniel