Friday, December 17, 2010

Odd Math.Round() behaviour in Mono (same as .NET?)

I had read on a forum somewhere that Math.Round() rounded to the closest even number. I mentioned this to the guys in #mosa and one mentioned that seems like it would be a bug, as it should go from 4.5 -> 5, rather than the (supposedly) expected 4.5 -> 4, with 4 being the closest even number. Another example is 9.5 -> 10 because 10 is the closest even number (9 is odd).

I wrote a simple method to test this.



using System;

namespace round_test
{
class MainClass
{
public static void Main (string[] args)
{
double x = 0d;

while (x < 5d)
{
Console.WriteLine("actual: " + x.ToString());
Console.WriteLine("rounded: " + Math.Round(x).ToString());

x += 0.1d;
}
}
}
}


The output using mono wasn't very consistent. For instance, 2.5 -> 3 while the rest of the n.5 -> closest even number to n (4.5 -> 4, 3.5 -> 4). (full output here). Odd behaviour, bug??


EDIT: It seems there are two types of rounding, explained on MSDN. The default is banker's rounding.

Thursday, December 9, 2010

Dark theme extension for Wikipedia

I spend a lot of time on wikipedia. A lot of times, to wind down the day, I will just get lost on wikipedia for an hour or two before bed. This is problematic though, because staring into a light bulb for a few hours (in a dark room) right before bed doesn't play well with trying to sleep. I found a neat script to darken the theme of wikipedia, but it hadn't been updated in a while and didn't quite work with the new theme they are using. I took about an hour today to get it running well enough for me, fixing some things the original author left out (like better link colors) and removing other fluff. The results are shown below:



If you would like to install the script (Greasemonkey, but just works on Chrome), click here.

Tuesday, December 7, 2010

Lost ticket revenue in Arlington, TX

The Court here in Arlington offers a (5671 page) PDF with all the arrest warrants in the city for unpaid tickets. It turns out our city is currently missing $56,594,788.04 in revenue for tickets not paid.

If you download the PDF, you can add up the totals yourself. Convert it to HTML using pdftohtml and run:

k=0;for i in `cat outstanding_warrantss.html | egrep -o '[0-9]{1,6}\.[0-9]{2,}'`; do k=`echo "$k+$i" | bc`; done; echo $k


All in all:

bperry@bperry-desktop:~/Downloads$ k=0;for i in `cat outstanding_warrantss.html | egrep -o '[0-9]{1,6}\.[0-9]{2,}'`; do k=`echo "$k+$i" | bc`; done; echo $k
56594788.04
bperry@bperry-desktop:~/Downloads$ time $(k=0;for i in `cat outstanding_warrantss.html | egrep -o '[0-9]{1,6}\.[0-9]{2,}'`; do k=`echo "$k+$i" | bc`; done; echo $k)
56594788.04: command not found

real 10m45.130s
user 0m8.470s
sys 1m43.170s
bperry@bperry-desktop:~/Downloads$


We need a Dog the Bounty Hunter. That is quite a bit of monies.