Editing 1275: int(pi)
Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.
The edit can be undone.
Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.
Latest revision | Your text | ||
Line 8: | Line 8: | ||
==Explanation== | ==Explanation== | ||
− | This comic | + | This comic tells a simple, deadpan joke: the number "{{w|3 (number)|3}}" is cursed because it is used more than one time at the original equation volume(r)=(4/3)*pi*(r^3) and thus should not be used explicitly in programming. Instead there should be a constant defined like "THREE=3" so THREE could be used at the equation like as volume(r)=(4/THREE)*pi*(r^THREE). For a number "3" this is senseless but for many numbers, often used in a program, it is helpful. Changing the constant "THREE=3" to "THREE=300" would affect all uses of this constant by just a single edit. |
− | + | In lack of this constant [[Randall]] does a workaround, recommending that programmers instead use <code>int(pi)</code>, which means the {{w|integer}} part of {{w|pi}}, without the {{w|fractional part}}. Pi, an {{w|irrational number}}, has a value starting <code>3.14159...</code>, making <code>int(pi)</code> equal to 3. | |
− | + | In the title text, Randall takes the joke a step further, suggesting the usage of {{w|floor and ceiling functions}}: <code>ceiling(pi)</code> would be pi rounded ''up'' to the next integer, which is {{w|4 (number)|4}}; and <code>floor(pi)</code> is pi rounded ''down'' to the next integer, which is 3. (Note that <code>int(n)</code> and <code>floor(n)</code> have the same value when <code>n</code> is greater or equal to zero. For values less than zero, <code>int(n)</code> is equal to <code>ceiling(n)</code>.) | |
− | + | The joke here plays on the fact that basic rules of programming are confusing and novice programmers are often told to simply not do certain things without any explanation (see [[292: goto]]). This includes, in particular, a general proscription against "{{w|Magic number (programming)#Unnamed numerical constants|magic numbers}}" in the code. Replacing all significant magic numbers with named constants makes programs easier to read, understand and maintain. Randall takes this to an extreme by suggesting that certain numbers could be inherently problematic, but the general idea is perfectly believable. | |
− | The | + | The formula in the comic is for the {{w|Sphere#Enclosed_volume|volume of a sphere}}: (4/3)*pi*(r^3). |
− | + | <i>Mmm... Floor pie.</i> is a reference to Homer Simpson. [http://www.youtube.com/watch?v=GnjzmdxTXGQ] | |
See also {{w|Cargo cult programming}}. | See also {{w|Cargo cult programming}}. | ||
==Transcript== | ==Transcript== | ||
− | |||
:volume(r) = (4/int(pi))*pi*r^int(pi) | :volume(r) = (4/int(pi))*pi*r^int(pi) | ||
+ | :Programming Tip: The number "3" is cursed. Avoid it. | ||
− | |||
− | |||
{{comic discussion}} | {{comic discussion}} | ||
[[Category:Programming]] | [[Category:Programming]] | ||
− | [[Category: | + | [[Category:Math]] |
− |