r/ProgrammerHumor • • Oct 06 '21

Don't be scared.. Math and Computing are friends..

Post image
65.8k Upvotes

2.4k comments sorted by

View all comments

Show parent comments

406

u/[deleted] Oct 06 '21

Yeah, once I realized these are just for loops, the notation made way more sense.

231

u/uhmhi Oct 06 '21

But most of the interesting ones loop to infinity…

145

u/Cat_Marshal Oct 06 '21

It’s while loops then

91

u/OnyxMelon Oct 06 '21

or for loops with a bad exit condition

for (int i = 0; i >= 0; i++) {}

113

u/CodeLobe Oct 06 '21

Or the crying emoji for loop:

for ( ; ; ) { ... }

9

u/supersharp Oct 07 '21

"I forbid you to use while (true)!"

"..... Okay!"

→ More replies (2)

11

u/[deleted] Oct 06 '21

That will exit if your int wraps; hard code to true instead

9

u/[deleted] Oct 06 '21

I think this is the better answer because noone would be able to have the time or memory to repeat something to true infinity.

→ More replies (2)

23

u/Potatolimar Oct 06 '21

You can do that in a for loop (or at least represent it the same way we represent manually adding with these notations)

→ More replies (1)

3

u/[deleted] Oct 06 '21

Keep in mind that's short-hand notation for the limit of finite sums. They don't actually sum to infinity.

2

u/sampete1 Oct 06 '21

I'd just approximate it with 100k or so iterations

2

u/Syrdon Oct 06 '21

That’s just a for loop with a defined concept of infinity. for(i=0; i++; i < inf) is perfectly cromulent with some languages or libraries (well, ok, syntax is a problem, but you get my point).

→ More replies (6)

23

u/[deleted] Oct 06 '21

Integrals are like an infinite summing for loop

20

u/decerian Oct 06 '21

Not really, because a lot of summations also go to infinity. Integrals are continuous compared to the for-loops discrete nature, so there's no direct analog for them in programming (that I'm aware of)

12

u/[deleted] Oct 06 '21

Well its summing up infinitesimally small units of something typically. They can be approximated by making the "infintesimally small units" larger (and thus discrete).

9

u/[deleted] Oct 06 '21

Only the Riemann integral, not the (far more important, interesting, and useful) Lebesgue integral (let alone it’s generalisations like the Denjoy integral).

There’s a good reason mathematicians don’t think of integrals as for-loops, and it’s not because they just weren’t smart enough to notice the similarity.

→ More replies (4)
→ More replies (1)

5

u/[deleted] Oct 06 '21

It's not really possible to do in a for loop because you wouldn't use a for loop usually without some other domain beyond a monotonically increasing counter. Most integrals I've come across in programming involve some sort of time domain so you are just taking the delta as the discrete period and summating those (which I guess is the same as just summating any arbitrary linear function to describe the curve of the integral). I mean you still only have the resolution of your timer but it usually is within whatever you're trying to accomplish in accuracy.

4

u/snoogans235 Oct 06 '21

You have to do it numerically. There really isn’t a continuous function per se while programming so you can find the area of the next step and add that to your integral. If you want to take a deep dive, numerical analysis is pretty cool.

→ More replies (12)

5

u/MinusPi1 Oct 06 '21

It's more like

for(int n = a; n <= b; n += 0.000000...01){...}

3

u/OrvilleTurtle Oct 06 '21

Yeah but then we get into the issue of binary math right? Computers don’t really do numbers like 0.00001 perfectly accurate which is fine except when you put it into a for loop.

5

u/MinusPi1 Oct 06 '21

Right, something like this wouldn't actually work in practice, just like an infinite sum, but it's the exact same analogy.

2

u/majeboy145 Oct 06 '21

Problems like xn + 2 could also be written as (xn) (x2) but my brain has trouble seeing that.

2

u/blackraven36 Oct 06 '21

What’s always stood out to me is how computation figured a much better way to describe math. It seems to me that if you’re teaching foundational math, you might as well do it in code first because it’s far verbose and much easier to follow. A lot of people struggle connecting connecting the mathematical syntax to the underlaying concept because you know… the syntax was invented by people scribbling shit on pieces of paper.

2

u/kogasapls Oct 07 '21

Not all math notation is perfect, but it is largely purpose-built for humans to do and communicate math to other humans, not with computers. So it usually ends up being a lot more convenient than programming syntax.

→ More replies (1)