Float or double?

In scientific computation we use floating point numbers a lot. This article is a guide to picking the right floating point representation for you. In most programming languages there are two built-in precisions to pick from: 32-bit (single-precision) and 64-bit (double-precision). In the C family of languages these are known as float and double, and those are the names I will use in this article. There are other precisions: half, quad etc. I won't cover these here, but a lot of the discussion makes sense for half vs float or double vs quad too. So to be clear: I will only talk about 32-bit and 64-bit IEEE 754 here.

This article is also written for you who have a lot of data. If you need a few numbers here and there, just use double and don't think more about it!

The article is split into two separate (but related) discussions: what to use for storing you data, and what to use during calculations on your data. Sometimes you want to store your data with float but do calculations using double.

Read More

Stick to UTF-8 and these three character sets

Handling text is an important part of every programmers life, and there are many subtleties to it. Lately I have started to realize that all text I handle in my everyday work falls into three categories:

  • Free text: this includes names of people and places, contents of chat messages, books, etc.
  • Code: this includes C++, JSON, HTML, etc – anything that is both human and machine readable and writable.
  • Identifiers: these are unique names, e.g. instance keys in JSON, file names, database keys and resource names.

I think we as programmers can make our lives much simpler if we decide to stick to these three categories, and to all agree on their character sets and encodings.

Read More

Simple coroutines for games in C++

In games it is very common to have scripted sequences - for instance, a piece of dialogue or a cutscene. I don’t mean these sequences are written in a scripting language (though they might), but that they follow a script like a play or a movie does. Of course, contrary to a movie script in a game the script may have some conditional branches (an NPC only saying something if the player choses to ask a certain question, for instance).

Expressing such a sequence in code should be straight-forward  – after all, computer code is very much like a script. However, these scripts are often span minutes and happen while a bunch of other things are also happening. The now obvious solution to this is to put these in a separate thread - but now you open yourself up to race conditions and other nasty thread-related bugs. So what to do?

Read More

Error Context - a stack trace for data

Often a crash is the result of unexpected data. For instance, a crash could be caused by an extra space at the end of a string, or an integer being negative when your code didn't expect it to be. Or maybe the crash happened during the processing of a bunch of files and you want to know the name of the file that caused the crash. What you really want is the ability to inspect the values of arguments and locals at different parts of the stack. But how can we write that data to a log file on a crash without writing it when there is no crash?

Read More

The fastest code is the code that never runs

It was early 2014, and I was working at Arrowhead Game Studios on our reboot of the classic arcade game Gauntlet. It was the week before we where going to present our game to the world for the very first time. We where going to let the public play a select level from the game, and over the last weeks the team had been busy fixing bugs, refining gameplay and tuning the visuals. It was looking good, but during the last week something happened: the frame rate dropped from the desired 60 FPS to a crawling 20-25 FPS. Panic! What had gone wrong? And did we have time to fix it?

Read More

Lessons after a year with Lua

When I started working at Arrowhead Game Studios a little over a year ago, I came from over a decade of using statically typed languages, the main one being C++. At Arrowhead we wrote most of our game code in Lua, which is a small but sweet scripting language. In this article I will summarize the main lessons I've learned, and how scripting languages in general, and Lua in particular, related to statically typed, compiled languages such as C++.

Read More