| « Prev | Page 1 | Page 2 | Page 3 | Next » |
|
What? n * (-1)?
Most modern languages allow negation simply by prefixing the unary minus symbol to the value. int a = 1, b = 3; int c = (-a) * b; Why go through all the work of an explicit multiplication by negative one? I had to correct one of my developers on that point two weeks ago. |
Re: Classic WTF: The Challenges of Negation
2008-12-29 09:11
•
by
draeath
(unregistered)
|
|
More examples of what happens when you pay per line.
|
Re: Classic WTF: The Challenges of Negation
2008-12-29 09:15
•
by
Vertu
(unregistered)
|
|
Why n*(-1), I think 0-n or simply -n would usually be faster and better operation.
|
|
-(Nice Code)
|
Re: Classic WTF: The Challenges of Negation
2008-12-29 09:36
•
by
Voodoo Coder
|
Seems to me that both of those solutions have the same amount of operations in them as n*(-1)...I can't imagine where n*(-1) would be either less clear, or slower, than any other solutions...but then, maybe I missed the tongue being in the cheek on your comment... |
|
To be fair, there is at least one case where simply "-n" won't quite work: maximum-magnitude negative integers in two's complement (e.g., "short i = -32768").
|
Re: Classic WTF: The Challenges of Negation
2008-12-29 09:40
•
by
Zatanix
|
|
I think he wrote "n * (-1)" in the article, to make it really explicit that we are talking about negating numbers. If he had written "When '-n' won't do" the meaning would not have been as clear, imo.
(btw, i really doubt that multiplying by -1 is any more work than using unary minus on any modern compiler. Whether it is more readable depends on the situation, but yeah i agree that usually unary minus is prettier) |
Re: Classic WTF: The Challenges of Negation
2008-12-29 09:43
•
by
Yuliy
(unregistered)
|
|
The second one looks like it takes the long way around taking a bitwise negation within the scope of a byte. The simple way around this is of course "return n ^ 0xFF".
|
Re: Classic WTF: The Challenges of Negation
2008-12-29 09:48
•
by
barvins
(unregistered)
|
|
Multiplication is slow. At least on older processors it used to be much slower than subtraction. Of course, it only matters if you are doing processor-intensive stuff like raycasting.
|
Re: Classic WTF: The Challenges of Negation
2008-12-29 09:49
•
by
barvins
(unregistered)
|
Multiplication is slow. At least on older processors it used to be much slower than subtraction. Of course, it only matters if you are doing processor-intensive stuff like raycasting. (forgot to quote the stuff I was referring to) |
|
I love the first one. It seems like he even got the basis of it - taking advantage of the fact that an undefined variable starts with a value of zero in VB6 - but didn't quite realize it.
|
Re: Classic WTF: The Challenges of Negation
2008-12-29 10:12
•
by
Capt. Obvious
(unregistered)
|
Well, keep in mind we're talking about doubles, not shorts/ints/etc. Thus, the first bit is a sign bit. Most efficent would be n^0x8000000000000000 (At least on a 64-bit machine.) |
Re: Classic WTF: The Challenges of Negation
2008-12-29 10:13
•
by
t604
(unregistered)
|
|
Um... this smells like bullshit. Someone wrote the code just to submit it.
|
Re: Classic WTF: The Challenges of Negation
2008-12-29 10:15
•
by
JamesCurran
|
Actually, the piece at the beginning expanding it to 8 bits will have no effect on the outcome (unless ConvertDecimal requires at least 8 digits) (If strBin is more than 8 characters, the rest will still work). The size independent method of taking the 1s compliment is "return ~n;" |
Re: Classic WTF: The Challenges of Negation
2008-12-29 10:19
•
by
VibroKatana
|
Obviously multiplication will be faster then converting it to a string, doing comparisons and conversions on the like, then parsing it back to a number. With n*-1 (or preferably -n) you should only need 1 pass to get the new value. Shoot the difference between n and -n is usually 1 bit. |
Re: Classic WTF: The Challenges of Negation
2008-12-29 10:21
•
by
JamesCurran
|
That assuming that your language/platform allows bitwise operations on floating point numbers (most do not). It also assumes that your cpu uses as it's internal format, the IEEE floating point *exchange* format (not a guarantee). |
|
Comment * (-1)
(Negative comment) |
Re: Classic WTF: The Challenges of Negation
2008-12-29 10:28
•
by
SpamBot
(unregistered)
|
you must be new here ... </obligatory> |
|
Why is poor nDec being ostracized that way? It's not good enough to be on the line with the others?
|
Re: Classic WTF: The Challenges of Negation
2008-12-29 10:41
•
by
Shriike
(unregistered)
|
|
Wouldn't the second one not event work?
If I had 0 (00000000) it would return -1 (11111111) so obviously he wasn't as stupid as we thought, he wasn't trying to just reverse the sign of a number, he was reversing the sign and then taking -1 Captcha: opto (I enjoyed it) |
Re: Classic WTF: The Challenges of Negation
2008-12-29 10:43
•
by
Brillant Mathman
(unregistered)
|
|
Everyone knows the best and proper way to negate a number is
n = n - 2*n; Nice and clear. |
Re: Classic WTF: The Challenges of Negation
2008-12-29 10:44
•
by
Bernie
(unregistered)
|
Your comment smells like BS. I think you wrote it just to submit... errr... nevermind. Now that my brain is awake, I'd like to introduce the amazingly stupid recursive comment: this comment! |
Re: Classic WTF: The Challenges of Negation
2008-12-29 10:48
•
by
Bob Dole
(unregistered)
|
|
The real WTF is VB6.(Did I do that right?) In VB6, you can't return a value so you have to set the function equal to the value before exiting the function. I hate that BS "Function = Value" syntax. Functions aren't variables, cut that shit out.
\You may hate on VB, but VB .Net made it so you can return, and you don't have the OnError logic crap. VB6 may be the devil. You can hate on VB, but VB6 is the real issue. |
Re: Classic WTF: The Challenges of Negation
2008-12-29 11:06
•
by
Kris
(unregistered)
|
I'm guessing one of the above posters got it right. Whoever wrote that is being paid by the line. |
Re: Classic WTF: The Challenges of Negation
2008-12-29 11:19
•
by
iToad
(unregistered)
|
i = (i ^ 0xFFFFFFFF) + 1; Assuming: - All integers are 32 bits. - Two's complement integer storage. - Programming in C. - Error checking is for wimps. |
Re: Classic WTF: The Challenges of Negation
2008-12-29 11:21
•
by
CrazyBomber
|
|
Here's a neat code obfuscation technique:
int i = 24742; int negative = 2323234; negative =- i; Or is it too easy to spot? :) |
Re: Classic WTF: The Challenges of Negation
2008-12-29 11:39
•
by
DWalker59
|
Shoot, it's not 1 bit, at least not in two's complement form. You'll find that ALL of the bits are flipped, and the sign bit is 1 instead of 0. I think. |
Re: Classic WTF: The Challenges of Negation
2008-12-29 12:10
•
by
sf
(unregistered)
|
Well for one thing negation usually means change of sign, so 0-n would not work unless you knew n was always positive. -1 * n will always work. You are correct about -n, though, if your language supports it. Not all do. |
Re: Classic WTF: The Challenges of Negation
2008-12-29 12:11
•
by
Crash Magnet
(unregistered)
|
n = (~n) + 1; Clear, n'est pas |
Re: Classic WTF: The Challenges of Negation
2008-12-29 12:28
•
by
Dave
(unregistered)
|
0 - -1 = 1, sign changed. 0 - 1 = -1, sign changed. where's the problem? |
Re: Classic WTF: The Challenges of Negation
2008-12-29 12:30
•
by
Dave
(unregistered)
|
In addition, 0 - n is faster for floating point than -1 * n is, in many cases...(premature optimization my ass) |
Re: Classic WTF: The Challenges of Negation
2008-12-29 12:35
•
by
Mitur Binesderti
(unregistered)
|
|
You guys are so negative...
|
Re: Classic WTF: The Challenges of Negation
2008-12-29 12:40
•
by
campkev
|
Oh lord, please tell me you are joking. If you aren't, please tell me you are looking for a different line of work or at least are not anywhere near any code I am going to have to maintain in the future. |
Re: Classic WTF: The Challenges of Negation
2008-12-29 12:49
•
by
Fabien
(unregistered)
|
|
What a great language, it seems that "dblTempValue" is initialized by default with zero. And the author makes sure to make "dblTempValue - dblMagnitude" to confuse the reader
|
Re: Classic WTF: The Challenges of Negation
2008-12-29 12:51
•
by
Kuba
|
I hope it was just a brainfart. 0-(-5) = 0+5 = 5 0-(5) = -5 See it now? |
Re: Classic WTF: The Challenges of Negation
2008-12-29 12:57
•
by
Kuba
|
Any reasonable compiler will reduce 0-n and (-1)*n and -n to a single-opcode floating point negation; with associated load/store *iff* the value is not in a FP register at given point. I have seen a cleverest compiler a while ago, which "knew" that a load/negate/store was slower on a particular architecture than handling it using integer mask and complement instructions. So, if the value couldn't be made to be in FP register (the compiler did instruction reordering), it'd relent and do it using integer opcodes. The difference was small (IIRC 2 clock cycles). |
Re: Classic WTF: The Challenges of Negation
2008-12-29 13:02
•
by
lesle
(unregistered)
|
|
Well, multiplication is slower because it's actually repetitive addition, or at least it was 35 years ago on big iron. Likewise, division is repetitive substraction.
Has that really changed? |
Re: Classic WTF: The Challenges of Negation
2008-12-29 13:14
•
by
clickey McClicker
(unregistered)
|
|
So does 0*-1 = -0 or just plain 0.
|
Re: Classic WTF: The Challenges of Negation
2008-12-29 13:31
•
by
Franz Kafka
(unregistered)
|
Depends on the language. Some allow for a -0 that is !=0, while I think others use it as an init value that can be tested but is never produced by arithmetic. |
Re: Classic WTF: The Challenges of Negation
2008-12-29 13:40
•
by
Brad
(unregistered)
|
|
If I had to guess, I'd say that code was somebody's homework in a programming class.
|
Re: Classic WTF: The Challenges of Negation
2008-12-29 13:44
•
by
KenW
|
Ummm... this smells like bullshit. Someone wrote this comment just to say something. You must be new here. Go back and review other archived posts here. You'll see plenty of examples as bad as these (and some a lot worse). |
Re: Classic WTF: The Challenges of Negation
2008-12-29 13:44
•
by
Duke of New York
(unregistered)
|
It seems reasonable to assume that you've never worked with outsourced code. |
Re: Classic WTF: The Challenges of Negation
2008-12-29 13:53
•
by
mbessey
|
Yes, it really has. Multiplication and division are fixed time, single cycle operations on most CPUs now (really, since about 1990 or so). Unless you're programming an 8-bit microcontroller, it's nearly always faster to do things the obvious way, rather than the "clever" way. On a pipelined, superscalar CPU (i.e. most desktop/server systems), load & store time and cache behavior have much more effect on performance than using multiply vs bit-shifting, for example. |
Re: Classic WTF: The Challenges of Negation
2008-12-29 13:55
•
by
ciph3r
(unregistered)
|
|
You're all wrong! *shakes head*
over-complicating! *mumbles to self* dblVal = val * 2; for(int i=0;i<dblVal;i++) val--; THERE! |
Re: Classic WTF: The Challenges of Negation
2008-12-29 13:58
•
by
Mcoder
|
No other alternative will work here either, mainly because it can't be done. Now, "-n" will create an overflow error, that your program will ignore anyway, since you are not coding in assembly. Also, for the others, there is no speed difference on integer addition and multiplication on consumer-grade hardware since the 486, there is no difference for floating point for a wile either. Also, about floating point, most compilers will turn "-n" into a bit flipping anyway, it will probably also work for "0 - n" and "-1 * n", altough it simply can't deal with very "smart" constructions. |
Re: Classic WTF: The Challenges of Negation
2008-12-29 14:29
•
by
Shmork
(unregistered)
|
|
First rule of programming: no matter how WTF a piece of code is, there will always be someone to defend it.
|
Re: Classic WTF: The Challenges of Negation
2008-12-29 14:43
•
by
harry
(unregistered)
|
Since people complained about this: $ cat negzero.c |
Re: Classic WTF: The Challenges of Negation
2008-12-29 14:45
•
by
Steve H.
(unregistered)
|
Not if you program on small embedded processors that don't have a multiply instruction (or take much longer to multiply than divide). |
Re: Classic WTF: The Challenges of Negation
2008-12-29 14:48
•
by
Dirk Diggler
(unregistered)
|
I thought we weren't supposed to talk about it. |
Re: Classic WTF: The Challenges of Negation
2008-12-29 15:30
•
by
hcs
(unregistered)
|
|
for what little it may be worth:
Some of the comparisons are redundant as the 0 is promoted to 0. anyway. I don't know what how much of this is processor-dependent, compiler-dependent, or specified by the C standard. And this is why I hate floating point. (also boo for bonus newlines) |
| « Prev | Page 1 | Page 2 | Page 3 | Next » |