|
-
Apr 8th, 2009, 08:42 PM
#1
[RESOLVED] Convert a floating-point no. to its true/complete numeric value
How can I convert a number like 1.21932631356501E+215 to its complete form?
Last edited by dee-u; Apr 9th, 2009 at 01:41 PM.
-
Apr 8th, 2009, 09:04 PM
#2
Re: Convert a floating-point no. to its true/complete numeric value
Like this?
Code:
Dim fl As Double
fl = 1.21932631356501E+21
MsgBox Format(fl, "0") ' = 1219326313565010000000
-
Apr 8th, 2009, 09:10 PM
#3
Re: Convert a floating-point no. to its true/complete numeric value
dee-u
What do you mean by "complete form"?
Do you want to see 202 zeros?
Do you also want to see commas?
Is your screen wide enough?? 
EDIT: Actually, looks like jcis is on to something (albeit sans commas) 
Spoo
-
Apr 8th, 2009, 09:26 PM
#4
Re: Convert a floating-point no. to its true/complete numeric value
I actually would want to multiply 1234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891234567891 23456789 to 9876543219876543219876543219876543219876543219876543219876543219876543219876543219876543219876543219 87654321 aand it should also return a compete number instead of a floating point. The number I quoted above was the result in Double.
I am searching some ways on how I could determine the exact product of those two values so I could compare with the result from jcis' recommendation.
-
Apr 8th, 2009, 09:40 PM
#5
Re: Convert a floating-point no. to its true/complete numeric value
dee-u
Did you mean you wanted to multiply a 120 digit number by another 120
digit number and produce the exact result?
EDIT: If so, how do you propose to even show a number with 120 significant figures?
One way might be to utilize an array. Double-precision sure won't do the job.
Spoo
-
Apr 8th, 2009, 09:48 PM
#6
Re: Convert a floating-point no. to its true/complete numeric value
 Originally Posted by dee-u
How can I convert a number like 1.21932631356501E+215 to its complete form?
That's not really a floating point number; that's a very large integer. My solutions are very different for both problems.
For storing the exact value of a decimal, I would store the numerator and denominator. So instead of a floating point .333..., I'd store the integers 1 and 3. This of course won't work for irrational numbers, but those can't be stored with exact precision pretty much by definition. (Right?)
To handle very large integers, however, I'd set up my own math processor. This has come up in the past, and Logophobic has pointed out that using strings for this is hugely inefficient. But it works well for pointing you in the right direction.
Store those two numbers as strings. Then set up loops to go through and do your addition / subtraction / whatever exactly the same way you were taught in elementary school. For addition, add the two right-most digits together, carry the 1, move left one and add the next set of digits, carry the 1, etc...
EDIT: Come to think of it, Logo may have actually posted a functional solution. I'm too lazy to search for it now, though.
-
Apr 8th, 2009, 09:50 PM
#7
Re: Convert a floating-point no. to its true/complete numeric value
How could I utilize an array in this case? I would want it to make a function that will accept such large number (a string or other appropriate variable type) and return a string that has the result.
-
Apr 8th, 2009, 09:53 PM
#8
Re: Convert a floating-point no. to its true/complete numeric value
Strings are just arrays of characters. They are conceptually the same thing.
-
Apr 8th, 2009, 09:57 PM
#9
Re: Convert a floating-point no. to its true/complete numeric value
Im not sure if I understand, you mean just this?
Code:
Private Function GetMiltiply(pNumber1 As Double, pNumber2 As Double) As String
GetMiltiply = Format(pNumber1 * pNumber2, 0)
End Function
Code:
MsgBox GetMiltiply(1.23456789123457E+107, 9.87654321987654E+107)
-
Apr 8th, 2009, 10:00 PM
#10
Re: Convert a floating-point no. to its true/complete numeric value
 Originally Posted by dee-u
How could I utilize an array in this case?
LOL.. that was just an idea, not a complete solution. But, it could be
like a byte array, each element represents, 1,10,100,1000, etc. I went
this route as opposed to a string (I see that Ellis Dee also went there)
because you do want to actually do some math, right?
Addition or subtraction would be a snap, either way. But multiplication,
which was your original goal, seemed to me to beg for numeric values,
not characters, which would need to be converted.
Spoo
-
Apr 8th, 2009, 10:02 PM
#11
Re: Convert a floating-point no. to its true/complete numeric value
 Originally Posted by jcis
Im not sure if I understand, you mean just this?
Code:
Private Function GetMiltiply(pNumber1 As Double, pNumber2 As Double) As String
GetMiltiply = Format(pNumber1 * pNumber2, 0)
End Function
Code:
MsgBox GetMiltiply(1.23456789123457E+107, 9.87654321987654E+107)
No, he wants the actual values. Doubles are only approximations. For example:
Code:
Public Sub Test()
Dim dblNumber As Double
dblNumber = 1.23456789123457E+107
If dblNumber + 1 = dblNumber Then MsgBox "Epic Fail"
End Sub
It's never a good thing when your math ends up showing that a + 1 = a.
-
Apr 8th, 2009, 10:06 PM
#12
Re: Convert a floating-point no. to its true/complete numeric value
 Originally Posted by Ellis Dee
No, he wants the actual values. Doubles are only approximations. For example:
Code:
Public Sub Test()
Dim dblNumber As Double
dblNumber = 1.23456789123457E+107
If dblNumber + 1 = dblNumber Then MsgBox "Epic Fail"
End Sub
It's never a good thing when your math ends up showing that a + 1 = a.
Awwww, good example. I didn't know that.
-
Apr 8th, 2009, 11:00 PM
#13
Re: Convert a floating-point no. to its true/complete numeric value
 Originally Posted by Ellis Dee
EDIT: Come to think of it, Logo may have actually posted a functional solution. I'm too lazy to search for it now, though.
I tried searching but as of now I cannot find it, any keyword clues?
-
Apr 8th, 2009, 11:39 PM
#14
Fanatic Member
Re: Convert a floating-point no. to its true/complete numeric value
How about binary arrays. Do the digit by digit math with long variables for the answer and the carries. Use a for next loop that multiplies and adds the carry. You could use redim preserve to shift up for the higher place values but multiply with the same loop.
Where the dickens are you gonna display the answer on a scrolling sign in time square?
Last edited by technorobbo; Apr 9th, 2009 at 05:39 AM.
Reason: Post edited because I crossed radices
-
Apr 9th, 2009, 12:28 AM
#15
Re: Convert a floating-point no. to its true/complete numeric value
There are many ways to multiply BIG integers (fast or slow).
We had discussed about this a year ago on these forums (one of the threads are between me and dbasnett).
Search "BigInteger" and/or "LargeInteger" to find more.
This is a way to multiply 2 "digit string", it is not very fast because it use a "primary school method" to multiply.
It will be quicker if chop "number strings" into smaller parts of 4 digits (use with Long) or 7 digits (use with Currency) or 14 digits (use with Decimal Variant).
In this function, it uses 1-digit parts with Byte data type.
Code:
Sub BigTest()
Dim sNum1 As String
Dim sNum2 As String
Dim sNum3 As String
sNum1 = "123456789123456789123456789123456789123456789123456789123456" & _
"789123456789123456789123456789123456789123456789"
sNum2 = "987654321987654321987654321987654321987654321987654321987654" & _
"321987654321987654321987654321987654321987654321"
sNum3 = BigMultiply(sNum1, sNum2)
Debug.Print sNum3
'-- sNum3 = "121932631356500531591068431825636332060204232294772132529340" & _
"032763907932998475833233043733467611633702179533692882171458" & _
"314271223746370989178470754610570520042670285474770050906869" & _
"816338969581771069347203169112635269"
End Sub
Code:
Function BigMultiply(ByVal sNum1 As String, sNum2 As String) As String
Dim d1 As Byte, d2 As Byte, r As Byte, d As Byte
Dim n1 As Long, n2 As Long, n3 As Long
Dim i As Long, j As Long, k As Long, n As Long
Dim bn1() As Byte, bn2() As Byte
Dim Matrix() As Byte
Dim sResult As String
bn1 = StrConv(sNum1, vbFromUnicode)
bn2 = StrConv(sNum2, vbFromUnicode)
For i = 0 To UBound(bn1): bn1(i) = bn1(i) - 48: Next
For i = 0 To UBound(bn2): bn2(i) = bn2(i) - 48: Next
n1 = UBound(bn1) + 1
n2 = UBound(bn2) + 1
n3 = n1 + n2
ReDim Matrix(1 To n2, 1 To n3)
sResult = String$(n3, "0")
For i = n2 To 1 Step -1
r = 0
For j = n1 To 1 Step -1
n = bn2(i - 1) * bn1(j - 1) + r '<<-- updated
d = n Mod 10
r = n \ 10
k = i + j
Matrix(i, k) = d
Next
If r > 0 Then Matrix(i, k - 1) = r
Next
n = 0
For k = n3 To 1 Step -1
For i = n2 To 1 Step -1
n = n + Matrix(i, k)
Next
Mid$(sResult, k, 1) = n Mod 10
n = n \ 10
Next
If n > 0 Then '-- there was a mistake in these 2 lines:
sResult = n & sResult '-- n must be used instead of r
Else
Do While Left$(sResult, 1) = "0"
sResult = Mid$(sResult, 2)
Loop
End If
BigMultiply = sResult
End Function
Last edited by anhn; Apr 9th, 2009 at 11:54 PM.
-
Apr 9th, 2009, 06:49 AM
#16
Re: Convert a floating-point no. to its true/complete numeric value
 Originally Posted by dee-u
I tried searching but as of now I cannot find it, any keyword clues? 
Found it by searching for "universe".
http://www.vbforums.com/showthread.php?t=483207
Though it is a thread about converting very large hex numbers to decimal, I bring up the idea of using strings to do arithmetic on them. Logo then posts code snippets of how to do it with byte arrays as a (much) faster alternative.
There are also links that go to functional very large decimal calculators.
-
Apr 9th, 2009, 06:53 AM
#17
Re: [RESOLVED] Convert a floating-point no. to its true/complete numeric value
heh, I like the (lack of) precision demonstration function I posted to that thread much better:
Code:
Public Sub Imprecision()
Dim dblNumber As Double
Dim dblCompare As Double
dblNumber = HexToDecimal("2134325423423452354765785609234defaffbc9049823984908234")
dblCompare = dblNumber - 10000000000000#
If dblNumber = dblCompare Then
MsgBox "x minus 10,000,000,000,000 is equal to x"
End If
End Sub
Subtract 10 trillion from a number and the number still doesn't change. Ouch.
-
Apr 9th, 2009, 09:06 AM
#18
Re: [RESOLVED] Convert a floating-point no. to its true/complete numeric value
dee-u
I see that this is now [resolved] -- can you give us a clue as to your
resolution?
Spoo
-
Apr 9th, 2009, 09:10 PM
#19
Re: Convert a floating-point no. to its true/complete numeric value
-
Apr 9th, 2009, 09:34 PM
#20
Re: Convert a floating-point no. to its true/complete numeric value
 Originally Posted by anhn
There are many ways to multiply BIG integers (fast or slow).
Unless both parameters passed to your BigMultiply are of equal length I get a subscript out of range error.
Debug.Print BigMultiply("100", "10") ' error
Just thought I mention it, I suck at math and have no clue with stuff like this!
-
Apr 9th, 2009, 11:53 PM
#21
Re: Convert a floating-point no. to its true/complete numeric value
 Originally Posted by Edgemeal
Unless both parameters passed to your BigMultiply are of equal length I get a subscript out of range error.
Debug.Print BigMultiply("100", "10") ' error
Just thought I mention it, I suck at math and have no clue with stuff like this! 
There was a bug there with i and j mixed up
Replace
n = bn2(j - 1) * bn1(i - 1) + r
with
n = bn2(i - 1) * bn1(j - 1) + r
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
|