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: that the number "{{w|3 (number)|3}}" is cursed because it is used more than one time and thus should not be used explicitly in programming. Instead there should be a constant defined like "THREE=3" so THREE should be used at the equation. For a number "3" this is senseless but for many numbers, often used in a program, it is helpful. Changing a constant like "DISTANCE=200" to "DISTANCE=300" would affect all later 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>. And the function <code>round(n)</code> will either be equal to <code>floor(n)</code> or <code>ceiling(n)</code>.) | |
− | + | The joke here plays off of the fact that the intricacies of programming are endlessly confusing, and that [[292|novice programmers are often told to simply not do certain things without any explanation]]. This includes, in particular, a general proscription against "{{w|Magic number (programming)#Unnamed numerical constants|magic numbers}}" in the code. A new coder often has no way of knowing when a number can be legitimately written literally, and when it should be introduced as a named constant, or even as a constant expression, such as <code>2^16-1</code> rather than <code>65535</code>. Randall takes this to an extreme here 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]] |
− |