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.

Handling very large Integer numbers

BitByBit_ThorBitByBit_Thor Posts: 2,444Member
Hey,

I've been thinking some time about programming a collection of functions, sort of like a library (or perhaps even make it a class), to handle very large integer numbers.
My approach on this was to store the numbers as a string and then create the functions to handle strings and return a string.
I managed to write functions for Addition, Substraction, Multiplication, Dividation and Modulus, and a rough version of a Power function.
However, as I look at the code I cannot possibly believe that it'll work fast at all. Handling strings, making dozens of copies all the time, using Mid$ and string pasting operations etc can't be good.

I am wondering if anyone has an idea about how to create functions to handle very large integer numbers with some speed. Mostly talking about multiplication here, which I believe to be the slowest (I'm not even going to mention the Power function...).

A more detailed description of what I do when adding two numbers is this: I take a string number, I chop it into pieces (fixed length) and then do the operation on each block seperately: convert the two blocks you want to add together to a Long, let VB do the addition and remember the remainder (the part of the number that does not fit in the fixed length block). Then do the next two blocks, accounting for the remainder you had left from the previous two blocks.

The chopping the strings into blocks alone is a time-consuming process (for practical reasons I also reverse the order of the blocks from what is normal, eg last block comes first, first block becomes last).

Any ideas and thoughts would be much appreciated.

Best Regards,
Richard

Comments

  • iwilld0itiwilld0it Posts: 1,134Member
    [b][red]This message was edited by iwilld0it at 2007-1-5 13:9:56[/red][/b][hr]
    Couldn't you use the oh-so-large 256-bit decimal data-type in visual basic? VB6 cannot directly declare such a variable, but it can be done through variants like so:

    [code]
    Dim d As Variant
    d = CDec(1.00000000011126E+15)

    MsgBox d + 1
    MsgBox LenB(d) ' Just to prove its 256 bits
    [/code]


    : Hey,
    :
    : I've been thinking some time about programming a collection of functions, sort of like a library (or perhaps even make it a class), to handle very large integer numbers.
    : My approach on this was to store the numbers as a string and then create the functions to handle strings and return a string.
    : I managed to write functions for Addition, Substraction, Multiplication, Dividation and Modulus, and a rough version of a Power function.
    : However, as I look at the code I cannot possibly believe that it'll work fast at all. Handling strings, making dozens of copies all the time, using Mid$ and string pasting operations etc can't be good.
    :
    : I am wondering if anyone has an idea about how to create functions to handle very large integer numbers with some speed. Mostly talking about multiplication here, which I believe to be the slowest (I'm not even going to mention the Power function...).
    :
    : A more detailed description of what I do when adding two numbers is this: I take a string number, I chop it into pieces (fixed length) and then do the operation on each block seperately: convert the two blocks you want to add together to a Long, let VB do the addition and remember the remainder (the part of the number that does not fit in the fixed length block). Then do the next two blocks, accounting for the remainder you had left from the previous two blocks.
    :
    : The chopping the strings into blocks alone is a time-consuming process (for practical reasons I also reverse the order of the blocks from what is normal, eg last block comes first, first block becomes last).
    :
    : Any ideas and thoughts would be much appreciated.
    :
    : Best Regards,
    : Richard
    :



  • BitByBit_ThorBitByBit_Thor Posts: 2,444Member
    It's a sweet data type but not directly what I am after ;)
    I want to program a large 'datatype' myself, and now I am looking for smart ways to do so - smart ways to keep speed into the code.
    But thanks for mentioning it. I had kind of forgotten about the Decimal type again.

    Best Regards,
    Richard

  • DrMartenDrMarten Posts: 748Member
    [b][red]This message was edited by DrMarten at 2007-1-7 3:17:38[/red][/b][hr]
    : Hey,
    :
    : I've been thinking some time about programming a collection of functions, sort of like a library (or perhaps even make it a class), to handle very large integer numbers.
    : My approach on this was to store the numbers as a string and then create the functions to handle strings and return a string.
    : I managed to write functions for Addition, Substraction, Multiplication, Dividation and Modulus, and a rough version of a Power function.
    : However, as I look at the code I cannot possibly believe that it'll work fast at all. Handling strings, making dozens of copies all the time, using Mid$ and string pasting operations etc can't be good.
    :
    : I am wondering if anyone has an idea about how to create functions to handle very large integer numbers with some speed. Mostly talking about multiplication here, which I believe to be the slowest (I'm not even going to mention the Power function...).
    :
    : A more detailed description of what I do when adding two numbers is this: I take a string number, I chop it into pieces (fixed length) and then do the operation on each block seperately: convert the two blocks you want to add together to a Long, let VB do the addition and remember the remainder (the part of the number that does not fit in the fixed length block). Then do the next two blocks, accounting for the remainder you had left from the previous two blocks.
    :
    : The chopping the strings into blocks alone is a time-consuming process (for practical reasons I also reverse the order of the blocks from what is normal, eg last block comes first, first block becomes last).
    :
    : Any ideas and thoughts would be much appreciated.
    :
    : Best Regards,
    : Richard

    ______________________________________________________________________

    Hi,

    See the two last posts by Dan2178 at the end of this thread.>>

    http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=757494&SiteID=1

    [b][blue]I thought you and iwilld0it both had / have VB.Net too?
    [/blue][/b]

    I am considering writing a custom CLASS to do 10 million bit maths!!

    It would treat a number as 31250 * 32 bit chunks!!

    I would need to find out how to do stuff other than +, - , / and multiply
    and pow ( x ^ Y ) though. :-)


    Regards,

    Dr M.



  • BitByBit_ThorBitByBit_Thor Posts: 2,444Member
    [b][red]This message was edited by BitByBit_Thor at 2007-1-7 13:40:25[/red][/b][hr]
    : Hi,
    :
    : See the two last posts by Dan2178 at the end of this thread.>>
    :
    : http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=757494&SiteID=1
    :

    I wish they used code formatting! :-S
    Anyway, it's probably a smart way to split the exponent. I'll have a look at it.

    I glanced over the code, and they're doing pretty much the same with + as I did (only perhaps better, because it's VB.NET and all. If I'd optimize my own code it'd end up with what they have)

    And what I could gather from Division, my code *migh* even be better (not entirely sure)

    : [b][blue]I thought you and iwilld0it both had / have VB.Net too?
    : [/blue][/b]
    :

    I do, on one computer and on the other still VB6.
    It's just that for me VB6 programs way more sweetly :) I like to use it if I can over VB.NET. But that is, I admit, due to ignorance and lack of knowledge. I'm just so unfamilair with the Classes for VB.NET that it takes very long to program anything, so I still use VB6, even though I agree in much ways VB.NET is better. One bad evil thing though:
    I HATE VB.NET for not letting me declare the Array's Lower Boundry to say 1 or 2 or 50 or 50 million :P
    I like 1-based arrays... Or if not then atleast the way you do it in C:
    [code]
    char myArray[leftbr]count[rightbr]
    [/code]
    Pooh bad evil VB.NET:
    [code]
    Dim myArray(count [red]- 1[/red]) As Char
    [/code]
    And even worse is when you use them, always i - 1 or i = 0 To count - 1 begh :P

    And I really really like arrays :) So I'm sensitve about them rofl

    Alright enough ranting

    : I am considering writing a custom CLASS to do 10 million bit maths!!
    :
    : It would treat a number as 31250 * 32 bit chunks!!
    :
    What I am doing too :) In a way. I use a string to make it look neat, but when calculating it's chunks of 32 bits (or actually 31 bits because another evil VB6 thing is not having Unsigned Longs!)

    : I would need to find out how to do stuff other than +, - , / and multiply
    : and pow ( x ^ Y ) though. :-)
    :
    Don't even talk about that evil function! It's horror if x and y are the numbers you want them to be (big).

    Best Regards,
    Richard



Sign In or Register to comment.