Here's a fun little problem. Suppose you've got a system that makes use of the following struct:
struct User {
unsigned int user_id;
unsigned short access_level;
float account_balance; // stored as US dollars
}Your system is ridiculously well-tested, all the tests pass, everything works totally fine. I'll be so bold as to say it's (kinda)
(
Read more... )
Comments 8
A question, though. You obviously know I'm new to C++, so this might be really basic, but what does the line:
new_user.access_level = Privileges::ROOT;
mean? Specifically the Priviliges::ROOT part. I've seen the :: when you're defining a member function of a class outside of its declaration (I think that made sense) but I'm not quite sure what's going on here.
As for the account_balance thing, I've no idea. It can't be a precision or size thing. Does it have to do with the way you might withdraw or deposit money and things getting out of sync? Yeah, I don't know...
Reply
Reply
First basic rule of C++: ninety percent of "good coding practice" is to keep you from shooting yourself in the foot.
Reply
It almost gives it away, but the reason you should never, ever store data that requires precision - like financial info - is because, well, ISO floating-point (i.e. the float or double types in C, C++, Java, Python, or pretty much any language) is imprecise by design. It's not possible to have arbitrary-precision numbers in POD like floats or doubles, because to have arbitrary precision you need an arbitrary number of bytes. Floating-point fudges this by storing a floating-point number encoded in fractional powers of two, which means that only numbers that are sums of integer powers of two (and fit within a certain range) are stored precisely, and such numbers are still limited by the number of available bytes. This also means that addition and multiplication aren't commutative, for obvious reasons.
Hence you could have an Office Space type problem, where adding $1 to someone's ( ... )
Reply
Reply
Reply
Reply
Leave a comment