4 ms·
If you don't care about precision use single floats. That's most of the time in games. Some people even used ints as fixed precision fractions. If you care abo
by ajuc 13y ago
If you don't care about precision use single floats. That's most of the time in games. Some people even used ints as fixed precision fractions.
If you care about precision - doubles still aren't precise - for currency use bigdecimals, for some stuff ratios can give precise results, for irrational things your only options are doubles, but be aware that you aren't precise, even for simply looking operations. No way to compute 10% of something precisely with doubles.
- socillion 13y agoYou wouldn't use ints for currency?
- hvidgaard 13y agoYou shouldn't use fixed integers. What happens when you need to represent a fraction smaller than what your largest integer can? For financial calculation you use a dynamic integer type that expand as needed.
- vidarh 13y agoFor many financial applications, you know precisely in advance how many decimal points you need to be able to accommodate. E.g. for UK VAT calculations, you are never required to work it out to more than four decimal points (and can then round to 3). I sort-of agree with you, in that it is easy to get bitten, and that if people have a dynamic integer type available on their platform that's probably safest. If the choice is between using integers for fixed point vs. using double's though, I'd go for the fixed point any day.
- justincormack 13y ago64 bit ints in cents/pence/whatever should be OK. 32 bit ints are too small. Even in Zimbabwe which issued $100tn bills 64 bits should be enough.
- ajuc 13y agoYou can, but: - it's still advisable to wrap ints in some class to be able to distinguish ints that are already multipied and ints that aren't. It's also better cause then you can overload operators. I've written graphic demu using fixed point numbers (floats) without wrapping them, and it was very painful and error prone. So you still work on objects and not on primitive values. - there's a problem with how much precision you need. Different countries specify their way to round up doing money calculation differently (even with the same currencies - see Euro). When you use ints you have the assumption about precision repeated all over your codebase, which make it harder to change when tax law changes or you want to support other countries. With BigDecimals most of the code is universal.
- wlievens 13y ago> for irrational things your only options are doubles That's obviously not true. For irrational things, if exact values matter, use symbolic arithmetic. http://en.wikipedia.org/wiki/Symbolic_computation http://en.wikipedia.org/wiki/Symbolic_computation
- ajuc 13y agoYes, ok, my bad.
- nraynaud 13y agomost of the time it doesn't work very far. Very quickly you'll need some kind of truncated polynomial development or some Newton to compute a value. Last time I hit dead water very quickly, computing the length of a non-trivial curve is only doable in numerical world (that one was funny, because they expressed it as a quite complicated closed form of functions, and those functions had no closed form themselves).