PDA

View Full Version : Single or doubles?



cragwolf
11-04-2005, 11:05 PM
For my game engine, I'm defining records like TVector, TMatrix, TPoint, TRay, TPlane, and so on, but I don't know whether I should make the the type of their members single or double. I suppose I could define two versions, e.g. TVectors, and TVectord, for single and double, but then, which one do I actually use?

Related, many OpenGL functions tend to have two versions: one for doubles (glBlahd) and one for floats (glBlahf). Which one should I use in my game engine?

M109uk
12-04-2005, 01:15 AM
It doesnt really matter which one you go for, the difference is that a double has more significant digits than a single...

From the help file:


Type Range Digits Size (Bytes)
-----------------------------------------------------------------------------
Single 1.5 x 10^-45 .. 3.4 x 10^38 7-8 4
Double 5.0 x 10^-324 .. 1.7 x 10^308 15-16 8


hope helps :)

cragwolf
12-04-2005, 04:05 AM
But what about speed and memory issues? I think I may define my own type bmFloat, define that to be a Single and profile my program; then I'll define it to be a Double and profile it again; and finally compare results. Trouble with that method is that it's all or nothing. Maybe it makes sense to have some variables be Double and some variables be Single, depending on how they are used, rather than all Single or all Double. I dunno, just thinking out loud.

Sly
12-04-2005, 05:00 AM
Commercial games use single precision floats for speed and size reasons. Games do not require the higher precision that doubles give.

marcov
12-04-2005, 07:21 AM
Commercial games use single precision floats for speed and size reasons. Games do not require the higher precision that doubles give.

Mostly also because 3DNow (and SSE) can pack multiple singles into a double.

Single takes less memory (thus better cache util), however double is 8 bytes aligned. Hard to say what would be faster, needs profiling

siim
12-04-2005, 10:05 AM
var
//f, pi: Single;
i, j, time: Integer;
f, pi: Double;
begin
time := GetTickCount;
for j := 0 to 999999 do
begin
pi := System.Pi;
f := 1.0;
for i := 0 to 10 do
begin
f := f*pi;
f := f*pi;
f := f*pi;
f := f*pi;
end;
end;
ShowMessage(IntToStr(GetTickCount-time));


This code gave 600ms for Single and 800ms for Double on my machine

cragwolf
14-04-2005, 01:06 AM
Thanks for your replies everyone.