code_junkie

VBA long overflow

Nov 12th, 2011
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 KB | None | 0 0
  1. r = A - Int(A / B) * B
  2.  
  3. VERSION 1.0 CLASS
  4. BEGIN
  5. MultiUse = -1 'True
  6. Persistable = 0 'NotPersistable
  7. DataBindingBehavior = 0 'vbNone
  8. DataSourceBehavior = 0 'vbNone
  9. MTSTransactionMode = 0 'NotAnMTSObject
  10. END
  11. Attribute VB_Name = "Decimals"
  12. Attribute VB_GlobalNameSpace = False
  13. Attribute VB_Creatable = True
  14. Attribute VB_PredeclaredId = False
  15. Attribute VB_Exposed = True
  16. Attribute VB_Ext_KEY = "SavedWithClassBuilder6" ,"Yes"
  17. Attribute VB_Ext_KEY = "Top_Level" ,"Yes"
  18. Option Explicit
  19.  
  20. 'local variable(s) to hold property value(s)
  21. Private mvarDec As Variant 'local copy
  22.  
  23. Public Property Let Dec(ByVal vData As Variant)
  24. 'used when assigning a value to the property, on the left side of an assignment.
  25. 'Syntax: X.Dec = 5
  26. mvarDec = CDec(vData)
  27. End Property
  28.  
  29. Public Property Get Dec() As Variant
  30. Attribute Dec.VB_UserMemId = 0
  31. 'used when retrieving value of a property, on the right side of an assignment.
  32. 'Syntax: Debug.Print X.Dec
  33. Dec = CDec(mvarDec)
  34. End Property
  35.  
  36. Dim dec1 As New Std.Decimals
  37. Dim dec2 As New Std.Decimals
  38. Dim dec3 As New Std.Decimals
  39. Dim modulus As New Std.Decimals
  40.  
  41. Sub main()
  42. dec1 = "1000.000000001"
  43. dec2 = "1000.00000000000001"
  44. dec3 = dec1 + dec2
  45. Debug.Print dec1
  46. Debug.Print dec2
  47. Debug.Print dec3
  48. Debug.Print dec3 * dec3
  49. Debug.Print dec3 / 10
  50. Debug.Print dec3 / 100
  51. Debug.Print Sqr(dec3)
  52. modulus = dec1 - Int(dec1 / dec2) * dec2
  53. Debug.Print modulus
  54. End Sub
  55.  
  56. 1000.000000001
  57. 1000.00000000000001
  58. 2000.00000000100001
  59. 4000000.000004000040000001
  60. 200.000000000100001
  61. 20.0000000000100001
  62. 44.721359550007
  63. 0.00000000099999
  64. 1000.000000001
  65. 1000.00000000000001
  66. 2000.00000000100001
  67. 4000000.000004000040000001
  68. 200.000000000100001
  69. 20.0000000000100001
  70. 44.721359550007
  71. 0.00000000099999
  72.  
  73. Function BigMultiply(ByVal s1 As String, ByVal s2 As String) As String
  74.  
  75. Dim x As Long
  76. x = 7
  77.  
  78. Dim n1 As Long, n2 As Long, n As Long
  79. n1 = Int(Len(s1) / x + 0.999999)
  80. n2 = Int(Len(s2) / x + 0.999999)
  81. n = n1 + n2
  82.  
  83. Dim i As Long, j As Long
  84. ReDim za1(n1) As Double
  85. i = Len(s1) Mod x
  86. If i = 0 Then i = x
  87. za1(1) = Left(s1, i)
  88. i = i + 1
  89. For j = 2 To n1
  90. za1(j) = Mid(s1, i, x)
  91. i = i + x
  92. Next j
  93.  
  94. ReDim za2(n2) As Double
  95. i = Len(s2) Mod x
  96. If i = 0 Then i = x
  97. za2(1) = Left(s2, i)
  98. i = i + 1
  99. For j = 2 To n2
  100. za2(j) = Mid(s2, i, x)
  101. i = i + x
  102. Next j
  103.  
  104. ReDim z(n) As Double
  105. Dim u1 As Long, u2 As Long
  106. Dim e As String
  107. e = String(x, "0")
  108.  
  109. For u1 = 1 To n1
  110. i = u1
  111. For u2 = 1 To n2
  112. i = i + 1
  113. z(i) = z(i) + za1(u1) * za2(u2)
  114. Next u2
  115. Next u1
  116.  
  117. Dim s As String, y As Double, w As Double, m As Long
  118. m = n * x
  119. s = String(m, "0")
  120. y = 10 ^ x
  121. For i = n To 1 Step -1
  122. w = Int(z(i) / y)
  123. Mid(s, i * x - x + 1, x) = Format(z(i) - w * y, e)
  124. z(i - 1) = z(i - 1) + w
  125. Next i
  126. 'truncate leading zeros
  127. For i = 1 To m
  128. If Mid$(s, i, 1) <> "0" Then Exit For
  129. Next i
  130. If i > m Then
  131. BigMultiply = ""
  132. Else
  133. BigMultiply = Mid$(s, i)
  134. End If
  135.  
  136. End Function
Add Comment
Please, Sign In to add comment