Float versus double back
Board:
Home
Board index
Raytracing
General Development
(L) [2015/06/10] [JasonSmith] [Float versus double] Wayback!I have a mix of float and double types in the c++ code for my path tracing renderer, and I was just considering making things more consistent.   I hear from a Google search that pbrt uses a compile switch.
Double seems convenient for large scenes and for avoiding precision issues in arithmetic. But potentially harder on memory for large textures and models.
How have you decided what to use in your renderers?
(L) [2015/06/10] [mrluzeiro] [Float versus double] Wayback!Due the nature the objects I am dealing in a current project, I am normalizing all object and space to fit between -1.0f and 1.0f
So for raytrace I use floats, but in some pre-calc & intermediate process (of the model/object data) I use doubles.
I was trying to look for some studies regarding "normalization of the data" for rendering proposes but I couldn't find any..
(L) [2015/06/10] [szellmann] [Float versus double] Wayback!Being able to switch this, e.g. using a typedef at a global scope, may in general be a good idea.
That said, I never experienced precision issues that rectified using double, I could always work around them, e.g. using an additional Newton-Raphson step for sqrt(), etc. With lots of matrix mults, maybe for instancing or so, you however might prove me wrong, I'm not sure.
I think that going from single-precision to double-precision will impact rendering time more severely than it will impact memory: less data items will fit into a cache line, more costly memory fetches, lower SIMD utilization.
Best,
Stefan
(L) [2015/06/10] [JasonSmith] [Float versus double] Wayback!I think I'll make this into one or more switches.  Thanks for both your replies.  
I've experienced precision issues in the past with objects far from the origin, or very strange in scale.  It will be good to have a way to test the difference in quality and speed.
I think this is an interesting question.  Anyone with more wisdom or experience, please share!
(L) [2015/06/11] [graphicsMan] [Float versus double] Wayback!You can work around most precision issues with float, but in some edge cases, you have to be fancy.  Using double would probably reduce (or eliminate) your need for those workarounds, but keep in mind that when a problem exists for float, the same problem exists for double, just at a different scale.
(L) [2015/06/12] [mrluzeiro] [Float versus double] Wayback!I think I read something about "if you want to make a serious precision / correctness algorithm" you probably have to use fixed point math (and add as much bits as you need to get your desire precision).
Also, I already used a 2D library (i.e: polygon operation libs) that only uses integer data so they manage this way to make the algorithms to have mathematical correctness.
But this maybe is not feasible in RT.
 >> I have a mix of float and double types in the c++ code for my path tracing renderer
Maybe what you have is already the best option for you.
You may need to identify where are you loosing precision and why and how to minimize that issues.
Sample case: if you have a model in the center, and the same model very very very far away that you loose precision, you may want to keep that very far away model with his local position data and a double VEC3 to compute the offset / transformation. So you will always deal with local coords that float can handle..
My 2 cents.
(L) [2015/06/12] [graphicsMan] [Float versus double] Wayback!There has been work in both fixed-point hardware ray tracing and integer ray tracing.  See e.g. Johannes Hanika's dissertation and the work by Gribble et al.  (Note that just because people have done it doesn't mean it will be easy [SMILEY ;)] )
(L) [2015/06/15] [JasonSmith] [Float versus double] Wayback!I should say I haven't really run into any issues yet, I was just starting to feel like things were a bit messy, and I had a lot of redundant functions for different types in, for example, my vector class.  I really wanted to make most of my code more type-agnostic.
After some more searching, I see that the float/double question comes up a lot in general c++ development, as we would expect, and there is no real set answer, only tradeoffs like memory/precision.  Speed on modern computers seems like it's about equal between float/double with a bit of variance each way.  For example, some machines convert floats to doubles and then back to floats during float operations, which can actually make floats slower.
A friend who develops C++ code professionally suggested I look into templates, and this seems like it will be a nice way to clean up my code.  Templates will allow me to write my classes without code specific to float or double, and the compiler will create the right functions for the types I use at compile time.  This is an oversimplification, but that's the basic idea.
(L) [2015/06/17] [MohamedSakr] [Float versus double] Wayback!I would prefer floats, at least when you want to port your code to GPU it will be much easier, also floats are cheaper in memory operations, and better for SIMD.
precision issue isn't a real problem, most of the time in "real scenes" , there are options to generate bias "ray epsilon, sample intensity threshold, ..." as these errors comes from division by small numbers, which will happen also with doubles.
(L) [2015/07/12] [patlefort] [Float versus double] Wayback!I would use float unless you have specific needs for the precision of doubles. With floats, you can fit a vertex inside a single SSE register or you can perform 2x more operations at the same time. It'd be great if one day we can use doubles without worry but for now it's not worth the performance lost. As for templates, I wouldn't use it for that purpose for all your code. You would need to write everything in header files and use <float> everywhere (or more probably a #define MYREAL float/double if you want to switch from float or doubles anyways). I'd use templates only for small and very generic classes.
back