5 ms·
Yeah, I was going to say. The wisdom that I'm being taught in school is "Don't use single precision floats, at least not when you're on any kind of a modern sys
by lelandbatey 13y ago
Yeah, I was going to say. The wisdom that I'm being taught in school is "Don't use single precision floats, at least not when you're on any kind of a modern system."
- nkurz 13y agoThat's OK wisdom unless you are aiming for high performance. I'd put it at about the same level as "Don't use C if something higher level will work." If you are hoping to gain from vectorization, or if you have enough of them to care about memory usage, single precision is twice as good, unless the extra precision of a double actually gains you something.
- lelandbatey 13y agoOnce again, I fell into the trap of saying "you should often do x" while leaving off the "... Except when you shouldn't". The exact comment from the CS professor I have in mind was "Using doubles when doing realtime human facing calculations (such as a calculator) is the best way to go. However, floats have their uses, such as on systems where there isn't enough memory, or where a float fulfills the requirements." but that's a bit much to type from a phone so I went for "use doubles". And now I'll never be so lazy with my comments again!
- ajuc 13y agoIf 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).
- dagw 13y agoLike all good wisdom, there are plenty of exceptions. I use single precision all the time. The most obvious advantage to me is that I can load twice as large data sets into memory, which has some definitive performance advantages.
- forrestthewoods 13y agoOh god. The future is doomed.