Eufalconimorph,

<span style="color:#323232;">#define max(x,y) ( { __auto_type __x = (x); __auto_type __y = (y); __x > __y ? __x : __y; })
</span>

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.

cpw,

Max11 is all my code. Why doesn’t it work???🤔

lwhjp,
@lwhjp@lemmy.sdf.org avatar

TDD


<span style="color:#323232;">const max12 = (x, y) => {
</span><span style="color:#323232;">    if (x === 1 &amp;&amp; y === 2) {
</span><span style="color:#323232;">        return 2;
</span><span style="color:#323232;">    } else if (x === 7 &amp;&amp; y === 4) {
</span><span style="color:#323232;">        return 7;
</span><span style="color:#323232;">    } else {
</span><span style="color:#323232;">        return x;
</span><span style="color:#323232;">    }
</span><span style="color:#323232;">};
</span>
Windex007,

I use 8, but only when I’m operating on unsigned longs.

canihasaccount,

Where’s the Julia programmer that hits every one of these with @benchmark and then works for six hours to shave three nanoseconds off of the fastest one?

(Example: discourse.julialang.org/t/…/35209)

crandlecan,

404 or walled

joe_cool,

works here

driving_crooner,
@driving_crooner@lemmy.eco.br avatar

Mathematician 3

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

neeeeDanke,

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,
@driving_crooner@lemmy.eco.br avatar

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,

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,
@driving_crooner@lemmy.eco.br avatar

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,

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,
@driving_crooner@lemmy.eco.br avatar

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

neeeeDanke, (edited )

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.

driving_crooner,
@driving_crooner@lemmy.eco.br avatar

I just remembered where I learned about that function, in this course on convex optimization that unfortunately I never had the opportunity to finishing it but is really good.

Number358,
Moxvallix,
@Moxvallix@sopuli.xyz avatar

I’m number 11.

DumbAceDragon,
@DumbAceDragon@sh.itjust.works avatar

Either engineer or bit hacker, depending on whether or not I’m trying to avoid branching.

Snazz,

Bit hacker 2 is really fascinating. It uses a bit mask of all 1s (-1) or all 0s (0) and takes advantage of the fact that y ^ (x ^ y) = x and y ^ 0 = y

wethegreenpeople,

Engineer I guess… Thief is the objectively better enterprise programmer option but I don’t know why I always forget about it and just write a ternary ¯⁠\⁠⁠(⁠ツ⁠)⁠⁠/⁠¯

mindbleach,

JS instills distrust. Rely on built-in sort? It’s alphanumeric. Rely on built-in reverse? It modifies your array. Copy an array? No you didn’t.

Yeah no kidding we bang out a kata in a dozen characters. I don’t even believe in order of operations at this point. I’ve been routinely betrayed.

writeblankspace,

Thief, or Procrastinator.

snowcatridge10,

Thief

marco,
@marco@beehaw.org avatar

And not feeling one byte bad about it :p

Shhalahr,

Procrastinator.

Okay, but seriously: “Thief”. Why reimplement it if it’s already available in the language?

dinckelman,

2, but I’m incredibly embarrassed to say that I’ve had to do 9 before

tigerhawkvok,

9 is objectively better if you have untrusted inputs and need a stable failure mode.

dinckelman,

That was precisely the reason. I prefer strongly typed languages languages, so it just felt gross to write that

  • All
  • Subscribed
  • Moderated
  • Favorites
  • random
  • uselessserver093
  • Food
  • aaaaaaacccccccce
  • [email protected]
  • test
  • CafeMeta
  • testmag
  • MUD
  • RhythmGameZone
  • RSS
  • dabs
  • Socialism
  • KbinCafe
  • TheResearchGuardian
  • Ask_kbincafe
  • oklahoma
  • feritale
  • SuperSentai
  • KamenRider
  • All magazines