- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
ugh
Admin
NEGATE_VALUE(1+2) amounts to +1.
Admin
And it is even wrong, or would you expect NEGATE_VALUE(2+2) to be zero?
Admin
Clearly you have never mate full use of the pre-processor with token pasting, substitution and all of the fun stuff. Years ago I wrote entire programs using the pre-processor so that the generated "C" code was nothing more than printf and a value.
Admin
NEGATE_VALUE(-1) returns +1
By the way, negate refers to the act of nullifying or denying something, which is different from what the function means to do.
Admin
#define NEGATE_VALUE(x) ( NEGATE_VALUE(1) * x )
Fixed that for you.
Admin
I like the color of your thoughts.
Admin
Not only is it superfluous by almost any definition, but it's also wrong, in that it should at the very least have parens around the use of
x
, otherwise you end up seeing what @Ulli noted.Admin
There, there. Why are you all so negative about that?
Admin
#define POSITIVE_VALUE(x) (+1 * x)
Addendum 2023-08-28 08:58: #define DO_NOT_NEGATE_VALUE(x) (+1 * x)
Admin
They probably encountered a corner case where -x didn't result in the value they expected.
Admin
Like -0, for instance?
The first thing I thought of when I saw this function was, "Gee, that's exactly how it's done in BASIC!"
Addendum 2023-08-28 09:56: EDIT: I meant macro
Admin
This is wrong. Preprocessor is a text replacement tool. NEGATE_VALUE(1+1) is replaced with ( -1 * 1+1 ) that evaluates to 0...
Admin
More likely
-32768
in 16-bit two's-complement integers. If you negate it, it remains negative, but if you multiply it by the literal constant-1
in an environment whereint
is 32-bit, you get32768
(because the value is promoted toint
before the multiplication).Admin
My first programming job was at a company where there had been an argument about whether to use Turbo Pascal or Microsoft C as the standard environment. The C folks one, but there was one holdout who REALLY wanted Turbo Pascal so he created a file called "legible.h":
#define begin { #define end } #define := = ...
And yes, he used legible.h to make C code look like Turbo Pascal, which was very confusing to people who were new to the team.
Brilliant guy, but the day he left the company we went through and deleted legible.h from the codebase and cleaned everything up.
Admin
There may be different operator overloads for unary minus and binary subtractions operators, which do different things for the objects this is called on. If it's a place trying to "#undef return", who knows what other craziness that C++ allows is also being used.
Admin
Nope. You still need the brackets round the (x).
Admin
I started programming in C in 1982. In C++ in later years. In those days, the internet was called "manuals." Crack open even one and it'll tell you not to do this.
Admin
It's for readability of course. It would be so easy to miss that little "-" minus sign. But no one misses "NEGATE_VALUE".
#define TERNARY_BOOL_TEST_FOLLOWED_BY_TRUE_EXP_THEN_FALSE_EXP(b,t,f) (b) ? (t) : (f)
Now we can use the ternary operator again.
Admin
I'm very negative about CREEP. wikipedia Committee_for_the_Re-Election_of_the_President
Admin
A CDC Cyber used ones complement to negate an integer, so for example 0...010 is 2 and 1...101 is -2. On such a machine 0...000 is 0 and 1...111 is -0.
Admin
The most horror I had to put up with in c++ was using MFC. Now that's a WTF all by itself.
The amusing lack of parens around x in that macro is a good reminder that c/c++ macros will always find a way to bite you and then proceed on chopping your head off in interesting ways.
And if that's not enough, c++ has invented template programming, which is yet another WTF monstrosity defended by a core of fanatics with no moral compass.
Admin
@Ralf: I haven't used C++ specifically much yet, but my primary project language is Fortran. Not having something like templates means not having generic data containers without losing type safety or using weird preprocessor magic. Templates are, at least originally, a solution to allowing generic programming with type safety and without adding the performance overhead of putting everything on the heap by default.
Now, after trying to understand how Boost.Units works, I am convinced that somewhere along the line dark magic has entered the picture. But having the possibility of enforcing correctness of code with regard to physical units without performance overhead -- which for simulation code often would be unacceptable -- is mouth-watering. Sadly, no chance to try it yet :(
Admin
Off: another interesting point is that -MIN_INT cannot be represented as a signed integer (it would be MAX_INT+1). That's why abs(3) should return
unsigned int
instead ofsigned int
. (Never mind, you rarely use MIN_INT, so it is usually okay.)Admin
My guess would be "someone wrote -x and their manager didn't understand what it did, and insisted it be made clearer."
Never had any quite that bad but I did have one who could not get his head around De Morgan's laws. Thankfully, after strenuous enough explanation / insistence on my part, he acquiesced (more out of exhaustion than understanding, I expect, but I'll take it).
Admin
@Ralf - Please read https://en.wikipedia.org/wiki/Modern_C%2B%2B_Design
But do the world a favor. Wear a motorcycle helmet so that when your head explodes, your loved ones have less of a mess to cleanup....(yes, terminal brain explosion has been recorded as a result of attempting to to learn and apply the techniques in this book)
Admin
Negating it also causes it to be promoted though.
Admin
And yet, it was... what?
Admin
There's always operator overloading... just saying.
Admin
Unary minus does integral promotions just fine, so the argument is converted to int just the same as when you multiply it with an int...
Admin
The presence of #undef return means somewhere there is a header file that's #define'ing return. And that is really dam scary.
Admin
Just change it to