Float versus double

Practical and theoretical implementation discussion.
JasonSmith
Posts: 7
Joined: Wed Oct 15, 2014 4:07 am
Contact:

Float versus double

Postby JasonSmith » Wed Jun 10, 2015 6:41 am

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?

mrluzeiro
Posts: 28
Joined: Tue May 26, 2015 2:27 pm

Re: Float versus double

Postby mrluzeiro » Wed Jun 10, 2015 1:14 pm

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..

szellmann
Posts: 51
Joined: Fri Oct 10, 2014 9:15 am
Contact:

Re: Float versus double

Postby szellmann » Wed Jun 10, 2015 8:17 pm

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

JasonSmith
Posts: 7
Joined: Wed Oct 15, 2014 4:07 am
Contact:

Re: Float versus double

Postby JasonSmith » Wed Jun 10, 2015 9:29 pm

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!

graphicsMan
Posts: 160
Joined: Mon Nov 28, 2011 7:28 pm

Re: Float versus double

Postby graphicsMan » Thu Jun 11, 2015 5:30 pm

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.

mrluzeiro
Posts: 28
Joined: Tue May 26, 2015 2:27 pm

Re: Float versus double

Postby mrluzeiro » Fri Jun 12, 2015 8:35 am

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.

graphicsMan
Posts: 160
Joined: Mon Nov 28, 2011 7:28 pm

Re: Float versus double

Postby graphicsMan » Fri Jun 12, 2015 3:50 pm

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 ;) )

JasonSmith
Posts: 7
Joined: Wed Oct 15, 2014 4:07 am
Contact:

Re: Float versus double

Postby JasonSmith » Mon Jun 15, 2015 3:29 pm

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.

MohamedSakr
Posts: 83
Joined: Thu Apr 24, 2014 2:27 am

Re: Float versus double

Postby MohamedSakr » Wed Jun 17, 2015 5:33 am

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.

patlefort
Posts: 9
Joined: Sun Jun 21, 2015 11:11 pm

Re: Float versus double

Postby patlefort » Sun Jul 12, 2015 4:54 am

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.


Return to “General Development”

Who is online

Users browsing this forum: No registered users and 7 guests