11
\$\begingroup\$

I'm currently writing a small cross-platform OpenGL-based 2D game engine for our studio. When I researched which 2D Vector class to use, I stumbled across three different design paradigms:

  • Float & Call-by-value, like in this Gamasutra article. Seems to be fast but offers little precision (see also this Thread). Pro: Fast, portable and compatible with most libraries.

  • Double & Call-by-reference. If I understand the above article right, I could also use 2 double precision variables instead of the 4 float numbers. According to the above thread double's still slower than float.

  • Template for double and float: The widely popular book "Game Engine Architecture" uses templates to make it possible to use float and double as needed. The obvious downside is code bloat. Also, I doubt it that the code can be optimized without basically writing two classes, anyway.

I would appreciate to learn what solutions you're using in your in-house engines and how precise e.g. popular game engines are, so I can decide what solution I'll implement in our engine. In the moment I'm thinking about simply using float precision and live with it.

\$\endgroup\$
5
  • \$\begingroup\$ Slower where? You can't talk about speed if that's not tied to a particular architecture. \$\endgroup\$
    – o0'.
    Commented Oct 1, 2010 at 9:33
  • 2
    \$\begingroup\$ @Lo'oris: I don't know any architecture where double is faster than float. \$\endgroup\$
    – user744
    Commented Oct 1, 2010 at 11:06
  • \$\begingroup\$ Rather than writing your own vector class, consider using Sony's (bulletphysics.org/Bullet/phpBB3/…). It is an easy drop-in header-only "library" that has already been micro-optimized, and all vector libraries are basically the same external interface anyway. \$\endgroup\$
    – user744
    Commented Oct 1, 2010 at 11:32
  • \$\begingroup\$ @Joe: 2 doubles instead of 4 floats? \$\endgroup\$
    – o0'.
    Commented Oct 1, 2010 at 11:41
  • \$\begingroup\$ @Lo'oris: The question is worded a little weird, I don't know if the questioner is confused or just you - you use two doubles "instead of" four floats when dealing with SIMD, because the register is 128 bits wide. You still need to process an equal number of numbers, and you're doing it at half-speed. \$\endgroup\$
    – user744
    Commented Oct 1, 2010 at 12:00

2 Answers 2

10
\$\begingroup\$

I always use float unless I have a reason to use double, much as I use int unless I have a reason to use int64_t.

Floats have no problem doing precise integer arithmetic up to 224, which is what most people are (mostly irrationally) afraid of. Doubles do not solve the problem of common values being unrepresentable exactly - whether you get 0.10000001 or 0.10000000000000001, you still must make sure your code considers it "equal" to 0.09999999.

Doubles are slower on every platform you'll care about writing games, take twice the memory bandwidth, and have fewer specialized CPU instructions available.

Single-precision floats remain the standard for hardware graphics interfaces, both on the C/C++ side and inside shaders.

You'll probably need a double vector at some point, but it should be used when needed, not by default.

As for templating - you're right, for maximum performance you'll want to specialize the templates manually anyway. Aside from that, I ran some tests for some templated code over the weekend (making a 4-ple IntRect and FloatRect), and found that the templated version took about 20x longer to compile than just repeating the code, which amounted to about 15 lines. Repetition sucks, but waiting for compiles sucks more - and it's not like I'm ever going to need a StringRect or FooRect.

\$\endgroup\$
1
  • \$\begingroup\$ "Doubles are slower on every platform […]" – Citation needed. \$\endgroup\$
    – Emil Laine
    Commented Jun 17, 2016 at 20:28
3
\$\begingroup\$

The Microsoft XNA game framework has a Vector2 class which uses floats. This would be my personal biggest reason for going with floats, as I trust the wisdom and experience of the XNA team to be much greater than my own.

I found it rather interesting to read the answer in this Stack Overflow question: "Float vs Double Performance". There is also a gamedev.net thread with some discussion of double performance in GPUs. I think it's safe to assume that doubles are slower than floats, at least in many GPUs, and I personally always used the float OpenGL functions over their double counterparts. This may not apply nowadays, but if you consider that not everyone who runs your game has the latest GPU with native double support, I think this is reason enough to use floats and gain that extra bit of performance.

\$\endgroup\$
5
  • 1
    \$\begingroup\$ From the SO question: "On x86 processors, at least, float and double will each be converted to a 10-byte real by the FPU for processing." While this is true, it neglects both SIMD instructions and the additional memory (= worse cache coherency, more memory bandwidth) requirements of doubles, both of which continue to make it slower than float in real-world tests. \$\endgroup\$
    – user744
    Commented Oct 1, 2010 at 12:02
  • \$\begingroup\$ I figured there was a catch. You should comment that onto the SO answer, I'd upvote it. \$\endgroup\$
    – Ricket
    Commented Oct 1, 2010 at 16:23
  • \$\begingroup\$ It is already there as a comment. \$\endgroup\$
    – user744
    Commented Oct 1, 2010 at 16:38
  • 1
    \$\begingroup\$ Since you brought up XNA, I may as well point out that Direct2D also uses single-precision floats. \$\endgroup\$ Commented Oct 1, 2010 at 19:23
  • \$\begingroup\$ And for completeness, OpenGL has float and double versions of its methods, so it's neutral in this sense. \$\endgroup\$
    – Ricket
    Commented Oct 2, 2010 at 22:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .