- 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
> That assumes that 0 != -0.0
Bad assumption, fluffy
Admin
WTF! Oh come on now. Someone just made this up. I find it hard to believe anyone could be this stupid. Plus it's already a "double" so your compiler should be dealing with the 1's vs 2's compliment.
I've maintained code for years and I have seen some bad code but this, Oh Sh*t it's probably real code. scream
I'm going to go try to think up some way to profit from all the stupidity on this planet. Good day all.
Admin
Why not simply converting the double value to binary then to string and checking whether the first character is a '1'?
LOL
Admin
The one's complement of -1 is 11111110 on an 8 bit machine.
Admin
People, if -0.0 == 0 evaluates to TRUE, then -0.0 is NOT negative. A minus sign does not make a number negative; being strictly smaller than zero does (for real numbers).
As to Dave M's post about n < 0: FALSE is what the program should print. -0.0 may be a legitimate representation of zero, but it is not negative. If you want to treat -0.0 as negative, then you are stretching the definition of 'negative' quite a bit.
Admin
I like reading code like this. Whenever I think the code will create an unneeded temporary object, either implicitly or explicitly, I take a swig from a flask that I keep in a drawer at my desk.
Or, that I used to keep there, anyay. It got emptied.
.B ekiM
Admin
SR, floating point numbers != real numbers. (negative(r) <-> r<0) =/=> (negative(fp) <-> fp<0)
Hmmkay? </nit>
Admin
I wonder if there could be a possible flaw in this solution. Isn't converting a number to a string dependant on the format settings defined in your system? What if you've defined a numerical format where the sign is actually put at the end of the number instead of the front? Thus -1 would convert to "1-"...
Very rare flaw, of course, if it would even occur. I think this ToString function is smart enough to ignore whatever format the user prefers and just uses the default format. [;)]
Admin
Funny, that's exactly why I avoided that major. Ah, well; we live, we learn.... :)
Admin
Off the topic but I had a friend of mine who was in a beginning programming class and the instructor was discussing global variables. After a lengthily discussion on this topic another student chimed in and asked if they could explain it since he felt as if he had a complete grasp on the concept. What he said was astoundingly idiotic to say the least. Ready???? <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
"So if I create a global variable in my application someone in <?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" /><st1:country-region w:st="on"><st1:place w:st="on">China</st1:place></st1:country-region> can see it." I don't think I need to say anymore.<o:p></o:p>
Admin
You should have told them 'yes'. It's BS, of course, but at least they would have learned not to use globals. ;)
Admin
Curious. Many things about this.
First, 1's complement representation of -1 is 11111110, not 10000001. That would be Sign Bit representation.
Second, literal integers have nothing to do with their underlying representation. If the compiler doesn't convert the literal in the code correctly to the machine-specific representation, it's broken. Use a different compiler. If you compiled it for a different machine - let's just say that I severely doubt that there are or will be two CPU architectures with the same instruction set (making the code compiled for one run on the other) and different negative number representations.
Somehow that makes me doubt the existence of this article.
Admin
In Java you can do this:
Admin
A factor 10, according to Facts and Fallacies of Software Engineering by Robert L. Glass
http://www.amazon.com/gp/product/0321117425/102-5616837-5488934?v=glance&n=283155
"Something didn't quite work out ... " "- CAPTCHA Validation Incorrect"... WTF? LOL ;)
Admin
Well, it depends.... if you consider mathematical e (where if you take a number grater than 0 and small as you want, e is lesser than this number.... it is infinetly small) 0+e is represented as 0+ and 0-e is represented as 0-.
So we have:
0+ = +0.00000..... . . . .....0001
0- = -0.00000..... . . . .....0001
If you consider only the first decimal (keeping the value) you have
0+ = +0.0
0- = -0.0
So 0.0 > -0.0
OK, I'm already calling the asylum.:D
Samuele
Admin
Well, many, many moons ago I wrote a short program and shook up every teacher in the department.
10 X = 3000000
20 X = X + 1
30 IF X < X + 1 GOTO 20
40 PRINT X
*EVERY* teacher insisted this was an infinite loop. (The 3 million seed value is simply to make it terminate in a reasonable length of time.)
(This came up because the previous semester one teacher had given a final exam problem that had two logical approaches, one of which would fail for precision reasons--and there hadn't been a bit of discussion in class about precision issues.)
Admin
OH!
Now that I quoted it. The code makes sense. Before, you couldnt see the " 'less than' X + 1 GOTO 20" part of it. ;) Makes sense now that there is some kind of loop there .;)
Admin
And he wrote it wrong: in one's complement the representation would be 11111110. And the 10000001 he issued is also an existing notation, but it's called 'signed magnitude'.
Admin
I'd say that only if the representation has a notion of positive and negative zero, it would matter. If a number is stored as exponent plus a fractional 2s complement signed integer mantissa, there's no such thing as negative zero. There's only one zero, and it's commonly agreed upon that it's positive :)
Admin
I asked Google about Ken Arnold, and he seems to be someone who should know better. Is this article on the internet somewhere? If so, please post a link because I would like to read it in its entirety.
Thanks
Admin
There is an old COBOL technique called "overpunched signed fields."
When there wasn't room for -1234, you could use 123F instead. Where ABCDEFGHI in the last digit represented 0123456789 respectively, as well as indicated a negative number overall.
Admin
Speed can be negative the same way that currency can be... When you are subtracting it... Negative currency means amounts that you owe someone,
Negative speed means a chunk of speed that you "owe" - or will subtract in a calculation.
On a more philosophical note, that's what ALL negative numbers mean... They are just shortcut symbology to represent subtraction. In reality there is no such thing as negative... As
Admin
Speed can be negative the same way that currency can be... When you are subtracting it... Negative currency means amounts that you owe someone,
Negative speed means a chunk of speed that you "owe" - or will subtract in a calculation.
On a more philosophical note, that's what ALL negative numbers mean... They are just shortcut symbology to represent subtraction. In reality there is no such thing as negative... As to
Admin
Sorry, accidently hit enter...
Cont... As to Velocity, it is a misnomer to say it can be negative. No vector can be negative. A Vector is an ordered pair of scalars (actually any other objects, I guess), and only the constituent elements can be "negative". Taking the negative of a vector implies reversing the sign of all of it's constituent elements, some of which may be positive and some of which may be negative. Describing the vector as a whole as either positive or negative doesn't really make sense or have any meaning. The absolute value of a vector can be a scalar, but it of course is always positive.
Admin
A 2D vector can be positive or negative. 3D vectors are a different story. However, scalars cannot be negative, since that would make them a vector. I guess.
Admin
I don't believe there is any more intrinsic mprecision in floats / doubles than there is in integral values. The imprecision is just of a different kind. Both have the capability to exactly represent any one of a set of numbers. (2 to the N for an N-Bit variable). They are both absolutely accurate at representing the numbers they are designed to represent, and imprecise at representing any of the other numbers which lie between those on the number line.
No Integer variable can represent 0.5 exactly . No decimal number can represent one third exactly, although it can represent .000001221 exactly. A IEEE float or double can represent any number which is a sum of positive and negative powers of 2 - exactly, with no imprecision. But that still leaves a gap between every adjacent pair of numbers... just like all the other schemes do. It's just that with floats and doubles, the integers end up in thoses gaps.
And whatever the type of representation being used, integral, decimal, or foloat/double, zero can be represented exactly - there is never any imprecision for zero.
Admin
2D vectors cannot be positive / negative. No vector can be definitely evaluated as positive or negative.
Here ar four 2D vectors... Which ones are positive, and which ones are negative?
(2, -5)
(-4, 8)
(1, -3)
(5, -7)
Admin
there is no imprecision in the floating point number 0.0 but there could be imprecision in how 0 was reached. for instance. what do you think the following program will output? run it and you will notice 0 != 0 for sufficiently large (or small) values of 0.
public static void main(String[] args) {
double num = 1;
for (int i = 0 ; i < 10; i++) {
num -= (1.0/10.0);
}
if (num < 0) {
System.out.println("Holy shit 0 is less than 0");
}
if (num > 0) {
System.out.println("Holy shit 0 is more than 0");
}
if (num == 0) {
System.out.println("Thank god 0 equals 0");
}
}
Admin
I agree that the imprecision is not in zero, butI disagree that is in the process... Actually it is in the values of 0.1 through 1.0 that were used. those values cannot be represented exactly as a a IEEE double/float. This exactly analogous to the following, (in the opposite direction).
<FONT size=2></FONT><FONT color=#0000ff size=2>double</FONT><FONT size=2> x = 0x034;
</FONT>d</FONT><FONT color=#0000ff size=2>ouble</FONT><FONT size=2> y1 = 0x0D0;
</FONT><FONT color=#0000ff size=2>double</FONT><FONT size=2> z = 0x000008;
</FONT><FONT color=#0000ff size=2>double</FONT><FONT size=2> ExactIncr = (x / y1); </FONT><FONT color=#008000 size=2>// Is exact cause it's power of 2
</FONT><FONT color=#0000ff size=2>double</FONT><FONT size=2> total = z * ExactIncr;
</FONT><FONT color=#0000ff size=2>for</FONT><FONT size=2> (</FONT><FONT color=#0000ff size=2>int</FONT><FONT size=2> i = 0; i < 8; i++)
total -= ExactIncr;
</FONT><FONT color=#008080 size=2>Console</FONT><FONT size=2>.WriteLine(</FONT><FONT color=#800000 size=2>"Total = {0}"</FONT><FONT size=2>, total); </FONT><FONT color=#008000 size=2>// Prints exactly zero
</FONT><FONT color=#008000 size=2>// --------------------------
</FONT><FONT color=#0000ff size=2>double</FONT><FONT size=2> y2 = 0x0D3;
</FONT><FONT color=#0000ff size=2>double</FONT><FONT size=2> InExactIncr = (x / y2); </FONT><FONT color=#008000 size=2>// Is NOT exact cause not power of 2
</FONT><FONT size=2>total = z * InExactIncr;
</FONT><FONT color=#0000ff size=2>for</FONT><FONT size=2> (</FONT><FONT color=#0000ff size=2>int</FONT><FONT size=2> i = 0; i < 8; i++)
total -= InExactIncr;
</FONT><FONT color=#008080 size=2>Console</FONT><FONT size=2>.WriteLine(</FONT><FONT color=#800000 size=2>"Total = {0}"</FONT><FONT size=2>, total); </FONT><FONT color=#008000 size=2>// Prints slightly off zero...
</FONT><FONT color=#008000 size=2>// --------------------------
</FONT><FONT color=#008080 size=2>Console</FONT><FONT size=2>.Write(</FONT><FONT color=#800000 size=2>"Hit any key top exit"</FONT><FONT size=2>);
</FONT><FONT color=#008080 size=2>Console</FONT><FONT size=2>.ReadLine();
Admin
Won't happen. Signed floating point numbers have (duh), a sign bit. Simple as pie to test that bit, and thus determine whether or not the entire thing is negative, tiny as hell or not.
Admin
All of those are neither
Heres Another
(-1,-5)
I think that ones negative overall though im not certain
Admin
There is no such thing as a negative vector - it doesn't make sense. Only reals (read: numbers in |R) can be negative. A number is negative if it is less than 0. Since there is no ordering in |R x |R (or any vector space except |R) you can't say that a vector is less than another vector, thus there is no negative vector (by extension).
And while I re-read the posts here, I see that despite the absolute, überstupidity of this WTF, people still try to find a valid reason for it. There is no valid reason for such kind of code, unless you really want to scare your coworkers. The real solution, however, imply using either IsTrue() or IsFalse() (maybe both) and perhaps the original author wanted to avoid it by using this ugly hack.
Regards,
E.
Admin
Then there is the embedded programming approach. Figure out the largest positive number that will be encountered, and anything greater than that is a negative number.
Admin
I've thought about sending in a wtf for exactly this code! I had a programmer propose just such a method for determining a number is negative. Luckily we stopped that one before it got to code form. I can't believe there is more than one person that would come up with this 'solution'.
Admin
I dunno....
I have a SQL Server 2005 table, with a Decimal column, with a ZERO value in the first row.
When I export it to Excel, I get 0.0000000001
Try as I might, I can't figure out the math.
Admin
.... let me clarify how the zero got there: the Default value of the column was 0.
Admin
Maybe it was scheduled for the April edition, not August, but the underlying representation of the calendar was different.