Comment by edrxty
4 years ago
Yet another pre-processor-to-victory post. Check the header in the source.
If we're going to use crazy header files, I want to see someone get the linux kernel to build and boot while including this: https://gist.github.com/aras-p/6224951
> #define M_PI 3.2f
A long time ago, a friend who was studying mathematics at the time, approached me laughing hysterically and showed me a page in an "intro to C"-style book. It showed an example of how one would write a "get circumference of a circle" function. At the top of the code, there was a #define for the value of pi.
The text describing the code said something like this about why pi is #defined and not included directly in the expression:
"We define pi as a constant for two reasons: 1) it makes the expressions using it more readable 2) should the value of pi ever change, we will only have to change it in one place in the code"
As funny as it sounds, there is still a bit of truth in there, though: code might change from using floats to double for example, so you might want to replace single-precision constants by double-precision constants. Only need to replace a single pi constant in that case. :)
There is a similar joke in the book Learning Perl, 3rd Edition by Randal L. Schwartz and Tom Phoenix. I wrote a post about it here fourteen years ago: https://susam.net/blog/from-perl-to-pi.html . Quoting from the book:
"It's easier to type $pi than 𝜋, especially if you don't have Unicode. And it will be easy to maintain the program in case the value of ever changes."
There is also a comment by Randal Schwartz in the comments section where he credits Tom Phoenix with that particular bit of humour.
Good catch! I might be misremembering this event and it was actually Perl, not C. I see Learning Perl, 3rd Edition was published in 2001, which does put its publication at about the right time in early 00s.
This #define would have almost been required by law in Indiana: https://en.wikipedia.org/wiki/Indiana_Pi_Bill
Add Pi to locale?
Depends if you're using biblical pi
This is just too good. It must be a joke on the author's part :D
Actually I find it pretty clever because pi value may change in a programming context, as can change its precision
6 replies →
That #define is making fun of this:
https://www.forbes.com/sites/kionasmith/2018/02/05/indianas-...
But why is it then 3.2f instead of 3.1? Seeing as 3.14 gets rounded to 3.1. Or is that part of the joke and the reason for why the value nee changing?
Regardless of the actual answer, it can make sense to round it up depending on the use case e.g. if you’re calculating the tolerances for a shaft onto which you place a disk, computing the disk diameter (and thus volume, and weight) to be larger than actual provides a safety margin which rounding down would not. Any additional safety margin added afterwards might not be sufficient in all round-down cases.
Sorry, I should be more clear. This is a line from the header linked by the parent comment that reminded me of this story.
Oh my the defines. What is language and semantics?
"When I use a word," Humpty Dumpty said, in a rather scornful tone, "it means just what I choose it to mean - neither more nor less."
Very ugly, ill-defined, uncodified democracy. No one tells you when you vote, no one counts up the votes, there are no official results.
When you speak or communicate with someone else, you are voting with them. "I vote this word means X, for a poor/crude/coarse agreement of what X is the first place."
And this is propaganda at its finest. Evil doublespeak: say one thing, and it actually is another. Snuck in.
Fantastic.
Related: https://github.com/Droogans/unmaintainable-code
I don't quite understand these lines:
I was under the impression it was "#define CNAME value" – what does it mean when there is no value? A trip to Google didn't turn up anything for me, so I'm wondering if a C master can weigh in. Thanks!
It defines those to a value of "", effectively stripping them from the source code.
The most common place where this sort of thing occurs is the common idiom
which allows you to sprinkle debug() calls through your code and have them disappear if you compile without defining the macro DEBUG.
Of course for your quoted use it's more common to see:
This forces you to use debug() as a statement. If no value is defined you could omit the semicolon on a debug() statement and it still compiles.
Can I do it with an #ifndef?