2 ms·
> understand your problem domain It sounds like the article's explanation of how precision erodes as the number increases would help a lot when thinking about
by pjungwir 12y ago
> understand your problem domain
It sounds like the article's explanation of how precision erodes as the number increases would help a lot when thinking about this. A good epsilon is a function of the size of your numbers.
I've been writing a lot of money-related code lately in C with long doubles, so floating point has been on my mind, and this article is timed perfectly. According to my tests I can get past $36 billion before I see $0.000001 error, which I think is good enough for me, but I should revisit my reasoning now that I understand things better!
- AndrewOMartin 12y agoYou may want to use a more intuitively behaving datatype for currency, even if just to avoid flame on certain community sites on the web. Consider an integer representing ten-thousandths of a penny, if guaranteed to stay in USD or GBP, or a dedicated currency library otherwise. Currency's just one of those things that makes some people very emotional when the notion of any kind of error or rounding is mentioned.
- lightcatcher 12y agoI'm not very experienced in representing money in computer programs, but I thought the best practice was to store all values as integers (using either cents or smaller as your unit). Any reason you chose to use doubles instead of integers or longs?
- jacquesm 12y agoThe best way to represent money is as fixed point integers. 64.64 would be a good start, you can adjust that up/down with more knowledge about your application.