Marielle Seno

Greenhorn

Posts: 2

posted 4 years ago

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.

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.

posted 4 years ago

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

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

"Leadership is nature's way of removing morons from the productive flow" - Dogbert

Articles by Winston can be found here

posted 4 years ago

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

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

Winston

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

Articles by Winston can be found here

posted 4 years ago

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."Il y a peu de choses qui me soient impossibles..."

posted 4 years ago

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

Winston

- 1

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

Articles by Winston can be found here

Marielle Seno

Greenhorn

Posts: 2

posted 4 years ago

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!!!

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!!!

posted 4 years ago

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?

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?

Articles by Winston can be found here