this post was submitted on 22 Oct 2023
2 points (100.0% liked)

Programmer Humor

32376 readers
1136 users here now

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

founded 5 years ago
MODERATORS
 
top 17 comments
sorted by: hot top controversial new old
[–] lwhjp@lemmy.sdf.org 1 points 1 year ago

TDD

const max12 = (x, y) => {
    if (x === 1 && y === 2) {
        return 2;
    } else if (x === 7 && y === 4) {
        return 7;
    } else {
        return x;
    }
};
#define max(x,y) ( { __auto_type __x = (x); __auto_type __y = (y); __x > __y ? __x : __y; })

GNU C. Also works with Clang. Avoids evaluating the arguments multiple times. The optimizer will convert the branch into a conditional move, if it doesn't I'd replace the ternary with the "bit hacker 2" version.

[–] driving_crooner@lemmy.eco.br 0 points 1 year ago* (last edited 1 year ago) (1 children)

Mathematician 3

Max(x, y) = floor(ln(e^x + e^y))

[–] neeeeDanke@feddit.de 0 points 1 year ago* (last edited 1 year ago) (1 children)

so 0.3 ~= 1-ln(2)=max(1-ln(2),1-ln(2)) = floor(ln(2*e^(1-ln(2)))) = floor(ln(2)+(1-ln(2))) = 1 ?

That would bee engeneer 2, not Mathematician3 xD.

Just out of curiostity, what was you Idea behind that?

[–] driving_crooner@lemmy.eco.br 0 points 1 year ago* (last edited 1 year ago) (1 children)

Guess only work with integers, specially for the floor function that is going to give you an integer at the end everytime.

Not my idea, learned it somewhere while doing college in an statistics class. The idea is that the exponential function grow really fast, so small difference on variables become extreme difference on the exponential, then the log function reverse the exponential, but because it grew more for the biggest variable it reverts to the max variable making the other variables the decimal part (this is why you need the floor function). I think is cool because works for any number of variables, unlike mathematician 2 who only work for 2 variables (maybe it can be generalized for more variables but I don't think can be done).

For a min fuction it can be use ceiling(-ln(e^-x + e^-y))

[–] neeeeDanke@feddit.de 0 points 1 year ago* (last edited 1 year ago) (2 children)

to be fair it does seem to work for any two numbers where one is >1. As lim x,y--> inf ln(e^x+e^y) <= lim x,y --> inf ln(2 e^(max(x,y))) = max(x,y) + ln(2).

I think is cool because works for any number of variables

using the same proof as before we can see that: lim,x_i -->inf ln(sum_{i/in I} e^(x_i)) <= ln(|I|) +max{x_i | i /in I}.

So it would only work for at most [base of your log, so e<3 for ln] variables.

[–] driving_crooner@lemmy.eco.br 0 points 1 year ago (1 children)

I don't have a mathematical proof, but doing some experimental tests on excel, using multiple (more than 3) numers and using negative numbers (including only negative numbers) it works perfectly every time.

[–] neeeeDanke@feddit.de 0 points 1 year ago* (last edited 1 year ago)

Try (100,100,100,100,100,101) or 50 ones and a two, should result in 102 and 4 as a max respectively. I tried using less numbers, but the less numbers you use, the higher the values (to be exact less off a deviation(%-difference) between the values, resulting in higher numbers) have to be and wolframAlpha does not like 10^100 values so I stopped trying.

[–] driving_crooner@lemmy.eco.br 0 points 1 year ago (1 children)

After searching a little, I found the name of the function and it's proof: https://en.wikipedia.org/wiki/LogSumExp

[–] neeeeDanke@feddit.de 0 points 1 year ago* (last edited 1 year ago)

thanks for looking it up:).

I do think the upper bound on that page is wrong thought. Incedentally in the article itself only the lower bound is prooven, but in its sources this paper prooves what I did in my comment before as well:

for the upper bound it has max +log(n) . (Section 2, eq 4) This lets us construct an example (see reply to your other comment) to disproove the notion about beeing able to calculate the max for many integers.

[–] kewko@lemdro.id 0 points 1 year ago (1 children)

Mathematician 2 kinda blew my mind, kinda obvious, just can't believe I was never taught or thought about it.

[–] Artyom@lemm.ee 0 points 1 year ago (1 children)

I've been staring at it for 10 minutes and I'm still not convinced it works.

[–] uberrice@feddit.de 0 points 1 year ago

Simple, really. Abs(x-y) is the difference between the two numbers, absolute, so positive value. So, adding abs(x-y) to the smaller of the two numbers turns it into the bigger number. Plus the bigger number, now you have 2 times the bigger number

[–] Fungah@lemmy.world 0 points 1 year ago (1 children)

Reminded of how truly little I know about programming despite the time have spent doing it

Ugh. I'll never be any good.

[–] Theharpyeagle@lemmy.world 1 points 1 year ago

Listen, in industry programming (and for personal projects if you want to get them done), the thief is the way to go. By all means, challenge yourself to understand each of these functions, but 99% of day to day development will not look like this.

[–] SuperIce@lemmy.world 0 points 1 year ago (1 children)

Why would you use anything other than Math.max?

[–] SzethFriendOfNimi@lemmy.world 1 points 1 year ago

Some of us have trust issues. Or worked with Java.

Which, now that I think about it, comes to the same thing.