下面我已经在我的项目中编写了基础58转换的代码。得到两个小数的剩余部分可以正常工作,但是当试图得到两个大数的余数时,例如 - 1.03475761285374E + 33返回0.有没有办法划分双精度并得到余数?
Function Base58Value(sInput As String) As String
'reverse the input
sInput = StrReverse(sInput)
'get the length of the input string
Dim inputLen As Long
inputLen = Len(sInput)
'set up an array for each character
Dim sCharacter() As Integer
ReDim sCharacter(inputLen)
'set up an array for step 2 - index times 8 to the 2nd power
Dim sStep2() As Double
ReDim sStep2(inputLen)
'set up an array or step 3
Dim sStep3() As Double
ReDim sStep3(inputLen)
Dim sFinalNumber As Double
For x = 1 To inputLen
curchar$ = Mid(sInput, x, 1)
sCharacter(x) = Asc(curchar)
sStep2(x) = 2 ^ ((x - 1) * 8)
sStep3(x) = sCharacter(x) * sStep2(x)
sFinalNumber = sFinalNumber + sStep3(x)
Next x
Dim sRemainder() As Integer
ReDim sRemainder(0)
Dim sValue As Double
sValue = sFinalNumber
Dim sNextValue As Double
Dim sIter As Long
sIter = 0
Do Until Fix(sValue) = 0
DoEvents
ReDim Preserve sRemainder(sIter + 1)
sRemainder(sIter) = sMod(Fix(sValue), 58)
sValue = Fix(sValue / 58)
sIter = sIter + 1
Loop
Dim ConversionTable$
ConversionTable = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
Dim addValues As String
addValues = ""
Dim xI As Integer
For xI = 0 To UBound(sRemainder) - 1
DoEvents
List1.AddItem (sRemainder(xI))
addValues = addValues + Mid(ConversionTable, sRemainder(xI) + 1, 1)
Next xI
Base58Value = StrReverse(addValues)
End Function
Public Function sMod(a, b)
Dim x As Variant
x = a / b
x = x - Fix(x)
x = x * b
sMod = x
End Function
答案 0 :(得分:1)
在没有实际分析你的代码所做的事情的情况下,我会说浮点值代表了范围和精度之间的折衷:它不可能拥有所有这两者。非常大的浮点数不一定能够存储非常精确的值。
VB double中只有大约53位的实际精度,因此可以精确存储的最大整数大约为15位数。
尝试使用Decimals(变体)。我不确定他们会做你需要的,因为我说我没有分析你的代码。但它们确实提供了更高的精度 - 最多29个十进制数字,是双倍提供的两倍。