- 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
Admin
Oh, Not just old school. Those of us concemned to an eternity of J2me Game programming are also painfully familiar with Fixing Points (and hanging on for dear life). Those "Old schoolers" dndn't know how lucky they were, when floats were just slow, as opposed to being spat out by the compiler as an invalid type.
Admin
Don't forget that most of this isn't decided by the C# compiler, but by the CLR.
Again, no. It will be allocated on the stack if the expression it's the value of is on the stack - a local (non-captured) variable, for instance, or part of a local (non-captured) variable which is also a value type.However, if you have something like:
then "price" will always be on the heap, as it's part of a reference type. Likewise any static variable of type "Money" will also be on the heap.
See http://pobox.com/~skeet/csharp/memory.html for more details.
Jon
Admin
Can't you see? It all makes perfect sense. Expressed in dollars and cents, (hundredths of a cent and thousands of a cent), pounds, shillings, and pence.
Admin
Not if it is a field in a reference type.
Admin
Jon, you rule! You never miss an opportunity to point the "incorrect oversimplification" :-)
Admin
Euros bad as him. Sterling effort I suppose, but I should Pound all three of you into the ground. Yen we'll get back on topic. Because Franc-ly, money puns are obsolete. And I've lost my notes.
Admin
Welcome to a basis point my friend
Admin
You guys make me mo-ney
[moan]
Admin
The problem with money is that it can become quite a lot very fast. Given that the INT column was already used with cents and later with hundreths we loose 4 orders of magnitude for what the maximum could be. In SQL Server you would end up with a max of + or - $200,000 which is not sooo much and could easily be exceeded. So a new INT column should be used, 1 for the $ one for the cents and their fractions.
Admin
For example, who says that real numbers are a sufficient model? I can certainly imagine a use for complex numbers (six columns, or only five if you collapse the real and imaginary radixes!). Store the tax in the "real" part, and the amount left in the "imaginary" part. (Well, that's the way my bank account looks at the moment.)
Admin
Because of such wisdom we will run into the year 2038 problem.
http://en.wikipedia.org/wiki/Year_2038_problem
Admin
The practice also lives in DSP programming. Programming for a DSP is so full of low level pain anyway that you don't really notice anything out of ordinary. The only remotely tricky part is when an algorithm needs to slide the decimal point during calculations. Matlab has quite sensible fixed point type that helps a lot.
Of course there are then those programmers who don't get the whole idea and use powers of ten for decimal. If only the NDA was not there...
Admin
well, if Pete was consequent, he ran an update on the prod data, multiplying the values by 10 for each version.
Let's just hope that was the case.
Captcha: Eros (yeah thats me)
Admin
When talking about prices in financial applications it's not unheard of to create money types as integers by the cent for type portability (among a few other reasons).
Problem occurs when you're not actually talking about cents but the abstract cent fractions (which could still be ok if you had all the integers required to calculate the fraction)
Admin
I guess it depends on how old the application is and what its history is. If it was originally written using a database that didn't have a MONEY data type, using an integer would have been a very sensible option. And if that's what the programmer was used to, he may have done it automatically anyway, even if he was using a DB with a MONEY type.
In fact, it's just the changes in precision that have caused the problems, and I'll give you bets it was the same management that insisted it was done that way that are still insisting it gets ported without any changes.
It's worth noting that if this is an application in the financial sector, these banking guys can be very strict about changes to their software. If an application works as required, they will be extremely reticent to change it at all, and if they do authorise a change, every tiniest modification has to be approved and cross-checked in ways that would trusly scare most of us mere mortal programmers.
Admin
At least, don't use a binary floating point type. If you've got a fixed decimal point type (as databases tend to) then that's great; using a decimal floating point type (like System.Decimal in .NET) will avoid most of the problems people run into with binary floating point types.
Jon
Admin
After working with currency, money, small money, decimal (18,4) , decimal (27,6)!!! and finding it both cumbersome (convert functions peppering stored procs etc) and difficult to maintain consistency across multiple tables/developers we decided when we set about re-writing our risk platform some years ago that all numbers should just be floats. For reporting, where necessary we use the round function, or just format in the client (often Excel).
We did consider using 'int' for share quantity, but found 2 billion sometimes wasn't enough for some of our positions - I work for a large firm! - so we went for floats here also.
We are VERY happy we made this decision and recommend it to anyone considering any other approach.
Admin
(Not Just the old school'ers matey! DSP and embedded guys are still using fixed point, and embedded c actually has proper base types for it!)
The scarry thing is that Pete's antics went unnoticed... so Either he was
Admin
That last comment was so money!
Admin
Admin
And it's multi-user too which means that there's some concurrency!
Admin
You assume that Pete's antics went unnoticed. I don't think that's the case. Consider, if you will, the question: what happened to make management so wary of change? Could it have been the experience that whenever they requested a change of Pete in his code, the application started breaking in weird and unexpected places?
If management requests that Pete update his code to calculate with 4 digit precision, and suddenly the price databases is off, and operators have to check and rekey any prices they use, that (and similar occurences) might make management anxious to avoid changes as much as possible because in their experience changes are costly and prone to bite you in unexpected ways.
And the only person who could have explained to them why this was so was the person whose job security rested on management never finding out.
Admin
Is it okay if I pay for it with 10 1-quadrillion bills?
Admin
frighteningly enough, I am working right now on a system that uses floating point math to handle money. One formatting routine marked everything to the left of the decimal as $ and 2 places to the right as cents .... looked good until a-(a/2 +a/2) = 1.38E-12. Took a bit of prying to decipher that the $1.38 error on the list wasn't what it looked like.
Also javascript is just nasty with it's rounding errors. I think I see more there than in any of the other languages I write in combigned. I have a display right now that works with the same data in 3 different modes, A+B+C=D, A+C+B=D and B+C+A=D+0.01.
I certainly agree with the 2 column storage for $ & cents (nobody is going to be bothered past int for precision on cents, and bigint will cover all but the biggest trading houses) - however, most of the errors in the WTF system could have been avoided if anyone had thought to do a simple money_field = money_field *10 over the effected tables while rolling out the precision updates (and updated the comments on the money class).
Admin
The only information that need be different is the currency ISO itself. Beyond that, you can easily store amounts used by any currency in the same column of type decimal.
Admin
Sure, storing data such as this as a floating point would be a huge mistake. Trying to out think the SQL schema designers by using a needlessly complicated application-based solution was an even bigger mistake.
Clearly storing the value as decimal/numeric was the right solution. In that vein, using the Money type would have also been a mistake IMO. Money is proprietary to various DB systems and can do weird rounding when used for division. That said, had he stored it as Money, it would have been easy to change to decimal as most application layers equate Money to decimal.
Frankly, the core mistake was the lack of thought regarding data integrity. Attention was only given to application integrity (we hope) and when they discovered an error, the data was hosed. Application developers are generally incompetent when it comes to database design and maintaining data integrity.
Admin
Unless it gets boxed, of course.
Admin
Admin
ZING!
Admin
THERE! I knew if I waited long enough I'd see some good old fashioned male immaturity! Not to mention some really daggy jokes!