Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java raytracing float vs double

Last semester we had to develop a raytracer for a course at school. School being done, I tried to fiddle a bit with it.

I wanted to see what would change if I changed all floating point calculations from double to float (all calculations being done with doubles). So I changed every variable to type float, and simply cast each double returned from the Math methods to float. Testing my raytracer on a couple of scenes showed a pretty decent performance increase.

Searching around the web I found various reasons why float could be faster, but also some saying that double could be as fast, or even faster in a 64-bit environment. The thing is, I am running in a 64-bit environment and JVM. What would be the reason for this increased performance?

Now, I'm reading the PBRT book and planning to rewrite a raytracer from scratch following this. Would choosing float for all floating point calculations pose any problems? I didn't notice any during my tests, but perhaps for certain scenes the lower precision might (intersection testing seems like where it could pose a possible problem)? Or perhaps a tradeoff, like using double for the critical tests and float for less critical calculations? I would have to use an other Math library to get rid of the casting, would I be going the float way.

like image 441
Koekje Avatar asked Jun 25 '15 16:06

Koekje


People also ask

Is it better to use float or double?

Double is more precise than float and can store 64 bits, double of the number of bits float can store. Double is more precise and for storing large numbers, we prefer double over float.

Are floats more efficient than doubles?

Floats are faster than doubles when you don't need double's precision and you are memory-bandwidth bound and your hardware doesn't carry a penalty on floats. They conserve memory-bandwidth because they occupy half the space per number.

Why is double preferred over float?

A double-type is preferred over float-type if the more precise and accurate result is required. The precision of double-type is up to 15 to 16 decimal points while the precision of float type is only around 6 to 7 decimal digits.

Do floats use less memory than doubles?

double has higher precision, whereas floats take up less memory and are faster.


1 Answers

There is pretty much no difference between float and double when it comes to speed of calculation, as far as desktop processors are the platform. The difference can only come from the increased memory bandwidth requirements because doubles require twice as much space.

Its different for GPU based calculations, those are more tailored for float and e.g. Nvidia GPU's break down considerably for double.

I'd go with a mixed approach; store data like polygons with float precision, but do all calculations in double. Small memory footprint, high precision - win-win.

like image 192
Durandal Avatar answered Sep 28 '22 07:09

Durandal