• P (unregistered)

    What would be the problem if input is negative? It should work just as fine.

    At least they aren't doing it like

    var sign = input / Math.abs(input);
    return sign < 0 ? Math.abs(input) : sign * Math.abs(input);
    
  • (nodebb)

    Before coffee.... negate(-2) == -2 - (-2*2) = -2 - (-4) = -2 + 4 = 2.....

  • (nodebb)

    Let me guess - there will be a Part II tomorrow: someone noticed that the result are not exactly the same (because René accidentally fixed the rounding errors), so it had to be quickly reverted back in a production hotfix.

  • (nodebb)

    It's a really good idea to write out exponentiation correctly, because at the moment that I read the article, it said that this method causes problems when the number is greater than two hundred and fifty two, which is epic nonsense. (It's for that reason, combined with the epic conflict between Markdown and the various exponentiation operators and the conflict between ^ as exponentiation and ^ as XOR, that I favour writing it as pow(2.0,52.0)...)

  • (nodebb)

    Also, re inflation. The worst episode of hyperinflation so far (Hungarian pengő between August 1945 and July 1946) got as far as pow(10.0,29.0) from end to end, so it's definitely worth considering pow(2.0,52.0) which is "only" around pow(10.0,17.0)

  • Anon (unregistered)

    Multiplying a double precision number by two won't be a problem until the expoent overflows, that is, until ~10^308 there is not loss of precision at all.

  • (author) in reply to Steve_The_Cynic

    Bah, our stylesheet clears the sup tag.

  • Mr Bits (unregistered)

    return value - value - value;

    Avoids the overflow problem.

  • Scott (unregistered)

    This strikes me as related to home-grown date functions. Instead of using the built-in multiplicative inverse (* -1), let's invent some new way.

    Or, we could use time-tested functionality that already exists...Good for Rene to fix that up.

  • (nodebb) in reply to Scott

    built-in multiplicative inverse (* -1)

    Er, no. Negating a thing gives the additive inverse. The multiplicative inverse of x is 1/x.

  • (nodebb) in reply to Anon

    Multiplying a double precision number by two won't be a problem until the expoent overflows, that is, until ~10^308 there is not loss of precision at all.

    Depends on whether the FPU / software FP / whatever keeps spare bits around during the subtraction. If it doesn't, you may find the case where the original mantissa ends in 1 to be a problem.

  • Mystery Guest (unregistered)

    We are currently having issues as the values coming in from our Indonesian office in Rupiah are too large for our old finance system, so...

  • (nodebb) in reply to Mr Bits
    return value - value - value;
    Depends - if the operation proceeds right -to- left then you still have to hold 2*value somewhere.
  • (nodebb)

    What if the OS in use can't express the value '2' exactly in its floating point routine? (2.000000000000000000016936)

  • Brian Boorman (google) in reply to cellocgw
    return value - value - value;
    Depends - if the operation proceeds right -to- left then you still have to hold 2*value somewhere.

    return (value - value) - value);

    I'm a subscriber to the explicit order of operations school instead of relying on language-dependent implicit ordering. Makes the intent absolutely clear and no gotchas when you have to use multiple languages.

  • Truism (unregistered)

    Man, they really doubled up on the negativity, didn't they?

  • Nathan (unregistered)

    Who the heck refers to negation as 'reversal'?

  • Sole Purpose Of Visit (unregistered) in reply to Steve_The_Cynic

    I'm still waiting for hyperinflation of the Vietnamese currency. Imagine the global sense of penis envy ...

    As for "Sometimes, finance falls into (a) similar place," I think that's an underestimate. I'd go with "statistical certainty." What's interesting to me, having painstakingly spent a year reducing each and every possible POS format to the essential lexical, syntactic, and even semantic rules, is that it's quite possibly to boil down decades of Business Analyst gibberish into something like 500 A4 pages, with circles and arrows and neat little tables on each one.

    What's even more interesting ... I suppose ... is that the out-sources charged with re-implementing the system chose to take those 500 pages and reinflate them to the original set of total gibberish, but on a different platform.

    Well, I say interesting.

  • Foo AKA Fooo (unregistered) in reply to Nathan

    Someone with no clue of mathematics, obviously. Such people handling our finances is not very reassuring.

  • Hanneman (unregistered) in reply to Foo AKA Fooo

    As he story goes about Jantje meeting his old math teacher. Teacher: Hey Jantje, You had so much troube with math so how are things today? Jantje says: I'm buying things for a dollar and sells them for 10. On those 10% I make a good living.

  • Sole Purpose Of Visit (unregistered) in reply to Remy Porter

    Still better than that horrible Discourse thing you guys adopted a while back.

    Not that I don't appreciate Meta-WTFs. Oh wait ... I didn't.

  • Ray (unregistered)

    Such complicated ways of doing it in JS :)

    Me, personally, I like:

    function negateNumber(value) {
        return -(value);
    }
    

    :)

  • sizer99 (google)

    Using floats instead of decimal for currency is another big WTF here. Of course JavaScript doesn't have a decimal type to use because JS is also a WTF. But you could use a 3rd part library like moneysafe.

  • (nodebb)

    Value * (1 - 2)

  • the g man (unregistered) in reply to Mr Bits

    If they were wanting to avoid overflow, they may as well have cancelled out:

    value - value
    

    and gone with just:

    return -value;
    

    But that was too obvious and difficult for them, after all.

  • (nodebb) in reply to Mystery Guest

    It was fun submitting an expenses claim for over 10,000,000 rupiah a few years back.. when it was worth more .. as it was for a week in a quite nice hotel in Samarinda, East Kalimantan. Wish I had taken a copy of the receipt.

    Addendum 2020-05-13 06:24: That would have blown away using a 32 bit integer .. or somebody trying to store money in a single precision float.

  • shcode (unregistered)

    ... snart enough to know x - 2x = - x, but not smart enough to realize that x * -1, or even 0 - x, achieve the same thing in a much more straightforward way. interesting.

  • The Great Obfuscator (unregistered)

    //Everything you love in one function function negate(a) { a.indexOf("-") < 0 ? a="-" + a : a=a.replace("-",""); return a; }

    SCNR

  • WTFGuy (unregistered)

    Surely the right way to do this looks more like

    /**
     * Reverses a value a number to its negative
     * @param {int} value - The value to be reversed
     * @return {number} The reversed value
     */
    negateNumber(value) {
        return value.toString().substr(0, 1) != "-".toString() ? "-" + value.toString().substr(0, 0 + value.toString().length) : value.toString().substr(1, -(1- value.toString().length));
    }
    

    Sheesh! kids these days!

  • (nodebb)

    The real WTF, and I'm surprised and disappointed nobody else mentioned it, why is this even a function at all?

  • Red Five (unregistered) in reply to Brian Boorman

    But if you're going to do negVal = (val - val) - val, why not just do negVal = 0 - val? Or negVal = val * -1? This last one you could even shortcut, assuming language support, with val *= -1.

  • Fnord (unregistered)

    TRWTF is that JS already has a unary minus.

  • eric bloedow (unregistered)

    this reminds me of an old story about an astronomer who tried to write his own "planetarium" program. his biggest mistake was using a separate FILE for EVERY SINGLE STAR. it worked, but it was terribly slow.

  • Old timer (unregistered) in reply to mike_james

    Nobody should story money in an integer or in a 32 bit float. But treasury valuations in rupiah are rounded to 1000 -- by contract, not just by convention -- dating from when treasury transactions were done in pencil, rather than in excel.

    Also, 'reversal' is what you do when reversing a transaction, and , as noted above, x-(2*x) gave a different value on a pre IEEE 754 fixed-point mini-computer, as used at one time for currency transactions, which caused specifications to be written that way.

  • (nodebb) in reply to Nathan

    I think an accounting person would consider Negation a "Reversal" as in "Reversing an Accounting Entry". Maybe?

Leave a comment on “Don't Negate Me”

Log In or post as a guest

Replying to comment #:

« Return to Article