double or float?

STTrife
Posts: 109
Joined: Tue May 01, 2012 10:42 am

double or float?

Post by STTrife »

Ok I was surprised that I couldn't' find this question on the forums yet, but I was wondering what would be preferred: float or double.

It is probably dependent on your type of simulation etc. but how much does it differ? Is double slower than float? And in what cases are you going to have precision benefits from doubles?
So what kind of arguments should I base this decision on?
And is it true that the 'default' choice would be floats (so go for float unless you have a good reason to use doubles?), as that is the way it compiles when you don't specifically ask for doubles?

Thanks for the information!
Nickert
Posts: 25
Joined: Sat Dec 29, 2012 7:20 pm

Re: double or float?

Post by Nickert »

I believe this more or less answers your question: http://stackoverflow.com/questions/4584 ... -is-faster
MaxDZ8
Posts: 149
Joined: Fri Jun 24, 2011 8:53 am

Re: double or float?

Post by MaxDZ8 »

It's worth noticing there's no such thing as "x86 floating point", there's x87 floating point (albeit we can let it pass). It has been deprecated for years and supported for backwards compatibility only.
Nowadays, it's SSE floating point and when running on 32-bit data, it produces 32-bit results with 32-bit precision (sort of). SSE has a 128 bit registers so it will fit a 4D single precision vector. When running double, it's going to take at least twice as many registers and likely at least twice instructions. I'd be extremely surprised if it would turn out to be as fast as single precision on widespread hardware.
AVX has 256 bit registers, no idea how it performs though. But I don't think DP will be as fast as SP. AMD chips have an odd FP unit, and I'm afraid defining their 256-bit performance is going to require quite some discussion.

Using doubles allows you higher dynamic range and precision. Floating point computation works like this: when you add a large number to a small number, the small number might not have enough "importance" to flip even a single bit in the bigger one. Using doubles extends the range of numbers you can sum toghether with no/little information loss. Of course then we have brute magnitude and you get a smaller, more accurate "step" between numbers.

But in the real world you should really think twice before asking for doubles.
STTrife
Posts: 109
Joined: Tue May 01, 2012 10:42 am

Re: double or float?

Post by STTrife »

Thanks! Hmmm yeah i see the big and small number issue. What kind of large numbers should i think about of being problematic? In my world objects might be 10,000,000 meters apart but might move at normal speed. Would this give significant problems?

And if 10.000.000 is too large, what kind of numbers might still be acceptable with 32 bits floats?

Thanks a lot!
STTrife
Posts: 109
Joined: Tue May 01, 2012 10:42 am

Re: double or float?

Post by STTrife »

So I've been looking into floats, and I must say I'm a bit 'disappointed' with floats. If I understand correctly the 'significand' of a 32 bit float is only 23 bits. So for any number above 8,4 million it will not be able to have any decimal precision :(

I don't know how many decimal places bullet would need for an acceptable simulation, but even if you go over 8000, it will only leave 10 bits for the decimals, which will probably already give quite some bad artifacts in a very basic simulation I guess...

huge difference with doubles... I think I'll need to go with doubles if I want to keep things simple for my project. I don't care much about memory issues, and from the fragmented information I find on the internet, it doesn't seem like modern CPU's are much slower when working with doubles compared to working with floats.

If anyone has some detailed information specifically about bullet performance on modern 64bit CPU's when using floats or doubles, I'd be very interested in hearing it!