4
\$\begingroup\$

I'm currently selecting a C++ math library to use for a project. There are several questions here and on SO concerning the 'best' library available and many answers with different suggestions, but I would like to run some tests on each of them before I make a decision.

I would like to select a library that will cater for my current needs but also for any needs in any projects I may work on in the future. My problem is that due to being fairly new at this, I'm not entirely sure what those needs will be yet.

Aside from the points that I can think of listed below, and assuming that I will only test libraries with a suitable license, what other commonly used features should I test/look for before making a selection?

  • Matrix operations - including addition, subtraction, multiplication, inversion, transposition, translation, rotation, scale
  • Vector operations - cross/dot product, normalisation, zeroing, magnitude, length2, scalar operands, normal calculation
  • Euler angle + Quarternion operations - rotation, interpolation, conversion, inversion, multiplication
  • Coordinate system conversion (Cartesian, spherical)
  • Random numbers - generator speed, period, statistical randomness
  • Possibly noise generation
  • Ease of integration with gl functions like glTranslate()

Also, assuming these libraries have been tested for mathematical accuracy, are there factors other than ease of use (documentation, mailing list/developer access), speed (vectorisation, SIMD), and memory usage to consider?

\$\endgroup\$

4 Answers 4

1
\$\begingroup\$

Your list of functions looks pretty exhaustive, possibly containing more than you actually need.

Given the number of libs out there, my personal priority list when picking which one to use is:

  • license (GPL is out, unless project is GPL to begin with)
  • portability
  • comfortability of use - this is mostly personal opinion, and cause for the multitude of libs out there
  • documentation
  • capabilities
  • clarity of source code (so when docs run out, you still can figure it out)
  • performance

Most of the time, math lib performance isn't your bottleneck. This naturally depends on what you're expecting to do with it.. First make it work, then make it fast. Profile your code. Premature optimization, etc.

Also, if you're integrating some physics package, it's bound to have a math lib integrated. You might want to check it out before adding another one.

\$\endgroup\$
1
  • \$\begingroup\$ If he is working with 3D graphics, then he'll need all that. \$\endgroup\$
    – akaltar
    Commented Apr 8, 2015 at 19:13
4
\$\begingroup\$

This may not be the best answer for this question but during game development you may encounter many more greater challenges than the performance and stability of math library you are using. I'm not a man of c++ but before a math library what you have to decide is

1 ) Your game, your needs

2 ) The most suitable framework for your game or no framework if you want to let you develop it asap. And is consistent at least with its graphics and maths.

The most common mistake around programmers including me is to lost your way around frameworks and libraries and spent great amount of time on what to use instead of real usable code production.

\$\endgroup\$
2
\$\begingroup\$

Since you mentioned noise generation and random numbers I'd like to answer with a slight different response: portability.

Most math libraries are going to target specific systems such as little endian win32 platforms. This is fine if you are only planning on meeting those targets but is really going to hold you back if you ever plan on porting to a portable device or - and this is the main reason for my response - porting your algorithms to shaders.

Perlin noise is a great example but a random number generator could also be ported as well as vector operations to shader work. GPUs are pretty much designed for bulk vector operations and if you plan on using the result for GPU work you now have the result in GPU memory.

Additionally you should also consider threading whether or not you port to other devices. With threading becoming a larger and more important I highly recommend looking for or designing libraries with support for multithreading. For example if a 4x4 Matrix class can be shared across threads you probably should have some locks around common operations so you don't read it in a half modified state. This really applies to random number generators more than anything though it isn't an issue if you aren't looking for determinism.

\$\endgroup\$
1
\$\begingroup\$

The library should not be a problem for your project. If it is your case that means that you are coupling your system with a library: this generally is not good idea. What if another better library emerges? And if the library you choose dies?

Try to stay orthogonal to the implementation of every basic library (unless you are coding a thin wrapper around it) and focus on your needs: this will decrease the amount of work need when/if you will need to change library.

\$\endgroup\$

You must log in to answer this question.

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