- 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
What the hell? The most WTF's in this are due to the anonymization?
The Doors - The End
Admin
"best commented"? Are you nuts? The method itself has no documentation at
all. What do its parameters mean? What values are legal for them? What is the
method's behaviour if its parameters assume illegal values? What does it
return in the unlikely event of it being used correctly?
(And a bonus question - what does the method for calculating interest look like ? :-))
Admin
Really? You think??
Admin
Funnily enough, there was a thread on slashdot yesterday on the very subject of types for representing money.
Admin
Anyone even contemplating using floating point numbers for currency should be shot. Immediately.
Admin
It's all well and good, but it should have a database table (TBL_NUMBER_BEAUTIFICATION or somesuch) that stores each number with its beautified version.
You'd need a DBA to add in extra numbers when you needed them of course, but that's fine and what enterprise is all about.
CAPTCHA: bedtime .. 9.30am - yeah I should be in bed still :)
Admin
I'd say the WTF is that you don't seem to differentiate between float and decimal, two distinct data types in C# (the language of the example). float having its well-known (though often misunderstood) limitations, and decimal being a datatype specifically designed for working with financial calculations and other fractional calculations in base 10.
Quite frankly, I think that many of the commenters on this site are prime candidates for WTF code creation if the quality of their comments are any basis by which to judge.
Admin
Admin
I don't often get drawn into replying to these WTFs, but on this occasion I feel I have something to actually contribute.
clears throat
GRAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAARRRRRRRRRRRRRGGGGGHHHHHHHH! DIE DIE DIE DIE DIE DIE!
pant pant
My, that is awful. It's about the first thing I've seen on TDWTF which, if I encountered it in real life, would make me just silently leave the building.
And the city.
And move to Bolivia.
Mm, ceviche.
Admin
???
Admin
Where's the database call? I cannot see one.
Admin
Luckily, a sane programmer remove the hideous mess that was on that method, and replaced it with a single line that does exactly the same thing as the previous version:
return "MAYBE";
Admin
Funny how you quote .NET, without knowing that the .NET decimal type is a 96-bit integer and an 8-bit (I think) decimal exponent. And presumably a sign bit. Amazing waste of space, given that they had at least 7 other bits to use (if not 23) without making it take more space.
Financial calculations are NOT about 'accuracy'. They're about correctness. You can't make sure that your calculations are 'to the nearest whatever' (Does 0.499999... round up or down? you need to wait until you find a digit that's not a 9, which may never happen). You can make sure that they follow your local laws, assuming that it's possible (e.g. you can calculate UK VAT in three different ways, which mostly differ by where they round).
Sadly, .NET doesn't provide things it should, like Decimal.HasMoreDecimalsThan(int places), Decimal.IsInteger() (which compiles to hasmoredecimalsthan(0)), and proper integer devision (floor(a/b) is not guaranteed to be the same as (a div b), e.g. if a is close to b and the calculation rounds up).
Admin
The StringBuilder is created and destroyed within the same function. No WTF here. ANY time you use +, you create a StringBuilder (In Java 1.5, at least, a+b+c compiles to new StringBuilder(a).Append(b).Append(c).ToString(). The real WTF is that versions before 1.5 used StringBuffer, which is slower since it's thread-safe).
And a few dozen objects is peanuts.
Admin
The code I'm currently seeing looks like it would prepend places + 1 zeroes to the "number" before putting in the decimal point somewhere in the middle, so passing in 120 with places == 1 would return 0012.0, passing in 2 with places == 3 would return 00.002 and it might have been enough to prepend places rather than (places + 1) zeroes.
Admin
And this made-up code is again presented as if it were a real example of professional code.
Admin
well if they want to avoid that they shouldn't be using pentium pros!! (floating point bug... haha...)
Admin
You're kidding, right?!? So if this were an architectural critique site, and the daily example was "skyscrapers with no elevators," you'd be the fucktard that'dcompalin, "I visited that building, and, although there are only stairs, the lobby colour is GREEN not BLUE!"
The REAL problem lately is all the crap comments from people like you. Let's see some more moderation here, please.
Admin
Meeting:
developer 1 : now lets do dates! I hate how all of society makes those hardcore "seconds" appear beautiful with their corrupt "minutes", "hours", and "days"....
developer 2 : yeah!! woohooo!! seconds rock!
developer 3 : waidamin! are seconds really the base unit of time? I mean... SI units all aside... isn't the second actually a particular multiplication of Planks time.. the shortest possible time in the universe...
developer 1 : pah! corruption!
developer 2 : yeah! woohoo!! Plank rocks!!
etc
Admin
No, I'd be complaining that the story is accompanied by a picture of the Eiffel Tower.
Admin
I don't think many of us had large issues with the logical/bitwise Or mishap. Also, simply because a system only uses integers and strings, it isn't necessarily a WTF. You may think so, but it is quite the contrary. Still, the code you originally posted was no where near the same code as what was submitted, and even the code now is different. Maybe what you need is a good disclaimer for people posting code, and maybe require them to alter it themselves. Apparently Alex, your refactoring skills are lacking to the point of being a WTF themselves. The current code in the post still misses the " Convert that resulting string to a number, divide the parameter by it, convert that floating-point number back to a string, then return that float-in-a-string.".
Admin
Alien, not Aliens, puhleeze!
Admin
I refuse to believe this is real. Nope. Never happened.
Admin
This forum drives me crazy. Decimal and integer are not opposites.
What is 99? It is an integer. It is also a decimal number. (Well its not base-2 through base-9, that's for sure.) Decimal simply refers to the base. Deci is from the Latin for 10. BASE *10*! Not fractional -- BASE 10!
P.S. Oracle uses fractions to represent time.
Admin
QFT
Captcha: Anonymous
But really, why do you have to remember the persons name when you want to quote them ^^
Admin
Thank God. There is one other person here who an iota of sense. I have said this same exact thing 2 or 3 times in this thread.
This reminds of the time when everybody went nuts over that "obfuscated version numbering" WTF and wrote in with suggestions to "dehash" the obfuscated string when support needs to map it to a working version number ... using the word "dehash" (and not kidding) is a bigger WTF than anything posted on the main page of this site in months.
Admin
Right on! Alex, whatever somebody sends you, POST IT VERBATIM! You can't get sued for posting something somebody sent you -- just blank out obvious references to a business, such as copyrights in the comments section. But if somebody is sending you proprietary code that they shouldn't be sending you, that is THEIR fault. I would never send in code because I value my job and it would be hugely illegal. The worst that could happen is somebody sees the code and asks you to take it down. I love this site, but transliterating a code fragment into an entirely different language and making several mistakes along the way makes everybody jump on the WTFs that you introduced, and we completely miss the real point.
Second rule: nobody here should make fun of other people's code if they don't truly understand what is going on. There are a lot of opinions here about how to properly do money math -- only a couple people are correct. And yes, there is such a thing as "correct" -- it is mandated by law and industry standards. If you work for a large financial corporation, then money math is ALWAYS DONE IN DECIMAL.
Admin
Sweet!!! Free legal advise from savar!
I'm assuming you're also an expert in taxes and accounting too, so could you help me with some deductions I'd like to do this year?
Admin
Well, he could - he lives in the U.S. of A. after all. Whether the suit would be successful is a different question. Alex is probably justified to be on the safe side by changing the code he receives into something completely different. Whether the result is the kind of WTF that readers of this site eagerly anticipate every day is another question.
There is some legal gray area in which snippets of commercial copyrighted code could arguably be posted verbatim on this site for educational purposes. Schools do this with literature all the time. As long as it is obvious that no commercial harm is intended, via the gathering of profit from someone else's copyrighted work, it is unlikely that anyone could successfully sue Alex for copyright infringement. And as long as the actual source organization or person's name is not posted, a defamation/libel/slander suit is not likely to get far either. That would be my opinion, but IANAL, and IANA (I Am Not Alex). :)
This is a very good point as well, and not enough software developers (or those who call themselves software architects) understand it. Or the related point that the number of significant digits in a fractional calculation is a well-defined quantity, and having too many digits is just as wrong as having too few. I would say that part of the blame for this lies in mathematics and computer science degree programs which completely avoid dealing with real-world issues such as money math and significant figure calculations, and sometimes only barely touch on the pitfalls of binary floating point arithmetic.
You don't need to know about these areas to get a degree, but you will surely need them if you want to be able to write correct real-world code.
Admin
Reading threads involving money on this site are quite funny, like nobody here has ever had to work with an accounting department! They have this book, you see, called the GAAP. It is the 'accounting bible' so to speak. Go to your accounting department, you will find it on a bookshelf. In the GAAP you will find documentation about dealing with money in computer systems. Money is to be stored in 4 decimal place amounts ($1.1234) and during transactions rounded to two decimal places according to something called 'banker's rounding' which specifies that .5 is rounded towards the nearest even number. $1.3550 becomes $1.36 and $1.3450 becomes $1.34 (the reasons for this are obvious when millions of transactions are considered).
Point is you CAN'T store money in a FLOAT! It's against the law! Unless your ENRON of course, then you just pencil in whatevor numbers you fancy.
SQL Server has a nice 'MONEY' data type to take care of this for you.
It's also interesting to note that other countries have different requirements for this ... I'm just going buy my own experiences.
Admin
Umm... No. The GAAP is NOT a law. It's a set of standards published by the FASB. No private company is required to follow it. Private companies could have a chimpanzee do their accounting by simulating floating point arithmetic using rocks and sticks, rounding at 42 decimal places to the nearest odd value, but only on Wednesdays when the chimp isn't "in the mood".
Public companies and government agencies, on the other hand, might be required to adhere to GAAP. Not sure.... seems to me, that would be imposed by the SEC or some similar organization.
OT: Hey! Even backspace causes 2 new javascript errors! WTF?
Admin
No.
Admin
Unix Epoch Time? Nah... Who would ever use THAT?
CATPCHA: Whiskey, the kind the coders musta been drinking.
Admin
Yes, I see! So instead of 1.10, I store the number as 110. Now we're safe! I can just put that decimal back at any time. Nothing "funky" could possibly happen to this pure integer now. :)
Now, playing the role of the finance type, I divide this number up into three parts because I'm allocating 110 into 3 business units. They're both integers, so no prob here. I'll just.....
110 / 3 = 36.666666666666666666666666666667
Uhhh. Wait a sec. What the PAULA just happened to my pure non-funky integer? I just divided, and....
Let's put that decimal back (though I already have one), sweep it back into the database, and pretend this never happened.
THIS MESSAGE IS "G4" CLASSIFIED.
Admin
If you responded to this thread, and have never read the below link, you probably said something completely retarded.
<font><font color="#003366" face="Verdana, Arial, Helvetica, sans-serif">What Every Computer Scientist Should Know About Floating-Point Arithmetic</font></font>
http://docs.sun.com/source/806-3568/ncg_goldberg.htmlAdmin
<font face="Courier New" size="2">110 / 3 = 36</font>
Unless, of course, you meant:
<font face="Courier New" size="2">110 / (float) 3 = 36.66667</font>
or:
<font face="Courier New" size="2">110 / (double) 3 = 36.6666666666667</font>
or:
<font face="Courier New" size="2">110 / (decimal) 3 = 36.666666666666666666666666667</font>
All of which, given that the "real world" value was being stored in "cents" in an int, would give the same results when put back into the system: 36 cents. It's true that you've lost a penny, but when you only chose a precision of whole pennies, what did you expect?
If the decimal datatype did not already exist, and no one had yet developed BCD, it would be quite reasonable to use a "shifted" int value....just make sure you "shift" it to the precision you want (most accounting uses four digits after the decimal point, so shift your int by four places (multiply by 10000)). In this case, dividing 11000 by 3 only results in the loss of $0.0001, which will be acceptable in almost any situation.
CAPTCHA = hotdog
Admin
My guess is that dates are represented as the number of seconds since a predefined date. Except second may not be fine-grained enough. They probably represent it as a number of miliseconds.
Admin
GAAP is enforced by the SEC. Private companies -- I would imagine -- declare similar adherence to such standards in order to get people to invest in them...it would be quite foolish to give money to somebody who had no standards of accounting for it.
Admin
Jesus christ it's a lion get in the car!
Admin
Exactly. In many cases your system will work with explicit dollars and cents quantities only. When calculating interest, you convert your interest rate thusly:
1.015% = >101015
Multiply your prinicpal by the adjusted value, then integer divide by 1000.
This will leave your quantity with two extra decimal places of precision.
Now apply standard accounting rounding (0-49 down, 50-99 up), then divide by 100 to get back to your base units (whole cents).
The same procedure can be used with fractional cents as the base unit.
Using 64-bit in memory representations you can precisely handle very large amounts in this fashion.
Admin
SORRY, It was definitely Aliens, as in the 2nd movie of the series. You know, the one with the marines. Not the first one, with the all time great shot of the alien popping out the the guys chest while everyone's sitting at the table eating breakfast.
Get it right, or be relegated to being a WTF yourself.
Admin
Similarly, I once saw someone try to specify hierarchical set of data in XML like this: Each<font face="Courier New" size="2"> Foo* </font>element was identical except in name, and the number on it identified the depth of the element.
Admin
Accountants use banker's rounding.
Admin
on many platforms floating point simply doesn't exist. celphones, nintendo ds - anything with an arm-processor on it.
nothing very strange about this - and yes you can do division, multiplication - as well as any kind of advanced math (sin/cos/tan etc) on fixed point processors perfectly fine.
Admin
Now I can write Perl in any .NET language.