I’m in this post and I’m offended.

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

deleted by creator

__auto_type is a compiler builtin, not a library function. It’s not a function at all, the parentheses are for precedence & grouping.

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

Thief no doubt

Thief gang. Why stand on shoulders of giants if you’re not using it to your advantage?

GTG3000
link
fedilink
Русский
31Y

Why use const max = (x, y) => x > y ? x : y instead of function max(x, y) { return x > y ? x : y } ?

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: https://discourse.julialang.org/t/faster-bernoulli-sampling/35209)

crandlecan
link
fedilink
51Y

404 or walled

Joe Cool
link
fedilink
21Y

works here

Yoink.

Actually I’ve probably been all of these at various times in my career.

Mathematician 3

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

@neeeeDanke@feddit.de
link
fedilink
1
edit-2
1Y

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?

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
link
fedilink
2
edit-2
1Y

to be fair it does seem to work for any two numbers where one is >1. As lim x,y–> inf ln(ex+ey) <= 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.

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

@neeeeDanke@feddit.de
link
fedilink
2
edit-2
1Y

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.

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.

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
link
fedilink
2
edit-2
1Y

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.

maegul (he/they)
link
fedilink
English
8
edit-2
1Y

Otherwise, realistically, I’m prob the worst of all worlds … the procrastinator waiting/hoping to be the pair programmer that has hopefully remembered to just be the thief.

@force@lemmy.world
link
fedilink
English
21
edit-2
1Y

wtf kind of cursed programming language is this? JS? it’s so ugly, in no universe should a function look like that

but obviously as a rust enjoyer i have to do it like

fn max ⟨T: PartialOrd + Copy⟩(nums: ⁊[T]) -> Option⟨T⟩ {
    let mut greatest: ⁊T = ⁊nums[0];
    match nums.len() {
        0 => None,
        1 => Some(*greatest),
        _ => {
            for num in nums {
                if num > greatest {
                    greatest = num;
                }
            }
            Some(*greatest)
        }
    }
}

edit: lemmy formatting REALLY hates references and generics it seems… time to go back to medieval times

Isn’t it php?

Wow that’s a very exhausting language. I dropped your code into an online rust to asm converter and it actually wasn’t more! I did try to post it for fun but lemmy kept messing up the code block. Oh well, wasn’t that amusing anyway!

lol that’s not actually how rust is written, it was just a joke

it’d really be written

if x > y { x } else { y }

Hah thanks for clarifying. I was joking too and it’s a shame I couldn’t post the results.

Though I admit i don’t know anything about rust. I’m sure I’d like it better than the proprietary garbage i use now that just gets converted to ASM / PLC code in the end. But I can’t skip the middle man. I’m not gonna try but probably 30mins for me to “write” the above.

Besides, how do you make money if I can code something in an hour as opposed to 2 days?

@force@lemmy.world
link
fedilink
3
edit-2
1Y

deleted by creator

No no no you misunderstood me.

I was being honest, I know nothing of rust. I have however used python in embedded systems with positive results. The product didn’t make it but for other reasons.

Funny you mention java, that’s sorta what I’m stuck in but not like you think. Beyond the fact that it’s a bloated nightmare.

I’m just a low-level programmer at heart but I have bills to pay. The rust stuff was all just a joke… i don’t know it but maybe i should. Thanks for the info.

Saying anymore about what I do is just super embarrassing but i promise i meant no ill will. Excuse my frustration, I’m locked into a proprietary system i have no control over. You would laugh your ass off if you saw it. Anyway, i meant no offense, have a good night!

Ah yes, rust. The language that somehow manages to manages to as verbose as possible, with as much jargonized shorthand that a computer could handle.

Exactly, I don’t understand why languages have decided that every keyword needs to be as randomly minified as possible. fn, def, rune(ok that’s not minified, just a dumb name), fmt, std. Many of these things aren’t new, but programmers recognize descriptive variable names are important, the same should be true for keywords.

deleted by creator

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

DumbAceDragon
link
fedilink
English
101Y

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

@aeharding@lemmy.world
link
fedilink
English
241Y

Procrastinator + troll.

return x

Scroll Responsibly
link
fedilink
1
edit-2
1Y

my $max = $x > $y ? $x : $y;

Create a post

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

Rules:

  • Posts must be relevant to programming, programmers, or computer science.
  • No NSFW content.
  • Jokes must be in good taste. No hate speech, bigotry, etc.
  • 1 user online
  • 77 users / day
  • 211 users / week
  • 413 users / month
  • 2.92K users / 6 months
  • 1 subscriber
  • 1.53K Posts
  • 33.8K Comments
  • Modlog