- 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
Ehh... Mathematicians are the jealous ones! We CS majors get all the girls! Right? Erhh... right? Maybe not.
Admin
Yeah, cuz we know how to hack all the pr0n sitez!
Admin
Well, it doesn´t take a math guru also to see that the code is, besides crappy, wrong : it returns "even" when the number is odd (and vice versa).
Admin
Have you considered what would happen if index was bigger than MAX_INT? Or if 2 were bigger than MAX_INT? Clearly not. Luckily BigInteger is here to save your bacon!
Admin
Admin
Why make it simple when we can complicate?
+1 That one actually made me laugh out loud (luckily, I have an office of my own).But the existence of this function doesn't mean that it's in a business logic layer. It could very well be a Bean, or a static ArrayListToHtml() method.
Admin
I am working on a quite huge scientific project, where the underlying data structures were modified to use BigInts recently. Now I am happy to work with code snippets where I know that the greatest number ever occuring is TWO. Unfortunately BigInteger has no constant TWO :)
PS: BigInts make much sense in other parts of the project using the very same data structures.
Admin
I have absolutely no idea what could possibly go through the mind of the coder who typed this.
Would you not, while your typing slows down with each key you press, grow a marginally more ill and green look on your face as you plodded though the quote, the 2, the closing-quote and finished, heavily panting plus throbbing headache, with a closing parenthesis? Like so:
Big.. Integer..
...
...
ergh
...
(reaching for key)
...TWO
...
AUGH
phew
done
There. It is done. Created: an aircraft hangar. Inside: a matchbox.
Admin
If int index is bigger than MAX_INT then I think we are one step closer to TARDIS technology!
Admin
Admin
Bigger is better, I thought? Why do arithmetic with a measly 32 bits when 64 bits are just sitting there waiting for an opportunity to prove themselves?
Actually - and I recognize the sarcasm in the topic post but some others may not - there are legitimate uses for BigInt/Int64. We're rapidly approaching the point where a few of our tables will have more than 2147483648 rows, Decimal types are a waste of space in an identity key, and a GUID isn't appropriate in this case. Bigint is a small performance hit and should last a few centuries.
That's a pretty narrow case, though. I'm almost positive that Int64 is never used anywhere else in the entire system.
Admin
Admin
Yes, there is the "what if index is larger than MAX_INT" situation. I thought of that at first, until I re-read the code:
And, in addition to the return being a string of "even" or "odd", the modulo result is converted to a string as well.Admin
How about:
Big (snicker) Integer (snort) ... ... (shaking uncontrolably, face turning red from holding breath) ... ... TWO ... (laughs maniacally, scaring boss' children who were visiting and causing devout atheist coworkers to look for a church).
Admin
I'm surprised that there have been this many comments without someone pointing out that
if ((x%2)==0) is the same as if ((x&1)==0)
but the latter is marginally faster. Less readable (arguably), and bound to make no significant difference at all, but that doesn't usually stop the comments :)
Admin
But what about the people with 2147483649 messages in their inbox?
Admin
That is a thing I see frequently in VB.NET simply because on data type naming (int = Integer; BigInteger = Long).
This is most often a misuse brought over from VB6 programmers who didn't pay attention to changes in data types between VB6 and VB.NET as Integer in VB6 was 16bits and is now 32bits in .NET, but Long which was the 32 bits in VB6 is now 64bits. So some people might think they use 32bits with Long data types.
Admin
Hmm, it's less readable, and it's the kind of simple optimization the compiler or the JVM can do? I'll definitely go change all my code, right now!
Admin
Admin
Admin
Although one client did ask explicitly for raw data totalling a few million rows - can't imagine what they intend to do with it.
Admin
except index is passed in as a int in the params
Admin
Omg... that was great...
Admin
... that everywhere it's used looks something like this:
... = (rowClass(index) == "even")?0:1;
Admin
Admin
um, ok... why not...
while(results){ /* some code with RowClass */ if(RowClass.value == "odd") RowClass.value = "even"; else RowClass.value = "odd"; } ... instead?
Admin
Admin
or a one-line...
while(results){ /* some code with RowClass */ RowClass.value = RowClass.value == "odd"?"even":"odd"; }
Admin
Admin
As Daniel mentioned BigInteger can actually contain any number, its even used for 512bit numbers.
Admin
But, it looks like we are dealing with Java, so ints are always 32 bits, no matter what the underlying architecture.
If I remember right, even in C they have to be at least 7 bits.
Admin
(*) Sorry for being so picky...
Admin
Admin
Maybe his editor used a really large font.
Admin
Admin
Admin
I've seen a lot of people defend big int but they seem to miss a little problem.
The parameter into this function is an int, it's never going to be able to get any number that's close to needing big int.
Captcha: Yummy, like my bagel.
Admin
Err, I think that usually a big integer has a maximum of 2^32 bits, rather than 64 or "arbitrarily many"
Admin
Wait a minute.... when I first started out, I would frequently have to sift through mountains of printed hex dumps. I even used to read wave forms on an oscilloscope to read memory contents (breadboarding). Great, now I feel old.
Admin
Admin
No. It was implemented with a signed short.
Admin
I'm tempted to go draw a three foot tall number "two" on the whiteboard over there. When anyone asks, I'll tel them "It's my BigInt!"
Admin
Admin
One of the neatest features that sold me on Python is the way it lets you work with integers of any size:
Admin
WTF... this produces exactly the wrong answer every time!
That function could be added to the Paula Bean...
Admin
A couple points of information. Hard Disk drive size and the limit of seeking into a file to and passed 2GB (2^32) around about 1990. In those 17 years, there have been enough rounds of this coming to and going by a limit ala Y2k and the silliness of last week's daylight savings that you'd think the hive mindset would be smarter about these kinds of issues.
When 2^64 isn't enough, something written today will still be around.
BTW, what is the largest Usenet News newsgroup number? There was a time it was 32k then 64k.
Admin
Intervention. Those people need an intervention.
Admin
A beautiful mental image, although perhaps not quite horrifying enough.
On a straight line, using a line 2mm long for the "2", and a mere 64bit number for our BigInt...
According to Google calculator, (2^64) millimeters = 123308.761 Astronomical Units = 1.94986403 lightyears (and probably even more teaspoons)
Working with a real 3d matchbox 25mL in volume... the aircraft hangar should have a volume of... (25 mL) * .5 * (2^64) = 230 584.301 cubic km
The mind boggles
(disclaimer - this math is probably very, very wrong)
Admin
Bwahahah, I should probably submit this as a full WTF on its own, it's so bad. (Except it's a student project, so not so surprising.)
I and a friend had a CS theory class where we had to write a bigint class to do RSA encryption - we couldn't use the native bigint in whatever language we chose. I originally wrote mine in C, but as the deadline neared and my memory usage exploded, I realized it would be faster to rewrite in java than to fix the memory leaks. I ended up with a pretty good bigint class that would do math calculations on 32 bit chunks, calculating overflows and everything.
I had been keeping tabs on my friends progress, but I didn't quite pay enough attention to understand what he was up to until the very end. I forget what language he used (java also I guess), but apparently since the TAs said they'd only be testing 512 bit keys, he was statically allocating 1024 bits (to hold multiplications of two 512 bit numbers). So aside from not being an arbitrary sized bigint, it used the same memory whether it is was holding 2 or 2e200.
It was only after the due date that he explained, to my great horror, that he was using 32 bit integers to hold his bits.
1024 32 bit ints to hold each individual 1 or 0 ...
Admin