This message was edited by Bulgarian_VB at 2005-10-26 13:51:46
: I don't know about Python, Byt VB6 also can support a very very large number. In a Double type variable VB6 can ultimately support more than 300 dizit number value.
: Again I want to say that, this large number value is quite enougn to do everything I need. But what I am trying to find out is How to create a variable larger then the builtin type variables of VB6.
: In short if I start with Integer variables it wont be able to hold more than about -32000 to +32000, and about 64000 if it is unsigned (although I have to check it out that whethere VB6 supports unsigned Integer variables). But if I want to make a custom LargeInteger variable that may hold upto +1234567890 to -1234567890 then what should be the most appropriate and effecient way?
: Thanks for your response.
: Knowledge Is Power. Be Sure To Use This Power For Others WelFare. (;->
Let's see ... Long is 32-bit number (Let's talk integers for now).
Let's also suppose you need a 128-bit number, so ... you'll need four Long numbers:
FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF
As Richard pointed out (was it Richart? I forgot ... sorry) you will need custom made functions for arithmetic operations.
I have a suggestion for division and multiplication.
You suggested that you should subtract 155 times the number 155. So you'll have something like a counter.
... and so on...
Well .. this repeats too many times. How about this:
We have X/Y. To get the result, we need Y = 1.
(X/Y) * 1 = X/Y - true
1 = (1/2) / (1/2) - true
=> (X/Y) * [(1/2)/(1/2)] = X/Y
<=> 1 X
X 2 X 2 X
- * - = - OR - = -
Y 1 Y Y Y
The reason to do this is that if you look at the numbers in binary, you'll notice that multiplication and division by is done quite easy:
to multiply by 2 - simply shift the number to the left by 1 bit
to divide by 2 - shift right by 1 bit
You will have to consider carrying bits over to next part of the number.
Example (for simplicity, 16-bit number made up by two 8-bit numbers):
You have the following:
When you multiply by two, you have to shift left by 1 bit, but if you don't consider the carry-over, you will get this:
which obviously is not what you want. What you need to do is save the
first bit of the low-order byte, then do the shifting, and then add the carry-over to the high-order byte:
initial: 00000000 10000000
save 1st bit: CARRYY_OVER = BIT1(LO) ' = LO & 10000000 (bitwise AND)
shift: 00000000 00000000
add to HI: IF CARRY_OVER <> THEN HI = HI + 1
VB has built-in operators for shifting: SHL and SHR respectively for shifting left and right. Play around with this. Do some binary aritchemtic on paper using small numbers like two 8-bit numbers just to get a feel of how things work, and to run into some problems, which you will have to solve.
Hope I helped,