Summing floats, more than one way to do it

So you summed some floats and have some unexpected results? Don’t be afraid, it’s normal.

When summing floats you have errors, not only because some numbers can’t be represented exactly as a float, but you get errors from differences in magnitude too.

Say you have a large float and add a small one to it, the small one will loose a lot of it’s precision in the process. You can see what can happen if you use fixed-width decimal numbers:

12345.6 +

If you only have one addition, the error could be acceptable, but if you have to sum a lot of numbers the small ones could start to be a significant part of the sum, and that part can get lost in the process.

If you had:

12345.6 +
12345.65123 +

You just lost .1, even if you could have avoided that by reordering the operations:

    0.05123 +
    0.10246 +

So what can you do?

Actually, you have multiple choices, depending on what you want to achieve:

  • You can sort the numbers (small to big) and sum them after sorting. This will group together numbers of similar magnitude and it will yield smaller errors. But this is not the best method, there are more accurate methods that will not need a sort.
  • You can use Kahan summation. This is more accurate than sorting and it should be faster too.
  • You can use use doubles. This is the best method, but what if you’re summing a vector of doubles in the first place?
  • If you’re feeling adventurous you can run the sum on the FPU, but not many places let you insert assembly in the codebase.

If you want to see some code, you can download this file: Float sums example (1267). That code works on GCC 4.5 with the -std=c++0x flag.

Here is the output of a run, summing 10M random floats:

Starting: /home/florin/projects/floats/build/floats
2916.660400390625 <- Reverse sorted
3057.517578125 <- Normal sum
3192.88330078125 <- Sorted
3194.340576171875 <- Kahan summation
3194.340576171875 <- FPU
3194.340576171875 <- Double truncated
3194.34058601572951374691911041736602783203125 <- Double precision

How to check if a type is a function

The first time I saw variadic templates, I knew they are useful, as useful as lambdas in my opinion. Today we’ll take a look at how to check if some type is a function or not.

Keep in mind that this is compile time code.

The code goes something like this:

template <typename T>
struct is_fun
    static const bool value = false;

template <typename Return, typename... Args>
struct is_fun<Return(Args...)>
    static const bool value = true;

The code is using template specialization on function types. The variadic Args are there just so that we can catch any function, with any number of parameters — if you’re good at using the preprocessor you can do something with macros too, but that’s ugly.

If you want to use the value at runtime, there are a couple of ways to do it, one of them using the above code and decltype, and the one I’ll show you below, using the same method but function templates instead of structs.

template <typename T>
bool is_function(T t)
    return false;

template <typename Return, typename... Args>
bool is_function(Return(*)(Args...))
    return true;

That’s just the same thing, but using overloading instead of specialization.

You can use them like this:

    std::cout << is_fun<decltype(4)>::value << std::endl;

    std::cout << is_fun<decltype(test_fun)>::value << std::endl;

    std::cout << is_function(test_fun) << std::endl;

    std::cout << is_function(4) << std::endl;

where test_fun is defined like:

bool test_fun(float, int, bool) {
    return true;

How to use Xdebug from behind a NAT

I don’t work with PHP any more, except when I have to maintain a huge code-base that runs only on a specific version of PHP, on a specific version of a specific Linux distro and from a specific directory using a specific… you get the picture.

I run this thing in a virtual machine and I connect to it trough a VPN, so the only method to debug was print and error_log. For a year I kept trying to connect Xdebug back to Netbeans (yes, in PHP the debugger connects to the IDE) and for a year I kept ignoring the obvious: ssh.

The beauty of this is in its simplicity: just go into your specific version of php.ini (from your specific directory on your specific virtual machine) and set Xdebug to connect to localhost. I assume the rest of the configuration is using defaults, like port 9000 and stuff.

Now set up a reverse ssh tunnel to the server with port 9000 on both ends, something like:
ssh -R 9000:localhost:9000 user@server. You can do this with Putty too.

Fire up Netbeans and get your runtime config and file mappings right. You should now be able to debug the hell out of that code running on your specific… ok I’ll stop now.

Face tracking

Yesterday I started playing around with a Logitech QuickCam Orbit webcam. It has mechanical tilt and pan so it’s cool. The best part is that you can control it.

I’ve built a quick C++ library to move it and exported a class to Python using boost.python. After playing with OpenCV and writing a small python script, the webcam can track faces (its driver can do that too, but I was bored so I reinvented the wheel).

So, to cut the talk, here is the github repo for the code:

You can use the code to do other stuff, so pick and choose whatever bits you like. One note though: the code runs on Windows 64, I didn’t even try to compile under 32 bits.

The code is under LGPL so feel free to use it.