Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Confusion with a multiplication loop...

 
D diller
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I finally have my program nearly running, but am having some problems with getting my multiplication to work properly:

I think it's a problem with carrying, and it throws my numbers off quite a bit...sometimes. Smaller numbers, where you don't have to carry or only have to carry a few places, it's no problem. But for some reason once you get into carrying quite a bit, it's a huge problem. It's difficult to debug it because it requires going through some LONG multiplication. So I am hoping somebody will be able to spot something in my code that I can't.

Thanks!

(For example: 123*456 yields 41310108 when it should be 56088 - that's a pretty significant 'carrying' error!)
 
Campbell Ritchie
Sheriff
Pie
Posts: 49458
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you remember how you were taught long multiplication at the age of 8 at school? That should give you the algorithm to follow.
 
Paul Yule
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your ten's place variable is named "high" so why are you possibly adding it twice. I would rethink this logic:



You are saying if the ten's place is greater than 1 add that value to a certain place in the answer array. Then...always add that value to an additional place in the array? I suspect this isn't what you meant to do.

Lastly, each index inside your array should not have values greater than 10 because each one of them is supposed to be representative of 1 position in a normally represented number. I suspect that when you are adding your ones place and tens place number to answer it goes above 10 and when this happens you are not carrying. So when you add high and low to your array I would check in case this number goes above 9 redo your carry logic.

For example if you have 9 9 9 inside your answer array and you add 1 to the value at index 2 you currently are not checking for values > 9 so it would appear 9 9 10 when you printed it instead of checking.
 
D diller
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah it was something along those lines exactly, just figured it out actually. Thanks for the tips!
 
Campbell Ritchie
Sheriff
Pie
Posts: 49458
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done
 
salvin francis
Bartender
Posts: 1280
10
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey i am intrested in this solution too

I had once made a division algo using strings, it works so well i could even deploy it as a j2me project
into my mobile phone !!

surely multiplication is an added advantage.

btw a (very very) unoptimal method is "additive multiplication"
eg to calculate 5*3, calculate 5+5+5,

This will not be optimal when it comes to thousands and ten thousands,
But, I dont know why (I have nothing to support my theory),
I think the trade off will not be as much as we expect it to be.

After all, there are middle levels of multiplications involved AND middle levels of additions (with carries) involved for every digit in the multiplier

Just a thought ......


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic