7 ms·
Who's using single precision floats these days?
by jwmerrill 13y ago
Who's using single precision floats these days?
- plorkyeran 13y agoThey're the standard choice for 3d graphics in games.
- lelandbatey 13y agoYeah, 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.
- 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.
- nabla9 13y agoAnyone who wants to do fastest possible floating point arithmetic with graphics card. Also, if the memory, disk access, or IO-troughtput is the bottleneck with number crunching, single floats might give huge advantage.
- wlievens 13y agoWe do all our image processing using floats (as memory and disk storage - not as computation accumulators obviously). It helps a lot if you keep a dozen seventy-megapixel images in memory, because it means you can run twice as many jobs on your cluster.
- LnxPrgr3 13y agoDepends on the application. If I'm processing audio in a PBX, 64-bit floating point precision isn't going to do me much good when my source is a noisy 8-bit µ-law stream. All I'd gain is twice the demand for memory space and bandwidth—not a cost I'm willing to pay when I gain zero developer productivity from it. Even CD-quality audio only has 16 bits per sample. My digital camera only records 12 bits per pixel, and that's before the image is converted to a more reasonable 8-bit-per-channel format. Single precision is still overkill in a number of applications.
- yread 13y agobut these are all fixed point! When doing arithmetics you can lose as much as 1 bit of precision per floating point operation. So, it pays off to do use more precision for calculations even when the storage format doesn't persist it. Besides I remember reading somewhere that 32-bit floats instructions aren't actually any faster than 64-bit (except of course the cache...). They even might be slower because the FPU has to extend them to 64-bit before performing the operation
- LnxPrgr3 13y agoTo be clear: are you talking FPU instructions, or more modern SSE(N) instruction sets? Modern compilers on modern CPUs pretty aggressively vectorize my number crunching. If it's something you're worried about, I'd suggest measuring the difference in your actual application, both in terms of performance and accuracy. If I can come up with a decent test case I can legally publish, I'll post results, but your mileage may still vary.
- sharpneli 13y agoThe time when the actual floating point instructions were a bottleneck are long gone. Nowadays when you do computations with single float/double values in your registers they are equally fast. The biggest difference comes from memory bandwith and the ability to vectorize. Your CPU can calculate either 4 floats or 2 doubles in one instruction (assuming pre AVX X64 processor). With AVX it's 8 floats or 4 doubles.
- 13y ago
- Negitivefrags 13y agoYour comment makes me feel sad. It just reflects a whole mindset that makes me feel like an oppressed minority somehow. It sucks that most people don't really care about performance any more. It sucks when people causally dismiss people who do care about performance with arguments like "premature optimization" and so on. It kind of sucks that the web is taking over because it gives an excuse to use slow languages. It almost makes me feel sick to think that Javascript stores all numbers as doubles. So you know what? It makes me feel happy to use the most efficient type that will work. I like my single precision floats. I can die happy knowing that I didn't waste everyone's collective network, disks and CPUs with needless processing. Can't it be good to save just for the sake of saving? So you want a practical reason do you? I shaved over 2 gigabytes off the size off the download size of our product simply by using more efficient data types. Using half size floats is good enough for some data (16 bits). And let me tell you about a data type that we used to use called a char. 8 bits would you believe! Turns out those x86 CPUs that are sitting there a few VMs deep still support those things.
- hvidgaard 13y agoIt's not even about performance. It's about actively thinking about what data you'd be processing. Why use doubles when singles are okay? I'd sure not be using 64 bit ints just because "it's bigger" when a normal 32 bit would be just fine.
- marcosdumay 13y ago> Why use doubles when singles are okay? Because of rounding issues, that look like a no problem for a single precision number, but will be a no problem^2 for a double precision one. Thus, if I'm wrong (or get wrong after a change in scope), the program will still work.
- cwp 13y agoWell, if you need more precision than singles provide, then yeah, use doubles. The question was, "why use doubles when singles are okay?"
- zvrba 13y agoIn engineering applications, it's perfectly sensible to use float for world modelling, which give you precision of about 1 in 10 million. Then you can use double for intermediate results and lose less precision in a long calculation, producing a more accurate result (rounded to float) in the end. Also, if the dynamic range of your data allows it [and for physical application it usually does], you can first rescale it to [0,1] in order to further increase precision. [It's the interval where floats are most dense.] Having worked for a few years now in computational geometry, i'm more and more convinced that "float by default" is the way to go. Doubles should be used for intermediate computations.
- sampo 13y ago> rescale it to [0,1] in order to further increase precision. [It's the interval where floats are most dense.] Who not to scale to [0,0.5], where they are even more dense? ;-)
- brucedawson 13y agoAmateur. Scale to [0,FLT_MIN*2]. There they are truly most dense, and the density is uniform across the entire range. It's almost like using 24-bit integers.
- ygra 13y agoLast place I had to use them was OpenGL stuff on Android. No doubles even supported, as far as I could tell.
- nly 13y agoAnyone who doesn't need more than 6 digits of decimal precision but can't be arsed to scale everything in to a fixed point format.
- epsylon 13y agoThe whole games industry uses 99.999% floats.
- dj-wonk 13y agoIronically, like a float, I suspect the precision of your `99.999%` estimate is dubious.
- malkia 13y agoOh wow. Just about anyone in game development :) - lol... lol since I've had the exact opposite arguments with my own folks - but it wasn't for in game use, but rather game tools. But I think I was wrong at the end. Storing all model geometry vertices in floats would be too much of a loss, also stick to 16-bit vertex indices (instead of 32-bit).
- wglb 13y agoIn many applications, single is good enough. If you have a mass of values that needs to be stored somewhere, and the accuracy of single precision is fine, then single precision it should be. If there is a long chain of calculations that use those numbers, you might do the intermediate calculations in double, or even long double.