• (nodebb)

    ugh

  • Ulli (unregistered)

    NEGATE_VALUE(1+2) amounts to +1.

  • chg (unregistered)

    And it is even wrong, or would you expect NEGATE_VALUE(2+2) to be zero?

  • TheCPUWizard (unregistered)

    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.

  • Chop Chop (unregistered)

    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.

  • (nodebb)

    #define NEGATE_VALUE(x) ( NEGATE_VALUE(1) * x )

    Fixed that for you.

  • LZ79LRU (unregistered) in reply to TheCPUWizard

    I like the color of your thoughts.

  • (nodebb)

    #define NEGATE_VALUE(x) ( -1 * x )

    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.

  • Sauron (unregistered)

    There, there. Why are you all so negative about that?

  • (nodebb)

    #define POSITIVE_VALUE(x) (+1 * x)

    Addendum 2023-08-28 08:58: #define DO_NOT_NEGATE_VALUE(x) (+1 * x)

  • Andrew (unregistered)

    They probably encountered a corner case where -x didn't result in the value they expected.

  • (nodebb) in reply to Andrew

    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

  • (nodebb)

    This is wrong. Preprocessor is a text replacement tool. NEGATE_VALUE(1+1) is replaced with ( -1 * 1+1 ) that evaluates to 0...

  • (nodebb) in reply to Bananafish

    Like -0, for instance?

    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 where int is 32-bit, you get 32768 (because the value is promoted to int before the multiplication).

  • DB5 (unregistered)

    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.

  • (nodebb)

    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.

  • Tim Ward (unregistered) in reply to Domin Abbus

    Nope. You still need the brackets round the (x).

  • Argle (unregistered)

    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.

  • iWantToKeepAnon (unregistered)

    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.

  • (nodebb)

    I'm very negative about CREEP. wikipedia Committee_for_the_Re-Election_of_the_President

  • Erwin (unregistered) in reply to Bananafish

    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.

  • (nodebb)

    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.

  • (nodebb) in reply to Ralf

    @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 :(

  • Lőrinczy, Zsigmond (github)

    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 of signed int. (Never mind, you rarely use MIN_INT, so it is usually okay.)

  • (nodebb)

    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).

  • TheCPUWizard (unregistered)

    @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)

  • Entropy (unregistered) in reply to Steve_The_Cynic

    Negating it also causes it to be promoted though.

  • matt (unregistered)

    And yet, it was... what?

  • (nodebb)

    There's always operator overloading... just saying.

  • (unregistered) in reply to Steve_The_Cynic

    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...

  • oh no (unregistered)

    The presence of #undef return means somewhere there is a header file that's #define'ing return. And that is really dam scary.

  • Gnasher729 (unregistered) in reply to oh no

    Just change it to

    #ifdef return
    #error return is defined 
    #undef return
    #endif
    

Leave a comment on “Negative Creep”

Log In or post as a guest

Replying to comment #:

« Return to Article