- 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
I know! I know! I know!
Ooooh, I have the answer!
He should AVOID dividing. Dividing is just inverse multiplying!
milliseconds * 0,0000166666666666667 = Minutes
No negative/positive checks required, and no chance of encountering "DivideByZeroExecption"!
[:D][H]
Admin
That might work in your locale, but I think your octal constant has too many digits. <BEG>
Sincerely,
Gene Wirchenko
Admin
No, it depends on the hardware. All of which I've seen so far that have a divide instruction will truncate.
Admin
According to the official C spec, ints are supposed to be 2 bytes (16 bits), longs are 4 (32), and long long are 8 (64)...
Admin
How about this to convert millis in minutes:
int millis2minutes(int millis) { ....int min = 0; ....int cnt = 0; ....int factor = 1; ....// handle special 0 case ....if (millis = 0) ........return min; ....if (millis < 0) { ........factor *= -1; ....} ....millis = factor; ....while(millis-- >0) { ........cnt += 1; ........if (cnt == 60000) { ............min += 1; ............cnt = 0; ........} ....} ....return factormin; }
Just trying wtf-ing ...
Also, that 0 comments is hilarious since the guy's protecting himself from a multiplication by 0! .. 0 times (1/6000).
Admin
Ints are 2 bytes on some hardware, 4 bytes on others. For example, on 32-bit x86 they are 4 bytes long.
If you need to make sure that your ints are the right size, use long (4 bytes) or short (2 bytes)
Admin
Except this is Java (Note the 'boolean'), on which that means nothing.
int is 4, long is 8, short is 2.
Admin
And how exactly would this depend on hardware? If I wrote a scripting language that translated user input: 100 \ 3 to something like Math.ceil(100.0 \ 3.0) then it would not depend on the hardware...
Drak [pi][pi][pi]
Admin
I bow to the work of this 'artist'. Even if I tried I could not come up with this. Skills like these are hard to find ....
Admin
No.
For "portable" programs using current C compilers, use int8_t, int16_t, int32_t, int64_t (or the uintX_t types). Even though the standard says in 7.18.1 that these are optional (a wtf in itself). If your compiler for some platform does not support these types, generate your own portable defines.
Admin
but what happens if you divide 0 by DivisionByZeroException?????
Admin
I don't know, but this WTF seems really familiar to me, as if I've seen it before...
Deja-vu anyone? Or am I just paranoid?
Admin
Yeah, I've found it! jesperdj posted it on the Sun Java Forums I visit regularly!
Admin
I'd say that a compiler that barfs on some errors without giving you any hint what's wrong makes it rather difficult to attain non-frail knowledge in the language.... never change more than a few lines of a program before recompiling, otherwise you're in for a long trial-and-error hunt for the change that caused the problem.... Ugh, I don't want to imagine. Give me eclipse's compile-as-you-type error highlighting or give me death.
Admin
That's not a wtf. It doesn't make much sence to define them on platforms that can't support them.
Admin
The same bug is in the math that I learned at school and all sane programming languages that I know; shame on me for attending poor school and learning wrong languages.
Admin
No, ABAP converts 2.3 to 2, -2.3 to -2, but 2.7 to 3 ans -2.7 to -3, see
http://help.sap.com/saphelp_erp2005/helpdata/en/fc/eb32e2358411d1829f0000e829fbfe/frameset.htm
Admin
Bullshit. According to the Holy Standard, the only thing you can be sure of is that
sizeof(long long) >= sizeof(long) >= sizeof(int) >= sizeof(char).
So it is perfectly legal to make a compiler where all integral types are 2 bytes, or even 17 bytes.
Admin
Should have been
http://help.sap.com/saphelp_erp2005/helpdata/en/fc/eb3260358411d1829f0000e829fbfe/frameset.htm
or, more specifically:
http://help.sap.com/saphelp_47x200/helpdata/en/fc/eb3434358411d1829f0000e829fbfe/content.htm
Admin
All of those yield an undefined result, however, so division by zero is as good an error as any.
Admin
Bullshit. Citing http://www.open-std.org/jtc1/sc22/wg14/www/docs/n843.htm:
Admin
Where does it say that those are bytes? char just has some unit length.
(Replying to brazzy in case the quoting is buggered up)
Admin
It took me a while to understand what you were saying. Finally I saw it. Excuse me for this type [:D]
Ofcourse I meant:
milliseconds * 0.0000166666666666667 = Minutes
Admin
<FONT color=#ff0000 size=6>AARGH! I WANT EDIT FUNCTIONALITY!!! [:@]</FONT>
<FONT color=#000000 size=2>"Type" is supposed to be "Typo"...</FONT>
Admin
That could hardly be more wrong
Indeed, and sizeof(char) is defined as 1 by the Holy Standard if i remember well.
"1" is the length of a char, a char is 1 char long. Wether said char is 1 byte or 4 doesn't matter.
Char is the smallest data length of C as per the Holy Standard and the base for everything else (which is why sizeof returns a result in "size_t", which is the length of a char)
Admin
If you'd bothered to look at the standard, you'd have seen:
As someone has pointed out here a while ago, the only way to get char to be anything other than an 8-bit byte in C is by defining "byte" to have a different number of bits - the standard explicitly allows this.
The standard really shows C's roots in the iron age of computing - it strongly equates bytes with characters. Theoretically, a system that is really unicode-based would have to define a byte as 32 bits because that's how much you need to "hold any member of the basic character set of the execution environment", which is what the standard requires a byte to be able to.
Admin
Sorry, of course I meant "all integral types *except* char".
Admin
Why are 90% of the comments here written by and/or directed at third graders? I see ample opportunity for a sociology thesis. Anyone?
Admin
Shouldn't that be:
negative = -true;
?
Admin
Nobody has pointed out the real WTFs which are those horrible magic
numbers 60 and 1000. Basic coding practice says these should be:
/
A thousand
/
private static final int ONE_THOUSAND = 1000;
/
Sixty
/
private static final int SIXTY = 60;
Admin
This is the best comment ever:
// Watch out for exceptional value 0
Admin
Shizofrenia is a wonderful thing..
Admin
oooh close but no cigar
Or, of you're a proponent of Semantic/Verbose Programming:
Rendering the final code as:
There's a fine example of a Good Practice.
Admin
Yeah, thats better than defining
Admin
Feck, my code tags got buggered up. What i wanted to say was:
That's better than defining
public static final int TWO_HUNDRED_THOUSAND_SIX_THOUSAND_FOUR_HUNDRED_AND_FOURTY_THREE = 206443;
I rest my case.
Admin
Agreed. I wonder if anyone here uses it for C# development. If so, is it any good? Most Java developers I know swear by it, but I'm not sure if the number of plug-ins make it worthwhile for C#.
Admin
The original K&R book (not the ANSI edition) lists one platform with a C compiler where char, int, short, and long all have exactly 36 bits, which you will notice is not an multiplel of 8.
There were also machines with 12 bit bytes (pdp8?), when hex notation is not so nice, but octal works great. I wonder what Purdue graduates would think of such a concept.
In these days nobody makes such machines, but back in the bug iron age a byte was not always 8 bits.
Admin
Did you ever consider that he may not be trying to make it easier for the machine, but instead this conversion makes it easer for him? Perhaps he just cannot visualize how negative division works. : )
Admin
I haven't used Eclipse since college, but we can get background compilation in VS.NET using Resharper. I'm told that a lot of the functionality of resharper is also in Eclipse, but I can't be a good judge of that since I can hardly remember what Eclipse had 3 years ago.
Amazingly, the bastard child known as VB.NET has background compilation out of the box in VS.NET 2003. I think that VS2005 is supposed to have it for both languages, but I'm not sure of that either.
Admin
Bytes having more (or less) than 8 bits is fine - I can deal with that (alt least theoretically), it's just an arbitrary grouping.
What irks me is the assumption that 1 byte = one character from a fixed charset. But it must have seemed natural at a time when all your hardware and software came from one vendor, when your were an "IBM shop" meaning that Token Ring was THE network, EBCDIC was THE text encoding, and wanting your machine to talk to a machine that was from DEC and used Ethernet and ASCII was just pure madness.
This mindset pretty much explains the big steaming pile of WTF that is the multitude of text encodings we have to deal with today. I shudder at the thought of how it must have been like in 1970 to be a computer engineer e.g from Japan, trying to explain that no, your language's character set does NOT just require a few characters with funny dots and dashes to be added, and does in fact NOT fit into 128 or even 256 slots.
Well, now at least I know whom to blame for the difficulty in teaching aspiring Java programmers the difference between String and byte[] and why the former should never be used to hold the latter, and the reverse only with an explicitly specified character encoding.
Admin
No! it should be
int convert_milliseconds_to_minutes(long int t) {
long int t5 = t>>5;
return ((t5<<1)+(t5<<2)+(t5<<3)+(t5<<6)+(t5<<7)+(t5<<8)+(t5<<9)
+(t5<<11)+(t5<<15))>>26;
}
(Yes, I know this will not work).
Admin
Best comment in a while.
Concerning compiler barfing: Anyone who used old versions of gcc or Turbo anything knows all about compilers crashing over perfectly legal code, let alone bad code. Template instantiation in particular has been crash prone in every C++ compiler at some point.
Admin
I mean't:
Admin
It isn't assumed (at least it's not in C++, I assume the same holds true for C). See 26.3 in http://www.parashift.com/c++-faq-lite/intrinsic-types.html
I highly recommend the book the website forms a subset of for anyone doing C++ work.
Admin
That is like saying, "It irks me that Columbus sailed across the ocean. It is a WTF that we have all these harbors when we really need airports that handle jumbo jets."
Put in in historical context. When a few K of RAM cost thousands of dollars and disk drives storing 10 MB were the size of washing machines, These kinds of optimizations were needed. Every bit was precious.
Obviously, we wouldn't design a system like this today when a GB of ram can be had for $50 and a TB of hard drive storage is available for the PC market for a few hundred dollars. But we are stuck with a legacy.
Admin
No! That code breaks the principle of readability in code. It has too many instances of the number "6".
Admin
And highly I recommend referring to the language spec rather than someone's possibly very useful but certainly not official FAQ. As I cited before, the C standard (of which the C++ standard is merely an extension) defines a byte as "addressable unit of data storage large enough to hold any member of the basic character set of the execution environment". Since nowadays 1 byte = 8 bit is the norm dictated by hardware makers, but 8 bits are not enough for the charsets we want to use, disregarding this part of the standard is considered a Best Practice, which is basically a language WTF.
Admin
What I'm criticizing is that the standard blurs the distinction between binary data (bytes) and text data (characters) while permitting the mapping between them to be implicit, platform-dependant and inadequate to represent more than a handful of languages. The first three could have been done better without needing any additional space whatsoever, and the last with using extra space only when necessary.
Also, note that at the same time, despite the high costs for RAM and HD space, it was common to store and manipulate numerical data as text strings, which wastes space AND processor time.
Admin
The negative rounding problem people have mentioned is for the common +.5 trick used to round to the nearest when converting from float to int.
So default:
2.3 -> 2
-2.3 -> -2
2.7 -> 2
-2.7 -> -2
And you want
(start float) -> (int conversion)
2.3 -> 2
-2.3 -> -2
2.7 -> 3
-2.7 -> -3
Well, if you add .5 to the float and then convert, for positive numbers this works, so
(start float) = (after .5 addition) -> (int conversion)
2.3 = 2.8 -> 2 (correct)
-2.3 = -1.8 -> -1 (incorrect)
2.7 = 3.2 -> 3 (correct)
-2.7 = -2.2 -> -2 (incorrect)
So, if those numbers were made positive, added .5 to, and then made negative again, then it'd work. Of course a competent programmer would just negate .5 instead:
(start float) = (after .5 subtraction) -> (int conversion)
-2.3 = -2.8 -> -2 (correct)
-2.7 -> -3.2 -> -3 (correct)
Admin
ummm.. I like zero... I think that zero is quite exceptional, in a not really positive and not really negative way. Sort of like something that is nothing, but still is something even thought it is nothing, if you know what I mean.