|
-
Mar 8th, 2003, 12:52 PM
#1
Thread Starter
Addicted Member
LONG NUMBERS --> No Exponents
Hi...I'm trying to make a program to find patterns in numbers. The only problem is that the numbers i'm using can have more than 15 digits long, which is bigger than the maximum number for a DOUBLE type variable. I tried using strings but it still failed and gave me something like 1.11111111111111E16 and i dont want that because it rounds the numbers up and defeats the purpose behind patterns.
The thing is, i'm adding numbers together using something like
A = Val(Num1) + Val(Num2)
and that always gives me exponents. so how do i fix this?
-
Mar 8th, 2003, 01:10 PM
#2
PowerPoster
val returns numeric string of appropriate type. As you say you understand, double only goes to 15 digits, so if you feed val something that has to be returned as a double, then it will give you a value that has less than 15 digits and the way it does that is to use exponential notation.
It sounds as though you want to use the VB data types and yet not be bound by their limitations. Can't happen.
-
Mar 8th, 2003, 01:15 PM
#3
Frenzied Member
U should be able to do it......the old fashioned way....Give me a sec..
"Brothers, you asked for it."
...Francisco Domingo Carlos Andres Sebastian D'Anconia
-
Mar 8th, 2003, 01:32 PM
#4
Frenzied Member
Ok a very barbones version. U could add error handling code
VB Code:
Private Function Add_The_Way_We_Learnt_As_Kids(uValOne As String, uValTwo As String) As String
Dim bytCarry As Byte, intResult As String, lngPos As Long
Dim StrSum As String
If Len(uValOne) > Len(uValTwo) Then
uValTwo = String(Len(uValOne) - Len(uValTwo), "0") & uValTwo
ElseIf Len(uValOne) < Len(uValTwo) Then
uValOne = String(Len(uValTwo) - Len(uValOne), "0") & uValOne
End If
For lngPos = Len(uValOne) To 1 Step -1
intResult = (CInt(Mid(uValOne, lngPos, 1)) + CInt(Mid(uValTwo, lngPos, 1)))
If bytCarry = 1 Then
intResult = intResult + 1
bytCarry = 0
End If
If Len(CStr(intResult)) = 2 Then
bytCarry = 1
intResult = CInt(Right$(CStr(intResult), 1))
End If
StrSum = CStr(intResult) & StrSum
'Debug.Print StrSum
Next lngPos
Add_The_Way_We_Learnt_As_Kids = StrSum
End Function
Private Sub Command1_Click()
Dim testVal1 As String, testVal2 As String
testVal1 = "123458792314147451225187487840010005484445610005"
testVal2 = "4568144484844848489421213232955629595944"
Debug.Print Add_The_Way_We_Learnt_As_Kids(testVal1, testVal2)
Debug.Print CCur(testVal1) + CCur(testVal2)
End Sub
Same as we learnt in junior school. Beging from the right and carry over if greater than 9.
"Brothers, you asked for it."
...Francisco Domingo Carlos Andres Sebastian D'Anconia
-
Mar 8th, 2003, 02:53 PM
#5
Thread Starter
Addicted Member
the code's good..but it's buggy and I can't find the bug for some reason. You're all familiar with the fibinacci sequence, right? So i tested your code with that simple program...but for some reason, it can't do it and I couldn't find the problem. :S I fixed it for the first 2 digits but that's it. So I put it back to your code, and only 1 digit is added :S!
VB Code:
Option Explicit
Sub Main()
Fibinacci 216, "D:\OutPut.txt"
End Sub
Private Function Adding(uValOne As String, uValTwo As String) As String
Dim bytCarry As Byte
Dim intResult As String
Dim lngPos As Long
Dim StrSum As String
If Len(uValOne) > Len(uValTwo) Then
uValTwo = String(Len(uValOne) - Len(uValTwo), "0") & uValTwo
ElseIf Len(uValOne) < Len(uValTwo) Then
uValOne = String(Len(uValTwo) - Len(uValOne), "0") & uValOne
End If
For lngPos = Len(uValOne) To 1 Step -1
intResult = (CInt(Mid(uValOne, lngPos, 1)) + CInt(Mid(uValTwo, lngPos, 1)))
If bytCarry = 1 Then
intResult = intResult + 1
bytCarry = 0
End If
If Len(CStr(intResult)) = 2 Then
bytCarry = 1
intResult = CInt(Right$(CStr(intResult), 1))
End If
StrSum = CStr(intResult) & StrSum
Next lngPos
Adding = StrSum
End Function
Function Fibinacci(Times As Integer, FilePath As String) As Boolean
On Error Resume Next
Dim I As Long
Dim A As String
Dim B As String
Dim C As String
Kill FilePath
A = "1"
B = "1"
C = "0"
Open FilePath For Binary As #1
Put #1, , "1." & vbTab & A & vbCrLf
Put #1, , "2." & vbTab & B & vbCrLf
For I = 2 To Times
A = Adding(A, B)
DoEvents
Put #1, , I & "." & vbTab & A & vbCrLf
C = A
A = B
B = C
Next I
Close #1
End Function
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|