- 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
Let's see...
1024 Bytes = "1K"
2048 Bytes = "2K"
That works, so the algorithm must be right!
Very funny.
Admin
See size printed on your harddrive and you will see that 200GB=200,000,000,000B with equals 186GB.
So it's quite normal to do calculation like this!
Admin
I like the Hungarian notation as well. The length of the string is obviously the same as the string, just with different types... no, wait: "numSize" is the size of the string, as a number, and "strSize" is the size of the disk, as a string... Good thing the programmer used Hungarian notation, or the code would have failed. :-P
Admin
heh...186 GB mesaured in <a href="http://www.dansdata.com/gz008.htm">marketing terminology</a>. We get the same problem with bandwidth measurements. What moron decided to originally start measuring bandwidth in bits (baud rate/kilobits/megabits). I hate trying to explain that 1.5 mbps is actually 187.5 KB/s.
Admin
The person who wrote this code is actually right about the fact that KiloBytes are 10^3 Bytes. The notation of 2^10 Bytes is called KibiBytes. [1]
However, this algorithm fails for floating point numbers ('10.10' will give '10KB') and numbers with an exponent ('10E+3' will give '10KB').
Also, a mild WTF for not using a constant for '3' or 'K'. ;)
[1] http://www.pcguide.com/intro/fun/bindec.htm
Admin
I don't see anything wrong with this code. In fact, I heartily congratulate the developer for taking a stand on the "K" issue. Only computers and the warped mentality that designed and work with them would pervert a longstanding tradition of "K" or "Kilo" means 1000. A kilometer equal 1000 meters not 1028 (or some other arbitrary value) meters. The same goes for kilogram and kilowatt.
Although I have not performed a benchmark against this code, I also must applaud the developers "optimal" use of string manipulation to avoid the costly "divide" operation. As we all know, machines hate math as much as a 15-year-old valley girl! ;)
Admin
So what do you guys reckon happens in this code when a file happens to be less than 1,000 bytes in size? ;)
Admin
Bit shifting is your friend.
(B >> 3 == KB)
Though admittedly, I'm not sure what the above is written in and thus don't know if it supports bitwise operators.
Admin
Well, the 1024 definition of kilobyte came into use years before the inane "kibibyte" term was coined.
So when I says "kilobyte", you can't be sure how many bytes I mean; and when I say "kibibyte", you know exactly what I mean, but I sound like an idiot.
Admin
The difference between 2^10 and 10^3 is 2.4%. Not until you get to 10^10th does the difference really get significant.
Anyway, the base 2 measures have a fairly lengthy history by now too, not nearly as much as the metric system, but still. Anybody remember the Commodore 64 with its 65,536 bytes of RAM? And that was far from a beginning. We're not talking years before "kibibyte", we're talking decades.
Admin
You know, they say that you're a beginner programmer if you think a kilobyte has 1000 bytes. They also say that you're a seasoned programmer if you think a kilometer has 1024 meters ;-)
Admin
what if the file size was 999 bytes? or god forbid 99 bytes?
crash o rama......
Left(-1, strSize) NICE
Admin
<b>Steve Wallis
Bit shifting is your friend.
(B >> 3 == KB) </b>
hmmm. thats a even bigger wtf than the code posted here..
you know shifting one step is the same as /2
so >> 3 = /2 /2 /2 = /8
//Roger
Admin
Not to mention that this looks like VB6/VBA/VBScript code - which doesn't even have bitshifting...
Anyway, apart from the possible errors (<1k size), inaccuracies (1024 != 1000), and all-round stupidness and inefficiency, this one isn't that bad. Hey, at least it works sometimes.
Admin
Are you all insane??
Worrying about 1024 != 1000 ?
Here's the REAL WTF:
4.5 --> ERROR
999 --> ERROR
45.3 --> 4K
4.78 --> 4K
4999 --> 4K
1999 --> 1K
45.79 --> 45K ??!!!
-231 --> -K ?
-25.9 --> -2K ?!
Now IF the original fa.size was always a positive integer, and IF it was never less than 1000, then this MIGHT do (poorly) what it was intended to do, but you would still be left with some REALLY BAD ROUNDING:
1999 --> 1K
9999 --> 9K
Admin
I wonder where you’ve seen a 45.79-byte file… :)
Admin
> -231 --> -K ?
> -25.9 --> -2K ?!
Even more disconcerting is the negative file sizes. Since when did saving data INCREASe the available disk space?
Admin
Converting from Bytes to Kilobytes is essentially an arithmetic operation, but this code is performing string operations. Consequently, it would be much easier to perform validation on an integer than on a string. Assuming for the sake of argument that the string has already been validated (and that it is a base 10 integer greater than 999), this code essentially performs a divide by 1000 operation. Because of the way we represent numbers as strings, this works correctly. Unfortunately, the more precise operation is actually to divide by 1024, but that can't be done as easily on a string representation of a base 10 integer.
My guess is that the programmer either didn't know how to convert from a string to an integer, or decided that it wasn't worth the (nearly trivial) coding effort to convert to a number, divide by 1024, and convert back to a string. It's interesting that lazyness in this case results in fewer lines of code, unlike some of the other examples we've seen.
Admin
Oops. Generated my own WTF.
Admin
hehe.
oops.
sorry i don't think my brain was functioning particularly well when i posted that yesterday.
as soon as i scrolled past it this morning i went "wtf" myself, so i'm not surprised you pulled me up on that roger.
Admin
"What moron decided to originally start measuring bandwidth in bits..."
One who thought for more than 2 seconds about what was going on in the modem?
Admin
Woooooooo! 1024 = 1K. Dang, I guess I'll have to relive the Y2K thing all over again in the year 2048.
Admin
Would you rather buy 256 Megabytes or 268.435456 Megabytes of RAM?
Admin
What's so sad is this matches up to that bullshit new 'KibiByte' spec.. because math is hard.
Admin
all i want to know is if i have 715000kb how many bytes do i need when i purchase gb or something like that
Admin
Admin
Help me understand what Bytes and Kilobytes have to do with how much information can be saved on your computer.
Admin
Admin
That's 366.32 bits. I'm sure you can find some obscure filesystem that tries to compress bits.