Visual Basic

Moderators: None (Apply to moderate this forum)
Number of threads: 18013
Number of posts: 55386

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Ideas about a new VARIABLE ? Posted by ranainnet on 21 Oct 2005 at 12:41 PM
Hie there fellows.

Can anybody give me any idea of a new variable tyep. The question is if I need a variable that would hold more than a double tupe variable then what should I do? Is there any easy way in VB6 which will enable me to create this type of variable and do + - * / works on this type of variable?

I know double is a very very wide one which can hold more than I need, but I wonder what should I do if someone needs one that can hold more than that.

Any Ideas please.
_____________________________________________________________________________
Knowledge Is Power. Be Sure To Use This Power For Others WelFare. (;->
Report
Re: Ideas about a new VARIABLE ? Posted by BitByBit_Thor on 22 Oct 2005 at 6:52 AM
To do that, you will either 1) Need to write an own class (or collection of functions with a Type structure as the data container) that will supply the various operations on such a 'variable' or 2) Find someone who already has.

The standard operators will not work on this type, unless you are using VB.NET (or C++, C# etc) where you can overload the * operator (and others).
In VB6 it will look like:
Class BigNumber
-Function Multiply(Takes either a numeral variable or a BigNumber class)
-... other functions like the previous
-Number property (as a string) containing the integer number contained in BigNumber)

To give you an idea of how it will look. To do this with Integer numbers is the easiest. Creating a large double will be a bit more work.

But the biggest problem is that you will have to 'reinvent' all the mathematical operators... For example: You need create an algorithm to add these 'BigNumbers' to eachother. After that, to create the multiply operator, say for the formula 25 x BigNumber1, you will need to call the Addition 25 times with BigNumber1 as parameter.

Greets...
Richard

Report
Re: Ideas about a new VARIABLE ? Posted by ranainnet on 22 Oct 2005 at 9:57 AM
Thanks for your co-operation. And if u can please give me some more ideas about how to constract the + or - functions on the BigNumber.

Like should it be ok if I seperate the big number into small parts e.g.
12345679.....123456789 into

1234 5678 9... 1234 5678 9012

and put them into series of variables in my new class. And then make a function which will constract the pieces of numbers into one number.

ITS REALLY A CRITICAL WORK. Sorry for my low quality english. And thanks again.
_____________________________________________________________________________
Knowledge Is Power. Be Sure To Use This Power For Others WelFare. (;->

Report
Re: Ideas about a new VARIABLE ? Posted by BitByBit_Thor on 22 Oct 2005 at 11:22 AM
The problem with large numbers in VB is that you can't add 'm together without causing overflow errors.

The problem is that VB only has one unsigned type, which is the Integer. An unsigned long would be nice for this kind of work :-S

But perhaps it is easier in the first place to store your 'number' as a decimal string.

That way, you could take it character by character.

Plus would then be:
Take the first character of both strings.
Convert them to an integer and add them together (in a special function).
Then check if the number is larger than 9. If it is, remember this and substract 10 from it.
The special function should return the single decimal number that came out of the addition (with the 10 dropped from it). If the number was larger than 10, also tell this to the calling function.
Write to the new string this new answer.
Repeat this process for each character. If there was a remainder, don't forget to add this to.

Now I am sorry for my pour explanation. I don't have time right now, but I might clarify this later with an example of what I mean.

Greets...
Richard

Report
Re: Ideas about a new VARIABLE ? Posted by ranainnet on 23 Oct 2005 at 10:19 AM
This message was edited by ranainnet at 2005-10-23 10:22:53

Thanks a lot for ur effort of making thinsg yasier. Don't worry, you explained well. I will let u know after completing something that might do the task of the large variable. Another thing is that,


I understand that I have to deal with the overflow to do the addition and for that I have to do recursive calling to a function. After that I can call the addition function for several times to calculate a multiplication or power.

But I don't have any idea of how to do the division task? Should I subtract(-) 155 from 23456 for several times and count a number to find out the integer portion of the result to calculate 23456/155.

Bye for now, Hope to hear form U soon.
_____________________________________________________________________________
Knowledge Is Power. Be Sure To Use This Power For Others WelFare. (;->



Report
Re: Ideas about a new VARIABLE ? Posted by BitByBit_Thor on 26 Oct 2005 at 8:25 AM
I've been thinking about substraction (or rather I had been thinking).
For instance: 34567/155
Substract 155 from 34567 until the remaining number is smaller than 155.
Keep a count of how many times you do this. Call this number n for now.
The remainder will be r.
You'll get an answer: n will be the integer number (dropping the remainder). If you want to round it, check if r > (155 / 2), or in words "If the remainder is larger than half the divisor".

Have fun
(Note that this all is only for Integer numbers. To use decimal numbers, you'll have to think of what to do with the remainder)

Greets...
Richard

Report
Re: Ideas about a new VARIABLE ? Posted by infidel on 24 Oct 2005 at 8:02 AM
: Hie there fellows.
:
: Can anybody give me any idea of a new variable tyep. The question is if I need a variable that would hold more than a double tupe variable then what should I do? Is there any easy way in VB6 which will enable me to create this type of variable and do + - * / works on this type of variable?
:
: I know double is a very very wide one which can hold more than I need, but I wonder what should I do if someone needs one that can hold more than that.
:
: Any Ideas please.

Use Python

I think the Currency type in VB is "bigger" than the Double type.

Seriously, there are algorithms out there for performing arithmetic operations on very large numbers. I don't know how suitable VB is for implementing those algorithms. Python comes with this kind of power out-of-the-box:

>>> print 10**100
10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000



infidel

$ select * from users where clue > 0
no rows returned


Report
Re: Ideas about a new VARIABLE ? Posted by ranainnet on 24 Oct 2005 at 1:27 PM
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. (;->

Report
Re: Ideas about a new VARIABLE ? Posted by Bulgarian_VB on 26 Oct 2005 at 1:50 PM
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.
subtraction 154
subtraction 153
subtraction 152
... and so on...
subtraction 1
subtraction 0...

Well .. this repeats too many times. How about this:
   We have X/Y. To get the result, we need Y = 1.
   So ...
   (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
            -            -
            2            2

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:
   00000000 10000000

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:
   00000000 00000000
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:
            HI       LO
   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
            00000001 00000000


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,
Nikolay Semov



Report
Re: Ideas about a new VARIABLE ? Posted by BitByBit_Thor on 27 Oct 2005 at 9:27 AM
Thanks for explaining it.
I don't understand your calculation, but I do know what you are saying.
I thought about this method too, but it was a bit difficult to explain and besides, I had already suggested using a string rather than 4 longs. The reason for this is because VB does NOT have Unsigned Longs, which means you'll have to take in account the Sign Bit (which is a pain in the ass).

But, you said something about bitshifts (Which would surely make this way easier)? Is that in VB6 or are we talking VB.NET here?

Greets...
Richard

Report
Re: Ideas about a new VARIABLE ? Posted by Bulgarian_VB on 27 Oct 2005 at 9:35 AM
This message was edited by Bulgarian_VB at 2005-10-27 9:41:30

This message was edited by Bulgarian_VB at 2005-10-27 9:40:42

: Thanks for explaining it.
: I don't understand your calculation, but I do know what you are saying.
: I thought about this method too, but it was a bit difficult to explain and besides, I had already suggested using a string rather than 4 longs. The reason for this is because VB does NOT have Unsigned Longs, which means you'll have to take in account the Sign Bit (which is a pain in the ass).
:
: But, you said something about bitshifts (Which would surely make this way easier)? Is that in VB6 or are we talking VB.NET here?
:
: Greets...
: Richard
:
:
VB6 for sure. I don't know about VB.NET.

By the way ... you're looking at bits. Since you are defining operations on those bits, you don't need to care how VB interprets them. So you might as well NOT count a sign bit. Which reminds me: Not only would you have to define operations, but also define printing capabilities, that is... conversion into string. Then, you'll have the freedom to interpret the most significant bit as a sign bit, or not.

By the way .... what if at some point you need to use that large large number in a SQRT or trig function. Then you'll have a lot of trouble. Not that it's imposible, but there's a lot of work. When I think of it, actually, trig functions won't be that difficult since they are periodic, but anyway...

Hope I helped,
Nikolay Semov





Report
Re: Ideas about a new VARIABLE ? Posted by ranainnet on 27 Oct 2005 at 2:03 PM
This message was edited by ranainnet at 2005-10-27 14:5:32

Thanks everybody for ur responses. Those ideas are really helpful.

I learned about the bitwise division and multiplication in Assembly and C. But did not know whethere it would be applicable in VB6. Thanks fellows for the ideas.

Hear is something more interesting. Instead of making just a fixed length variable type, We can also build a function that takes the number of byte the new variable might take and then adjust all the related functions about + - * / accoudingly.

I think I m talking about a special type of ofject.

Thanks to everybody again.
_____________________________________________________________________________
Knowledge Is Power. Be Sure To Use This Power For Others WelFare. (;->



Report
Re: Ideas about a new VARIABLE ? Posted by BitByBit_Thor on 27 Oct 2005 at 2:05 PM
The reason why I concern myself with the sign bit is because of the lack (as far as I know) of good bit manipulating operators.
If there really is a bitshift in Vb6, then the sign bit is no longer any problem (not that it ever was, but without a bitshift you'll have to account for the sign bit, because you are modifying the number using numbers, not binary operations).

Greets...
Richard




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.