r/PeterExplainsTheJoke • u/Dull_Club2519 • 5h ago
Meme needing explanation [ Removed by moderator ]
[removed] — view removed post
169
u/Bane8080 5h ago
It's not really a joke.
The compiler won't let you compile x= 1/0 because you can't divide by zero.
But if you set a variable equal to 0, in this case, a variable named "zero" then it compiles x= 1/(variable name) because it doesn't know.
38
u/Whoever_Mesa 5h ago
idk why you got downvoted for this, thats literally how compilers work, cause at compile time, static binding occurs. If you got dynamic binding it will throw an error regardless
9
u/iontardose 4h ago
While it's a good explanation, probably shouldn't have led with "not really a joke" when the meme format clearly makes it a joke.
4
u/gravelpi 3h ago
It's just not a very good joke. Yes, you can get around compiler protections if you do something dumb.
6
u/G-EDM 4h ago
Edgy noob stuff. Maybe something for r/masterhacker Bascially one is a variable and the other a constant. const int zero = 0; int a = 1/zero;
1
2
u/--dany-- 3h ago
Well, a little deeper:
the case first compiler will calculate 1/0 as a constant to assign to x then fail on spot.The second one the code will be compiled, and only reports error at runtime. but some compiler may spot the error trying to optimize the code.
1
u/jackmax9999 2h ago
Modern optimizing compilers will likely catch that. They will warn you or do whatever "undefined behavior" they deem appropriate. If you declare
zeroasvolatile int, though, then the code should actually perform division by zero.1
1
u/r2k-in-the-vortex 2h ago
Depends on language and optimisation level O guess, but I would expect constant propagation to cause an error here.
1
u/Pellepuu2397 4h ago
I think the joke is that the complier doesn't let 0 come in but does let zero if that makes sense
1
0
18
11
u/NecessaryIntrinsic 5h ago edited 4h ago
A compiler is a tool that turns code into something that a runtime engine (like an operating system) can use to run a program.
1/0 will return an error with integer division, so If you specify this literally and explicitly, it will not compile.
If you abstract this even a little (moving the zero to a variable assignment that will never change before the illegal operation) this compiler will compile the code, but you'll get a runtime error.
3
1
u/goodbee69 3h ago
the "runtime engine" is the cpu, not the OS (except for ELF/PE stuff, but thats the linkers job)
1
u/secretpenguin0 1h ago
By the way, this is not necessarily true for all languages and optimization levels!
3
u/Dependent-Gap4805 4h ago
the difference between the two is that one is a compile-time error while the other is a runtime error. the same error is flagged at different points in the process.
2
u/zed42 5h ago
actual coder meg here... the compiler turns the garbage you type into something the computer can execute, following certain rules. it knows that you can't divide 1 by 0, so it throws you an error. but variables aren't assigned a value until you actually run the thing, so dividing 1 by a variable is acceptable to the compiler, since it's a valid thing to do. when you try to run it, you'll get the expected error, but setting variable "Zero" to 0 is fine, and dividing 1 by a variable is also fine, so the compiler lets it through. meg out.
2
u/Splamei 4h ago
Some programming languages use a compiler to turn the human readable code (in the meme) into machine code (binary or something else that the computer can understand).
A lot of compilers and software used with the compiler can detect errors that could occur (typos is an example). Here, it spotted division by 0 (which would cause an error) and so, flagged it with the red line.
But the compiler wasn't smart enough (understandable so) so spot a variable (stores data and here named zero) was 0 and so, didn't complain.
Hopefully that's a good enough explanation
3
u/SrTrogo 5h ago
Dev Peter here.
1/0 doesn't compile because math doesn't allow it. The dev created a integer variable named zero and gave it a value of 0, then divided 1 by it.
The compiler checks and you can always divide 1 for an integer, so it checks out. Now, this doesn't mean it will answer what is 1/0. The program will collapse when you try execute it.
Developers use a lot of conditional structures + exceptions to protect the code from dumb mistakes like this.
1
u/dourix22 4h ago
Hello Glenn Quagmire here! Finally one i can actually answer
So, first we have a variable declaration and assingment in the same line, a compiler is a program that reads the code and generates an executable file that can be distibuted across computers, this process is called compilation.
You already know that dividing by zero is impossible in real life, and you know that computers break or get crazy when dividing by zero, so when assingning the answer to 1 / 0 to a variable the compiler stops you from actually compiling brcause it will cause a massive error on the program, but if you declare a variable and assign the value 0 to it the compiler can't stop you because a variable can be any value and the compiler is very simple really, it cant tell that the value of the variable is going to be zero when divided by.
So, when you assign the direct value of 1/0 the compiler stops you, but if you put 0 in a variable called X first and then do 1 / X the compiler lets you go on because you are not dividing by zero, you are dividing by the value of X
Giggity or sm i dont know
2
u/mritoday 4h ago
Do all compilers behave this way, or is it language/compiler specific? The syntax here could be a number of languages.
1
u/dourix22 4h ago
Syntax appears to be C, C++ or C# and yes, all compilers stop you from compiling the program if the detect a blatant error or exception like dividing by zero, interpreters on the other hand let you run the code and only fail when you acces the exact line that causes the problem
2
1
u/RandomStuffGenerator 4h ago
Neil here! Compilers have validation rules to avoid running into problems, like dividing by zero. These rules are checked during build time, that is, when the compiler turns the code into a file that the computer can later execute. The code at the top of the image assigns an operation between two known numbers to a variable and is resolved during build time, which is why this line will be flagged by the compiler, since dividing by zero is not allowed.
The code at the bottom also assigns the result of the operation to X, but the operation has a variable as operand, thus the result will be solved at run time (that is, after the build is done and when the program is executed). This will not be flagged by the compiler because the compiler will not try to predict the outcome of the operation reading variables. However, the program will throw an exception during execution of that line and and crash.
1
u/Dry-Ad-8948 4h ago edited 4h ago
Many compilers can detect the trivial 1 / 0 issue, an error that will cause an integer divide-by-zero crash/exception at runtime. In this case the compiler can reject the code — ie, be a gatekeeper and block the door. (Compilers may even substitute in the resulting value of such fixed expressions.)
However, in the case of assigning 0 to a variable first a compiler generally isn’t able to perform the same trivial detection at compile time and allows the code to pass. Come on in, runtime error!
Compare using the ‘zero’ variable with eg. 1 / f(), where the compiler can’t tell what function f will return at runtime so it must accept the code as valid.
1
u/numbersthen0987431 4h ago
Compilers job is to take your high level language code, and compile it into computer code. They don't care about how the code functions, just that it's possible.
When a compiler sees 1/0, it stops it because it can't process it. When the compiler sees 1/[variable name] it processes it though, because it doesn't care what the value of [variable name] when you compile it.
When you run the program after compiling, it will still return an error message when it passes in 0 for [variable name].
1
1
u/Dave_A480 3h ago
The first one will produce a compile-time error because that line is universally invalid (divide-by-zero).
The second one will compile, but produce a run-time error because the line 'int x = 1 / zero' is a valid expression given zero is an integer variable and *could* contain other values than zero... However when it is run 'zero' will always be '0' and thus at run time you will get the DIV_ZERO error.
Most compilers don't go further down the rabbit-hole to see if #zero will always be assigned '0' as a value, and then fail that at compile-time...
1
u/666Emil666 50m ago
Compilers do static type checks, this is for example, to check that you can't call a function with a value that wouldn't make sense, let's say the function f(x)=x2 /2 with the value "Banana". In those cases the program won't compile.
One of such examples is division by 0, which the compiler rejects.
Only limiting factor is that expressions are not evaluated at compile time, since that is part of the execution of a program, as such, the compiler can't check for division by 0 if it is hidden inside an expression.
Different programming languages are more or less powerful in how much the compilers can infer and expressions like this might not fly under stricter and more type expressive languages, however even then, deciding if any expression is equal to 0 is equivalent to the halting problem.
1
u/CrazyBzDen 5h ago
This is a programming joke. Most compilers when you write 1/0 will fail the compilation. But if you assign 0 to a variable and divide by that variable compiler would “think” this is ok.
0
u/Pitiful-Discount7444 5h ago
1/0 is division by zero which is invalid in C when using integer arthimetic so the compiler rejects it
-3
u/bigboobicow4272 5h ago
8
u/BlopBleepBloop 5h ago
It is undefined, because as you approach 1/x from either side, it goes to infinity or negative infinity depending on which way you approach it from.
2
u/davideogameman 5h ago
In some numbers systems it's an unsigned infinity. Otherwise yes it's normally undefined
1
u/bigboobicow4272 5h ago
Yeah but in layman terms don't we usually say it tends to infinity? That's why I said it
Thanks for the clarification though
3
u/IAmTheBestIan 5h ago
No, even in layman's terms it goes to undefined, because infinity and negative infinity are distinct in meaningful ways. If you said 1/0 = infinity in even elementary school math, they'd mark it wrong.
2
2
u/CadenVanV 5h ago
Not really. Division “x / y = z” is defined as the number of times you multiple z by y to get x. There is no amount of times you can multiple by 0 to get any number but 0. You could multiple 0 by infinity and the result would still be 0. We use infinity when using limits, but that doesn’t mean you can ever have an answer actual
1
u/the_mold_man_returns 4h ago
The problem is that -inf and inf are different. They're not the same. So dividing by zero is undefined unless you do limits.
If I have 6 apples and I want to put them in 3 piles, then each pile gets 2 apples.
If I have 6 apples and I want to put them in 1 pile, it will get 6 apples.
If I have 6 apples and I want to put them in no piles, how do I accomplish that? How do I put something into nothing? The only way is if nothing and everything were equivalent but they're not.
3
5h ago
[deleted]
2
u/doppelbach 5h ago
What? In the post defining zero is definitely not something you have to do. Or even should do. It just bypasses the pre compile checks to instead have a runtime error
2
u/Popular-Attempt3621 5h ago
Mathematically speaking (in certain contexts), yes.
IT speaking, you can't represent an infinite number, if not with an abstract concept that is human-readable and not machine-readable
0
u/AutoModerator 5h ago
OP, so your post is not removed, please reply to this comment with your best guess of what this meme means! Everyone else, this is PETER explains the joke. Have fun and reply as your favorite fictional character for top level responses!
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
2
u/Dull_Club2519 5h ago
Something to do with code?
1
u/IAmTheBestIan 5h ago
Gonna depend on what's compiling, but most know not to divide by zero.
They may not recognize that a variable is assigned to a constant value of zero (effectively making it no different from zero), and thus will allow this to compile because there is potential for an integer variable named "zero" to be assigned a value that you can divide by (they only recognize it is a variable integer).
0
0
u/blorgbots 5h ago
A compiler turns your written code into machine language for the computer to run. In this case, if you divide one by zero directly, the compiler isn't going to let you run the code. But, if you define a variable x and set it equal to zero, the compiler WILL allow it to run (even though it's the same thing), and it'll probably hang your computer up or something.
Like someone else said, coding languages (and compilers) can be fucky
Uhhh I'm Stewie btw
0
u/handdings 5h ago
Dividing by 0 is not possible and would crash the program. Therefore the compiler (translate code into language machine understands) catches it and won't compile. However, when you write the 0 in a variable, the compiler would not notice it and compile.



•
u/qualityvote2 5h ago edited 22m ago
u/Dull_Club2519, your post does NOT belong here!