1. A very funny thing happened today.

    I believe in some language or another (can't remember which), the Abs function is actually a member function - x.abs() instead of abs(x). It also means you can do something like -1.abs(). I wanted to try to emulate that sort of thing with extensions methods. So, I wrote this:

    public static int Abs(this int value)
    {
        return Math.Abs(value);
    }
    

    Simple enough. So I'm writing code to find the determinant of a matrix (via Expansion by Minors), and the return values are coming out all wrong. Sometimes they were right, sometimes they were the wrong sign, and sometimes they were way off. I checked the code a hundred times until I came back to this:

    for (int i = 0; i < matrix.Size; i++)
        output += matrix[0, i] * matrix.Minor(0, i) * -1.Pow(i);
    

    Looks fine at first glance... to double check, I put each part of the equation into their own variables. During debug mode... -1.Pow(0) = -1!? That's not right!! Oh wait...

    for (int i = 0; i < matrix.Size; i++)
        output += matrix[0, i] * matrix.Minor(0, i) * (-1).Pow(i);
    

    ...problem solved :P

    I now see why abs(x) is better than x.abs()
    0

    Add a comment

Blog Archive
Active Projects
Active Projects
Total Pageviews
Total Pageviews
10816
Loading