Ideas about a new VARIABLE ? - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Ideas about a new VARIABLE ?

ranainnetranainnet Posts: 54Member
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. (;->
«1

Comments

  • BitByBit_ThorBitByBit_Thor Posts: 2,444Member
    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

  • ranainnetranainnet Posts: 54Member
    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. (;->

  • BitByBit_ThorBitByBit_Thor Posts: 2,444Member
    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

  • ranainnetranainnet Posts: 54Member
    [b][red]This message was edited by ranainnet at 2005-10-23 10:22:53[/red][/b][hr]
    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. (;->



  • infidelinfidel Posts: 2,900Member
    : 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:

    [code]
    >>> print 10**100
    10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
    [/code]


    [size=5][italic][blue][RED]i[/RED]nfidel[/blue][/italic][/size]

    [code]
    $ select * from users where clue > 0
    no rows returned
    [/code]

  • ranainnetranainnet Posts: 54Member
    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. (;->

  • BitByBit_ThorBitByBit_Thor Posts: 2,444Member
    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

  • Bulgarian_VBBulgarian_VB Posts: 307Member
    [b][red]This message was edited by Bulgarian_VB at 2005-10-26 13:51:46[/red][/b][hr]
    : 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:
    [code]
    FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF
    [/code]

    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:
    [code]
    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
    [/code]
    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:
    [code]
    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
    [/code]

    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



  • BitByBit_ThorBitByBit_Thor Posts: 2,444Member
    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

  • Bulgarian_VBBulgarian_VB Posts: 307Member
    [b][red]This message was edited by Bulgarian_VB at 2005-10-27 9:41:30[/red][/b][hr]
    [b][red]This message was edited by Bulgarian_VB at 2005-10-27 9:40:42[/red][/b][hr]
    : 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





«1
Sign In or Register to comment.