Floating point numbers are a mainstay of programming in areas such as games, graphics, and simulation. On the whole, they are easy and intuitive to use. However, they have certain quirks and issues to be aware of. For example, their representation is inherently flexible and often approximate. This means there’s no definitive answer to the question of how many decimal places they can hold.

By looking at the way floating point numbers are stored, it’s possible to understand why this happens, and what precision is likely to be available. Hypothetically, it’s possible under certain circumstances to get up to about 45 decimal places in a C++ float, and 324 in a double. However, as we’ll see in this post, it depends on context.Read more Decimal places in a floating point number ›

I’ve been very excited to see that Visual Studio 2015 supports the constexpr keyword in C++. It was introduced in the C++11 standard, and is being taken further in upcoming revisions.

There are a number of uses for the keyword, but the one which excites me the most is using it for writing functions which can be executed during compilation, potentially saving a lot of runtime overhead. In this post, I’ll show a couple of quick examples.Read more C++ constexpr functions ›

The standardisation of size-specific integer types in C/C++ is extremely useful for portability. That is, when you use types like uint16_t and int32_t, you know exactly what size of data type you’re getting (assuming your compiler supports it, which is usually the case unless you’re working with very specialist/embedded systems). This isn’t the case with the more traditional types like short and int whose sizes can vary from one compiler to another. However, the size is obviously part of the type name, meaning you need to alter declarations directly if you want to use a type of a different size.

Today I ran across an interesting little quirk of C++11. You can declare a pure virtual function which has no implementation and which is final. That means the class can never be instantiated or inherited, and the function will never have a body.

The low-level details of how data gets passed into a function are often overlooked by programmers. We obviously care about passing parameters by value vs. reference, and perhaps also by copy vs. move, but it’s easy to ignore anything deeper than that.

According to the C++ standard, the new operator should throw std::bad_alloc if it fails. This will typically happen if your process has run out of memory. However, this isn’t the case if your program uses the (rather outdated) Microsoft Foundation Classes. In this post, we’ll look at what’s going on, and what you can do about it.Read more MFC prevents bad_alloc from being thrown ›

Sometimes it’s useful to output the binary representation of a number in text, i.e. as an ASCII string of 0’s and 1’s. There are also situations where you might want convert back the other way, e.g. if you want to let a user enter a binary string manually. The bitset class in C++ makes this surprisingly quick and easy.Read more Convert a number to a binary string (and back) in C++ ›

When you’re dealing with a large program and multiple developers, it’s not always obvious how and when certain things get executed. One of the very useful ways to debug unexpected behaviour is to set a breakpoint on a suspect line of code, and examine the callstack when it gets hit to see the execution path.

For infrequent events, it’s not always desirable to halt the entire program while you do that though. Instead, you can tell Visual Studio to write the callstack to the output when the breakpoint gets hit, and immediately continue execution.