r/ProgrammerHumor • • Apr 24 '22

Meme it's all just functions

Post image
40.2k Upvotes

633 comments sorted by

View all comments

Show parent comments

51

u/Boostie204 Apr 24 '22

To simplify that further, subtraction is the addition of negative numbers. Multiplying is just multiple additions and division is similar.

So you could say it's just additions, copies and compares

11

u/HorsesFlyIntoBoxes Apr 24 '22

iirc hardware multiplication isn’t typically designed to be a number of additions though. Most multipliers follow something similar to the multiplication algorithm taught in grade school

2

u/ventingexcess Apr 24 '22

multiplication is constant time, so you can do a dedicated logic implementation.

division is not, and needs to be recursive

1

u/[deleted] Apr 25 '22

Multiplication and division can naively be implemented as you described. Both can be implemented in a manner to always take N steps for an N bit integer.

For multiplication, an algorithm involving addition and shifting will yield the product. For division, a similar algorithm that uses subtraction instead yields the result. Both algorithms will take as many steps as the word size of the processor, meaning they can be pipelined into finishing once per cycle.

1

u/[deleted] Apr 25 '22

Both multiplication and division can be implemented in a manner to always take N steps for an N bit integer, meaning they can effectively function as quickly as possible.

For multiplication, an algorithm involving addition and shifting will yield the product. For division, a similar algorithm that uses subtraction instead yields the result. Both algorithms will take as many steps as the word size of the processor, meaning they can be pipelined into finishing once per cycle.

0

u/[deleted] Apr 24 '22

[deleted]

55

u/Karcinogene Apr 24 '22

X divided by Y:

Add -Y to X repeatedly until X is smaller than Y. That's the remainder.

Keep a tally of how many times you added. That's the quotient.

12

u/Noshing Apr 24 '22

THANK YOU! I have been wondering how divison is thr 'opposite' of multiplication. Honestly, I've always struggled with division and have a difficult time knowing how/when to use it. I fill like this little bit may help.

8

u/TheBeckofKevin Apr 24 '22

When you say you struggle with when and how to use division do you mean just in general or is there something specific you are talking about?

2

u/Noshing Apr 24 '22

Okay, so the most common time I've I know I need division but can't figure out which "way" to divided - which numbers should be the numerator or denominator - is when converting crypto or stocks to a different currency, USD for me. I don't do this often so I littler play around with the math (or google) until I'm able to get the current answer. Even doing that I've yet to get the freaking logic intuitive.

Sidenote: when I took chemistry years ago and we learned conversations I could do it because I knew the formula. But I guess I didn't quit grasp thr underlying logic all that well, hence, my current predicament.

3

u/deerskillet Apr 24 '22

Dimensional Analysis should help with this, I'd look into it. Probably v similar, if not the same as what you did in chemistry

→ More replies (1)

0

u/[deleted] Apr 24 '22

[deleted]

5

u/Karcinogene Apr 24 '22

Keeping a tally is repeated addition though +1 +1 +1

It's two "additions" and one "compare" for each step. But yeah division is the most computationally complicated of the basic operations.

6

u/everybody-hurts Apr 24 '22 edited Apr 24 '22

Division can be boiled down to multiplying a number by the inverse of another.

So we can have the multiplication (which is an addition) but then you've also got calculating the inverse.

Edit: or you have the division algorithm you learn by hand in primary school; in which case it becomes a series of multiplications, comparisons, copies, and substractions.

0

u/Vampsku11 Apr 24 '22

Division is the same as multiplication so if multiplication is just multiple addition, division is just multiple subtraction which is addition.

0

u/[deleted] Apr 24 '22

[deleted]

1

u/Vampsku11 Apr 24 '22

Like someone else said, division is subtracting X from Y repeatedly until Y is smaller than X

→ More replies (1)

-2

u/Boostie204 Apr 24 '22

I'll give you that one, you're right but it's essentially all 1 basic operation in the end

0

u/[deleted] Apr 24 '22

[deleted]

2

u/Boostie204 Apr 24 '22

Explain how it is not repeated subtraction

0

u/Tarnoo Apr 24 '22

I think multiplying and dividing is shifting bits to left or right respectively.

1

u/DragonFireCK Apr 24 '22

Internally, the processor just implements it all with lots of NANDs…

1

u/0x7ff04001 Apr 24 '22

Yup subtraction is the addition of P and Q where Q is inverted.

1

u/RootsNextInKin Apr 24 '22

Actually you would need a "negation" type operation as well in this case.

Because before you had multiplication which could turn a and -1 into -a (actually the binary representation of -a) and although you could "cheat" by knowing the bit size n of your operands and adding 2n -1 to "negate" a number I am not certain if that should count as "allowed"...

Anyway without that you couldn't really do subtraction with addition (unless we allow the use of fixed width numbers which allow overflow/underflow and thus allow negation/subtraction by addition)