this post was submitted on 05 Dec 2024
232 points (93.3% liked)

Science Memes

16950 readers
2377 users here now

Welcome to c/science_memes @ Mander.xyz!

A place for majestic STEMLORD peacocking, as well as memes about the realities of working in a lab.



Rules

  1. Don't throw mud. Behave like an intellectual and remember the human.
  2. Keep it rooted (on topic).
  3. No spam.
  4. Infographics welcome, get schooled.

This is a science community. We use the Dawkins definition of meme.



Research Committee

Other Mander Communities

Science and Research

Biology and Life Sciences

Physical Sciences

Humanities and Social Sciences

Practical and Applied Sciences

Memes

Miscellaneous

founded 2 years ago
MODERATORS
 
all 44 comments
sorted by: hot top controversial new old
[–] juliebean@lemm.ee 64 points 10 months ago (5 children)

what would be the alternative? to always execute if the condition is true, but sometimes execute it even when false, for funsies?

[–] ptz@dubvee.org 40 points 10 months ago* (last edited 10 months ago) (3 children)

Hey, I like a little chaos in my codebase πŸ˜†

const funsies = () => (Math.round(Math.random() * 1000) % 2 == 0)
if ( condition || funsies() ) {
    // do the thing
}
[–] sorrybookbroke@sh.itjust.works 6 points 10 months ago* (last edited 10 months ago) (1 children)

Wait, why the the *1000 or mod 2? Won't that give a 50\50 chance or the same as Math.round(Math.random).

No shade, and I may be wrong myself I am very tired

[–] mexicancartel@lemmy.dbzer0.com 2 points 10 months ago

Probably to make the fractional random value between 0 and 1 to become an integer so that you can divisibility check for even with mod 2

[–] modality@lemmy.myserv.one 3 points 10 months ago (1 children)
[–] ptz@dubvee.org 19 points 10 months ago* (last edited 10 months ago)

Nope. I'm staunchly against "AI". If the code sucks, it's because I wrote bad code.

Edit: Oh, or did you mean is that how "AI" works?

[–] bstix@feddit.dk 18 points 10 months ago (1 children)

The "only" part implies exclusivity, which may be false, because other things might run the code anyway.

IF "I can see the sun" THEN "It's day."

Nothing wrong about that. However if we make it exclusive:

IF AND ONLY IF "I can see the sun" THEN "It's day."

That's obviously wrong. I can actually not keep the day away by sitting with closed eyes in my mothers basement with the curtains shut.

"Only if" might make sense in a legal contract, but there's no way a piece of code can stop other pieces of code from calling the same functions.

[–] adj16@lemmy.world 2 points 10 months ago (1 children)

But that’s not how if statements in code work. So what you’ve said isn’t wrong, but the premise of this meme is completely off

[–] bstix@feddit.dk 2 points 10 months ago* (last edited 10 months ago)

Yes sure. Code is logical stepwise. By including the "only if" it implies that other stuff is taken into account, which it isn't at that moment in code.

I mean, I don't need to extend the implications of an IF statement. It already does exactly what it says.

Anyway fuzzy logic does exist for people who want "sometimes if". It's useful in certain cases. I've only ever considered it in music production, where things very often get to the point of complexity where it makes a (sometimes) useful difference.

[–] General_Effort@lemmy.world 7 points 10 months ago

In normal parlance, "if and only if" rules out that something could also happen as a result of other circumstances. EG, if you fall out of a plane, you will lose your glasses. But there are other conditions that would lead to the same result.

In code, the alternative would be to have a different if statement that executes identical code. Or *cough* ~you could use a jump statement to execute literally the same code.~

[–] unmagical@lemmy.ml 1 points 10 months ago

Brb, making a truly "if" statement function in my products code base for funsies.

[–] raoul@lemmy.sdf.org 59 points 10 months ago (2 children)

The boolean operator 'If and only if' do not have a relation with the program instruction 'if'.

The programatic 'if' is a jump, not a boolean operator. It do not have truth table.

In logic:, if and iff can be seen like functions taking two booleans and returning a boolean

  • 'if a then b' (noted a -> b): return true if a is false or b is true. Example: 'if I eat pizza then I fart' This is true even if I fart all the time (if b is true, we do not care about the value of a) as long as I fart when eating pizza (if a is true, b must be also true)

  • 'a <-> b' is equivalent to 'a -> b and b -> a': the two should be true at the same time. I can only fart will eating pizza and cannot fart otherwise.

[–] Hoimo@ani.social 5 points 10 months ago (2 children)

So in programming, you'd write 'if' as:
not pizza or fart where the farting is irrelevant until the pizza is involved.

While 'iff' would be:
pizza equals fart where pizza means fart and no pizza means no fart.

I actually wrote iff as (not pizza and not fart) or (pizza and fart) before, and I'm pretty sure that's the way I wrote an iff in production code in the past, but your comment made me realize that "they should be true at the same time" can be tested really easily with equality.

[–] Venator@lemmy.nz 1 points 10 months ago

I don't love the pizza fart variable naming convention, but it's better than foobar and I don't have a better suggestion πŸ˜…

[–] captain_aggravated@sh.itjust.works 1 points 10 months ago (1 children)

If not pizza and not fart: pass

[–] problematicPanther@lemmy.world 1 points 10 months ago

If pizza then fart else !fart

[–] sik0fewl@lemmy.ca 11 points 10 months ago

I hope this memer is not a programmer or logician, but ideally neither.

[–] eunieisthebus@feddit.org 7 points 10 months ago

Waiting for a programming language with iff Syntax

[–] stevedice@sh.itjust.works 7 points 10 months ago* (last edited 10 months ago) (3 children)

No, they're not.

Let's assume they are. Let funky function be defined as:

int funky() {
    a=0
    b=1
    if ( a==1 ) {
        b=1
    }
    return(a)
}

Since a==1 if, and only if, b=1, in particular a==1 if b=1. We have b=1, therefore a==1. It follows funky will always return 1 but... it doesn't. QED.

[–] nickwitha_k@lemmy.sdf.org 13 points 10 months ago* (last edited 10 months ago) (2 children)

I'm pretty sure that funky() would always return 0, as defined. I'll pseudocode that up:

funky takes no args, returns int {
  a is assigned the value 0
  b is assigned the value 1

  test if a is equal to 1, if it is {
    b is assigned the value 1
  }

  return a
}

The if in your function can never be reached, without some weird manipulation of the value of a that breaks variable scoping in most syntaxes.

I think that I see your logic but it is syntactically incorrect:

    if ( a==1 ) {
        b=1
    }

In most syntaxes, this is a conditional execution and value assignment. That is, the code in curly braces only gets executed, if the conditional evaluates as true. If the conditional evaluates as true, the code is executed, assigning the value 1 to the variable b.

It does NOT imply that the assignment of the value 1 to the variable b is a conditional requiring the assignment of the value 1 to the variable b.

Remember: = in most programming is NOT an equality symbol but a value-assigment symbol. It would be nice if people creating the initial syntaxes used something else that is harder to confuse but they didn't.

[–] ikilledlaurapalmer@lemmy.world 9 points 10 months ago

Yeah, I’m not sure what the original intent was here. If we’re missing something I’d like to know

[–] stevedice@sh.itjust.works 4 points 10 months ago* (last edited 10 months ago) (1 children)

Yes, I know, that's the point. Funky is specifically constructed to always return 0. Then we assume "if" and "if, and only if" are equivalent and by following that assumption to its logical conclusion, we deduce that funky returns 1. Therefore, our assumption was incorrect because 0β‰ 1. It follows that "if" isn't equivalent to "if, and only if". Also, it's just a shitpost.

[–] nickwitha_k@lemmy.sdf.org 2 points 10 months ago (1 children)

If reading the code as non-programming logic, that conclusion makes sense, yes. However, if, in most syntaxes, is a type of flow control. What it wraps has no meaning to the if statement itself. Reading it through the lens of an interpreter/compiler makes it clear. The statement is approximately:

If and only if a is equal to 1, do the thing {
  The thing is: assign the variable b with the value 1
}

To one not familiar with how programs are executed, it would make sense that the return value could be 1. But understanding how flow control works in programming, makes this interpretation a challenge.

[–] stevedice@sh.itjust.works 4 points 10 months ago (1 children)

I don't think you're picking up what I'm putting down. I'm not arguing that the return value can be 1, I'm well aware that it can't β€” I wrote the function so that it will always return 0. It only returns 1 if we make an incorrect assumption (and mix up semantics with formal logic, but that's another conversation), the incorrect assumption being "if is equivalent to if, and only if"

[–] nickwitha_k@lemmy.sdf.org 2 points 10 months ago (1 children)

Sorry! I sometimes get carried away on correctness.

[–] stevedice@sh.itjust.works 2 points 10 months ago* (last edited 10 months ago)

I mean, making an assumption and arriving to a contradiction is as correct as a proof gets.

[–] dohpaz42@lemmy.world 5 points 10 months ago

An if statement with one condition is an if and only if statement. The moment you add a second (or more) condition (regardless of && or ||), then it’s no longer if and only if.

[–] Hammocks4All@lemmy.ml 5 points 10 months ago

If and only if… haha unless…

[–] kbal@fedia.io 4 points 10 months ago (1 children)
    if ( test == false )   
        x = 1
    else
        x != 1
[–] dohpaz42@lemmy.world 4 points 10 months ago (1 children)

Not sure what you’re trying to achieve with that else block, as it affects nothing.

[–] kbal@fedia.io 5 points 10 months ago

okay I fixed it

    if ( test == false )
        x = 1
    else
        assert( x != 1 )
[–] GammaGames@beehaw.org 3 points 10 months ago
[–] reallykindasorta@slrpnk.net 3 points 10 months ago

IFF you use the universal quantifier

[–] BreadOven@lemmy.world 2 points 10 months ago

Don't you just use iif?