# Confusion with a multiplication loop...

D diller

Greenhorn

Posts: 29

posted 7 years ago

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

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

Posts: 49776

69

Paul Yule

Ranch Hand

Posts: 229

posted 7 years ago

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.

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

posted 7 years ago

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

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

I agree. Here's the better link: Salvin.in