• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

I want to create a class like big integer

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm trying to create a class like big integer to add, subtract, and multiply very large integers. I'm using arrays where each element represents a digit. I'm having trouble implementing the add method. So far it only adds two negative numbers correctly. When I add a negative and a positive number, it just gives me the sum of the two numbers as a negative number. I was thinking about just sending the operation to my subtract method, but I haven't figured out how to implement that yet. Would it be implemented in a similar way to my add method but just replacing the add operator with a minus operator?
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Two things:
  • You didn't state your motivation for re-inenting the wheel. Without a reason, the best advice is to not re-invent the wheel.
  • Without posted code (please UseCodeTags), only guesses are possible.
  •  
    devin kinh
    Greenhorn
    Posts: 4
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Bear Bibeault wrote:Welcome to the Ranch.

    Two things:

  • You didn't state your motivation for re-inenting the wheel. Without a reason, the best advice is to not re-invent the wheel.
  • Without posted code (please UseCodeTags), only guesses are possible.

  • The assignment calls for it. It's to help us better understand algorithms I believe.
    Here's how I've implemented it so far :


    It's tricky because the arrays can come in different sizes and the numbers are positive and negative.
     
    Rancher
    Posts: 43081
    77
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    the arrays can come in different sizes


    Is that a requirement? I've worked with MPI (multi precision integer) libraries that fix the number of digits - that reduces complexity and increases speed considerably.

    and the numbers are positive and negative.


    Since you're using decimal arithmetic, it's relatively easy to get around this. Create a temporary array where each index holds the sum for the two digits to add at that index. For each of the two digits you're using its negative if the respective number had a minus sign. Then you "normalize" the result by moving all digits into the 0-9 range -starting from the last digit- and incrementing/decrementing the next higher digit depending on whether you had to add or subtract 10 from the lower digit. This approach makes implementing subtraction trivial once you've implemented addition.
     
    Sheriff
    Posts: 17644
    300
    Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I take it that you'd like to do something like this:

    If the assignment is meant to help you better understand algorithms, what's your algorithm for addition that you're supposed to better understand by trying to implement it? Or are you supposed to come up with your own algorithm?

    One thing I can say right off the bat is that your choice of variables names doesn't help in making your code readable. Readable code is easier to understand and it goes a long way to make your thought process flow better. Bad names like aIILength and bIILength are like speed bumps in your head when you read them. Go ahead, try reading your code OUT LOUD and see if what you hear yourself saying makes a lot of sense.

    When a class is designed like this, it's best to think in terms of this and the other instance. For example, the "standard" way to implement you own equals method looks something like this:

    See how the choice of names makes reading the code OUT LOUD much easier and almost conversational?
     
    devin kinh
    Greenhorn
    Posts: 4
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Ulf Dittmer wrote:

    the arrays can come in different sizes


    Is that a requirement? I've worked with MPI (multi precision integer) libraries that fix the number of digits - that reduces complexity and increases speed considerably.


    Well it's not exactly a requirement, but I am working with integers that have digits of different sizes so the arrays automatically initialize with a different number of elements.

    Ulf Dittmer wrote:

    and the numbers are positive and negative.


    Since you're using decimal arithmetic, it's relatively easy to get around this. Create a temporary array where each index holds the sum for the two digits to add at that index. For each of the two digits you're using its negative if the respective number had a minus sign. Then you "normalize" the result by moving all digits into the 0-9 range -starting from the last digit- and incrementing/decrementing the next higher digit depending on whether you had to add or subtract 10 from the lower digit. This approach makes implementing subtraction trivial once you've implemented addition.



    To be honest that's what I thought I was doing
    I'm trying to implement it the same way I would add two integers on paper where I carry the extra one, and use the remainder as the current element of the temporary array starting from the last index.
     
    devin kinh
    Greenhorn
    Posts: 4
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Junilu Lacar wrote:I take it that you'd like to do something like this:

    If the assignment is meant to help you better understand algorithms, what's your algorithm for addition that you're supposed to better understand by trying to implement it? Or are you supposed to come up with your own algorithm?


    yes that is pretty much what I'm trying to do, and yeah we have to make our own algorithm

    Junilu Lacar wrote:
    One thing I can say right off the bat is that your choice of variables names doesn't help in making your code readable. Readable code is easier to understand and it goes a long way to make your thought process flow better. Bad names like aIILength and bIILength are like speed bumps in your head when you read them. Go ahead, try reading your code OUT LOUD and see if what you hear yourself saying makes a lot of sense.

    When a class is designed like this, it's best to think in terms of this and the other instance. For example, the "standard" way to implement you own equals method looks something like this:

    See how the choice of names makes reading the code OUT LOUD much easier and almost conversational?


    Yeah sorry, I understand, this is the first time I'm posting to a forum so I don't normally have people reading my code other than my prof, and he doesn't really grade for readability.I'll make sure to make more sensible variable names if I plan on getting help for my code.
     
    Junilu Lacar
    Sheriff
    Posts: 17644
    300
    Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    devin kinh wrote:I'll make sure to make more sensible variable names if I plan on getting help for my code.


    Thanks but this is really as much for you as it is for others. I often stop programming for a few minutes so I can figure out a good name to give a class, variable, or method with my programming partner(s). Good names are important because they help you keep the concepts straight in your head. Programming is already a difficult task as it is without you throwing yourself and others for a loop with poorly chosen names that don't match the concepts that they represent. Good names reveal the code's intent. In contrast, poorly chosen names hide the code's intent or worse, mislead you.
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    reply
      Bookmark Topic Watch Topic
    • New Topic