• Post Reply Bookmark Topic Watch Topic
  • New Topic

BigInt implementation  RSS feed

 
Marielle Seno
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,

So I have a class of BigInt that will take compareTo, plus, and times method. I have figure out how to do the first two but I am confuse about the times part.

This code is what I have so far.


From what I notice about my code, the carry works fine but it goes on and on... For example when multiplying a digit to the very last digit of larger size it wont add the remainder to the last result... the remainder carries to the second digit that is being multiplied to the second loop.
Another thing is that I don't know how to every result shifting one to the left so it wont align to the first result.

Hopefully someone will see this. Thanks a lot.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marielle Seno wrote:This code is what I have so far.

Marielle,

1. Welcome to JavaRanch.
2. Please UseCodeTags (←click). I've added them for you this time. See how much better it makes your code look?

Winston
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marielle Seno wrote:From what I notice about my code, the carry works fine but it goes on and on... For example when multiplying a digit to the very last digit of larger size it wont add the remainder to the last result... the remainder carries to the second digit that is being multiplied to the second loop.
Another thing is that I don't know how to every result shifting one to the left so it wont align to the first result.

Well, my first advice (and it's always the same in cases like this): StopCoding.

Get out a piece of paper and work out the steps for long multiplication (I presume that's what you're trying to do) exactly, and write them down in English before you write another line of Java code.

One tip: Long multiplication (at least the way you're doing it) requires a lot of addition, so my suggestion would be to get an add() method working first, and then use that to do your additions. I think you'll find you end up with a lot less code.

Winston
 
Stevens Miller
Bartender
Posts: 1444
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am inferring that your definition of BigInt uses an array to store digits. This is, I believe, how the standard library's BigInteger class works. Using your debugger and stepping into its multiply method might be helpful to you.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stevens Miller wrote:This is, I believe, how the standard library's BigInteger class works...

Yeah, except BigInteger "digits" are 32 bits long (ie, base 4294967296).

Winston
 
Stevens Miller
Bartender
Posts: 1444
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What, you don't find that base helpful in your daily work?

Yeah, it might not be the best template, but I don't believe in re-inventing the wheel, and it does cope with the same issue(s) I think the OP has.
 
Marielle Seno
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Thanks guys,

You guys definitely reply fast on furoms.
Thanks for revising my code I'll do it just like that next time.

Yes I have my plus method working. I also write them down in paper line by line.
I just couldn't figure out how to stop the loop on first iteration so that the last product will bring down the whole sum of the number.
Plus I am not able to use breaks anywhere in my code.

If you guys can give me an idea of how to bring down the very last product of each iteration that would be great and Ill worry about adding them to get the final product of the BigInts.

(My code right now only works when I multiply BigInts by 1-digit then it return the correct product)

Thanks!!!
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marielle Seno wrote:(My code right now only works when I multiply BigInts by 1-digit then it return the correct product)

OK, well, there's a clue.

I'm not going to give you the code, but how about something like this:
For two numbers X and Y, to get X * Y:HIH

Winston

PS: Most of us (certainly me) are happier working with loops that work forward rather than backward - don't ask me why - so you might want to consider storing your digits in "little-endian" order (ie, in reverse sequence). Don't worry about it too much, but it's something to think about.

After all, the class doesn't care which order its digits are in, does it?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!