<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Stefano Ricciardi &#187; tips</title>
	<atom:link href="http://stefanoricciardi.com/tag/tips/feed/" rel="self" type="application/rss+xml" />
	<link>http://stefanoricciardi.com</link>
	<description>On Software Development and Thereabouts</description>
	<lastBuildDate>Thu, 02 Sep 2010 15:01:11 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Comparing Floating Point Numbers</title>
		<link>http://stefanoricciardi.com/2010/03/02/comparing-floating-point-numbers/</link>
		<comments>http://stefanoricciardi.com/2010/03/02/comparing-floating-point-numbers/#comments</comments>
		<pubDate>Tue, 02 Mar 2010 13:29:00 +0000</pubDate>
		<dc:creator>stefanoricciardi</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://stefanoricciardi.wordpress.com/?p=807</guid>
		<description><![CDATA[Floating Points and Rounding Errors. Working with floating points number can sometimes provide some (un)pleasant surprise, since many real numbers do not have a finite representation and this can lead to rounding errors. If you need to refresh your knowledge (and have some time to spare), you can find a great treatment in the article [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fstefanoricciardi.com%2F2010%2F03%2F02%2Fcomparing-floating-point-numbers%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fstefanoricciardi.com%2F2010%2F03%2F02%2Fcomparing-floating-point-numbers%2F&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<h2>Floating Points and Rounding Errors.</h2>
<p>Working with floating points number can sometimes provide some (un)pleasant surprise, since many real numbers do not have a finite representation and this can lead to rounding errors. </p>
<p>If you need to refresh your knowledge (and have some time to spare), you can find a great treatment in the article <a href="http://docs.sun.com/source/806-3568/ncg_goldberg.html">What Every Computer Scientist Should Know About Floating-Point Arithmetic</a>:</p>
<blockquote><p>
Squeezing infinitely many real numbers into a finite number of bits requires an approximate representation. Although there are infinitely many integers, in most programs the result of integer computations can be stored in 32 bits. In contrast, given any fixed number of bits, most calculations with real numbers will produce quantities that cannot be exactly represented using that many bits. Therefore the result of a floating-point calculation must often be rounded in order to fit back into its finite representation. This rounding error is the characteristic feature of floating-point computation.
</p></blockquote>
<p>A small example should make the problem clearly visible:</p>
<pre class="brush:csharp">
class Program
{
    static void Main(string[] args)
    {
        double a = 22.4;
        double b = a / 131;

        double c = 0;
        for (int i = 0; i < 131; i++)
        {
            c += b;
        }

        Console.WriteLine("Are they equal?: {0}", a == c);
        Console.WriteLine("Difference (a - c) is {0} ", a - c);
    }
}
</pre>
<p>Here we are taking a double (a), diving it by 131 and then creating a new number (c) by adding the result of that division 131 times. By pure math, a and c should be equal. If you try this small program yourself, this is the output that you would get:</p>
<pre>
Are they equal?: False
Difference (a - c) is -4,9737991503207E-14
</pre>
<p>As you can see, the result is not what you might expect, and it's affected by a rounding error that is building up every time b is added to the partial result c.</p>
<p>Note that this is dependent on the number that I have chosen. If you substitute 131 with 5, you will not experience this problem.</p>
<h2>Your Own Comparer</h2>
<p>Sometimes this behavior might annoy you, especially when you don't need such a high precision and a “rough” equality is all you need. A typical situation is unit testing an algorithm against some expected result: your assertions may fail because of a rounding error, even though the logic of your algorithm is formally correct.</p>
<p>In such a case, you might want to implement your own comparer. A desirable property for such a comparer is a threshold value: if the difference between two floating points is <em>below</em> that threshold they are considered equal.</p>
<p>All you need is to implement the .NET interface <code><a href="http://msdn.microsoft.com/en-us/library/ms132151.aspx">IEqualityComparer</a></code><a href="http://msdn.microsoft.com/en-us/library/ms132151.aspx"> </a>interface.</p>
<pre class="brush:csharp">
class DoubleComparer : IEqualityComparer
{
    private readonly double threshold;

    public DoubleComparer(double threshold)
    {
        this.threshold = threshold;
    }

    public bool Equals(double x, double y)
    {
        return Math.Abs(x - y) < this.threshold;
    }

    public int GetHashCode(double obj)
    {
       return obj.GetHashCode();
    }
}
</pre>
<p>I have created my own comparer to use in my unit tests. I only have to implement 2 methods: <code>Equals()</code> and <code>GetHashCode()</code>.</p>
<p>I don't care much for the actual algorithm of the hashcode implementation, so I am reusing <code>double</code> own algorithm.</p>
<p>Instead, I have applied my own logic to the Equals method: to me, two doubles are equal if the absolute value of their difference is within a given threshold (which I can pass to the comparer in the constructor).</p>
<h2>The Comparer In Use</h2>
<p>Suppose I have an algorithm to calculate the histogram of a given collection of doubles, and a unit test to validate the calculation against a collection of expected results.</p>
<p>In the example below, <em>samples</em> are the input values which I want to create the histogram for, and <em>expectedBreaks</em> is a sequence containing the left and right limits of the bins of the histogram (in the example I am requiring 5 bins, so the number of breaks is 5 + 1 = 6).</p>
<pre class="brush:csharp">
var p = new HistogramProvider();
double[] samples = { 20, 12, 4, 22, 19, 24, 9, 5, -12.2, 13, 0 };
double[] expectedBreaks = { -12.2, -4.96, 2.28, 9.52, 16.76, 24 };

var histogram = p.CreateHistogram(samples, 5);

Assert.IsTrue(expectedBreaks.SequenceEqual<double>(
                       histogram.Breaks,
                       new DoubleComparer(0.01)));
</pre>
<p>I have figured out the threshold to pass (0.01) based on the relative size of the doubles under analysis. Of course, you should use your own judgement to come up with a sensible threshold according of the range of your input data.</p>
<p><a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fstefanoricciardi.com%2f2010%2f03%2f02%2fcomparing-floating-point-numbers%2f"><img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fstefanoricciardi.com%2f2010%2f03%2f02%2fcomparing-floating-point-numbers%2f" border="0" alt="kick it on DotNetKicks.com" /></a></p>
<p><a rev="vote-for" href="http://dotnetshoutout.com/Comparing-Floating-Point-Numbers"><img alt="Shout it" src="http://dotnetshoutout.com/image.axd?url=http%3A%2F%2Fstefanoricciardi.com%2F2010%2F03%2F02%2Fcomparing-floating-point-numbers%2F"></a></p>
]]></content:encoded>
			<wfw:commentRss>http://stefanoricciardi.com/2010/03/02/comparing-floating-point-numbers/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Parsing Dates from CSV files with FileHelpers Library</title>
		<link>http://stefanoricciardi.com/2009/10/07/parsing-dates-from-csv-files-with-filehelpers-library/</link>
		<comments>http://stefanoricciardi.com/2009/10/07/parsing-dates-from-csv-files-with-filehelpers-library/#comments</comments>
		<pubDate>Wed, 07 Oct 2009 20:08:37 +0000</pubDate>
		<dc:creator>stefanoricciardi</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Libraries]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://stefanoricciardi.net/?p=400</guid>
		<description><![CDATA[In my current project I have found myself needing to parse a CSV file coming from a legacy C++ application. This is a fairly common task, and in the past I have always written the parsing engine myself, especially when there was some validation related to the business logic involved. This time I have chosen [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fstefanoricciardi.com%2F2009%2F10%2F07%2Fparsing-dates-from-csv-files-with-filehelpers-library%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fstefanoricciardi.com%2F2009%2F10%2F07%2Fparsing-dates-from-csv-files-with-filehelpers-library%2F&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p>In my current project I have found myself needing to parse a CSV file coming from a legacy C++ application. This is a fairly common task, and in the past I have always written the parsing engine myself, especially when there was some validation related to the business logic involved.</p>
<p>This time I have chosen to try the reportedly excellent <a href="http://www.filehelpers.com/">FileHelpers</a> library. Why reinvent the wheel yet another time? The library is well documented and in a matter of minutes I was able to start experimenting with some test data.</p>
<p>To simplify the discussion, suppose that the CSV file has the following structure:</p>
<pre>
# City, Date, Temperature
Chicago; 20090913 07:00:00; 62.23
New York; 20090913 08:00:00; 81.3
</pre>
<p>Each row of the CSV file is mapped to the following class:</p>
<pre class="brush:csharp">
[DelimitedRecord(&quot;;&quot;)]
[IgnoreEmptyLines()]
[ConditionalRecord(RecordCondition.ExcludeIfBegins, &quot;#&quot;)]
public class CsvRecord
{
    public string City;

    [FieldConverter(ConverterKind.Date, &quot;yyyyMMdd HH:mm:ss&quot;)]
    public DateTime Date;

    [FieldConverter(ConverterKind.Double, &quot;.&quot;)]
    public double Temperature;
}
</pre>
<p>The attributes are pretty self-explanatory: they specify which is the separator value and which lines can be ignored.</p>
<p>The interesting part here is the way FileHelpers allows to specify a pattern to convert a string into a <code>DateTime</code> value. In this case, the pattern used here is <code>yyyyMMdd HH:mm:ss</code> to match the CSV file.</p>
<p>Once the mapping between the CSV and the .NET object is setup, I have created a class wrapping the FileHelpers engine (just in case some day I want to change the parsing engine), with the following (simplified) method:</p>
<pre class="brush:csharp">
public IEnumerable&lt;CsvRecord&gt; GetRecordsFromFile(Stream s)
{
   FileHelperEngine engine = new FileHelperEngine(typeof(CsvRecord));
   s.Seek(0, SeekOrigin.Begin);

   CsvRecord[] records = engine.ReadStream(new StreamReader(s)) as CsvRecord[];
   return records;
}
</pre>
<p>This was working like a charm&#8230; until I moved the code to a separate test machine. All of a sudden the parser would complain that it could not parse the dates from the file. After a lot of thought, I realized that the test machine had a different regional settings: while my machine had English formats, the test machine had Italian formats. And for some reason FileHelpers was not able to work correctly in this scenario (even though I had explicitly specified the date and time pattern in the <code>CsvRecord</code> class).</p>
<p>Luckily, FileHelpers lets you specify your own converters if the predefined ones won&#8217;t do. I therefore created my custom converter for the dates as follows:</p>
<pre class="brush:csharp">
public class CustomDateTimeConverter : ConverterBase
{
    private const string DateTimeFormat = &quot;yyyyMMdd HH:mm:ss&quot;;

    public override object StringToField(string from)
    {
        return DateTime.ParseExact(from, DateTimeFormat, CultureInfo.InvariantCulture);
    }
}
</pre>
<p>and rewritten the <code>CsvRecord</code> class as follows:</p>
<pre class="brush:csharp">
[DelimitedRecord(&quot;;&quot;)]
[IgnoreEmptyLines()]
[ConditionalRecord(RecordCondition.ExcludeIfBegins, &quot;#&quot;)]
public class CsvRecord
{
    public string City;

    [FieldConverter(typeof(CustomDateTimeConverter))]
    public DateTime Date;

    [FieldConverter(ConverterKind.Double, &quot;.&quot;)]
    public double Temperature;
}
</pre>
<p>This time it worked also with Italian locale.</p>
<p>Frankly, I don&#8217;t know why the FileHelpers class had problems with the Italian locale (possibly with locales different from English ones). Probably having a look at the source code would reveal the truth.</p>
<p>I suspect that it is parsing the dates with a given format but without specifying an invariant culture format provider. But that is just a wild guess.</p>
<p><!-- AddThis Button BEGIN --></p>
<p><a href="http://www.addthis.com/bookmark.php?v=250"><img style="border:0;" src="http://s7.addthis.com/static/btn/lg-share-en.gif" alt="Bookmark and Share" width="125" height="16" /></a></p>
<p><!-- AddThis Button END --></p>
<div id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:e6ceeafa-fe5c-4bd6-a48d-325da43e5abc" class="wlWriterEditableSmartContent" style="display:inline;float:none;margin:0;padding:0;">Technorati Tags: <a rel="tag" href="http://technorati.com/tags/Programming">Programming</a>, <a rel="tag" href="http://technorati.com/tags/C%23">C#</a></div>
]]></content:encoded>
			<wfw:commentRss>http://stefanoricciardi.com/2009/10/07/parsing-dates-from-csv-files-with-filehelpers-library/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Visual Studio Tip: No Regions Around Implemented Interfaces</title>
		<link>http://stefanoricciardi.com/2009/09/10/visual-studio-tip-no-regions-around-implemented-interfaces/</link>
		<comments>http://stefanoricciardi.com/2009/09/10/visual-studio-tip-no-regions-around-implemented-interfaces/#comments</comments>
		<pubDate>Thu, 10 Sep 2009 10:25:59 +0000</pubDate>
		<dc:creator>stefanoricciardi</dc:creator>
				<category><![CDATA[Tools]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[Visual Studio 2008]]></category>

		<guid isPermaLink="false">http://stefanoricciardi.net/?p=344</guid>
		<description><![CDATA[One of the (many) useful features of Visual Studio is the Implement Interface. With it, if you declare a class as implementing a particular interface, you can have Visual Studio generate stubs for all the methods declared by that interface. With default settings, Visual Studio will place regions around the stubs that it just generated: [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fstefanoricciardi.com%2F2009%2F09%2F10%2Fvisual-studio-tip-no-regions-around-implemented-interfaces%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fstefanoricciardi.com%2F2009%2F09%2F10%2Fvisual-studio-tip-no-regions-around-implemented-interfaces%2F&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p>One of the (many) useful features of Visual Studio is the Implement Interface. With it, if you declare a class as implementing a particular interface, you can have Visual Studio generate stubs for all the methods declared by that interface.</p>
<p><a href="http://69.175.59.226/~stefano3/blog/wp-content/uploads/2009/09/testimplementinterfacenoregion1.jpg"><img class="aligncenter size-full wp-image-345" title="Visual Studio Implement Interface" src="http://69.175.59.226/~stefano3/blog/wp-content/uploads/2009/09/testimplementinterfacenoregion1.jpg" alt="Visual Studio Implement Interface" width="522" height="176" /></a></p>
<p>With default settings, Visual Studio will place regions around the stubs that it just generated:</p>
<p>
<a href="http://69.175.59.226/~stefano3/blog/wp-content/uploads/2009/09/testimplementinterfacenoregion21.jpg"><img src="http://69.175.59.226/~stefano3/blog/wp-content/uploads/2009/09/testimplementinterfacenoregion21.jpg" alt="Visual Studio putting regions around implemented interfaces" title="Visual Studio putting regions around implemented interfaces" width="480" height="305" class="aligncenter size-full wp-image-350" /></a>
</p>
<p>As you may know from previous posts, I am not a fan of using regions within C# code. I tend to favor lean classes and short methods, I find regions almost useless; worse, they tend to visually clutter the code.</p>
<p>
Luckily, there&#8217;s a way to configure Visual Studio not to put regions around implemented interfaces. It&#8217;s buried under Tools-&gt;Options-&gt;Text Editor-&gt;C#-&gt;Advanced. Simply uncheck the &#8220;Surround generated code with #region&#8221; in the &#8220;Implement interface&#8221; group: </p>
<p>
<a href="http://69.175.59.226/~stefano3/blog/wp-content/uploads/2009/09/testimplementinterfacenoregion3.jpg"><img src="http://69.175.59.226/~stefano3/blog/wp-content/uploads/2009/09/testimplementinterfacenoregion3.jpg" alt="Configuring Visual Studio to remove regions around interfaces" title="Configuring Visual Studio to remove regions around interfaces" width="600" height="356" class="aligncenter size-full wp-image-352" /></a>
</p>
<p>
Voilà. You now have all your stubs ready to be implemented, without those annoying regions around:
</p>
<p>
<a href="http://69.175.59.226/~stefano3/blog/wp-content/uploads/2009/09/testimplementinterfacenoregion4.jpg"><img src="http://69.175.59.226/~stefano3/blog/wp-content/uploads/2009/09/testimplementinterfacenoregion4.jpg" alt="Interfaces implemented without regions around" title="Interfaces implemented without regions around" width="486" height="248" class="aligncenter size-full wp-image-353" /></a>
</p>
<p><!-- AddThis Button BEGIN --></p>
<p><a href="http://www.addthis.com/bookmark.php?v=250"><img style="border:0;" src="http://s7.addthis.com/static/btn/lg-share-en.gif" alt="Bookmark and Share" width="125" height="16" /></a></p>
<p><!-- AddThis Button END --></p>
<div id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:e6ceeafa-fe5c-4bd6-a48d-325da43e5abc" class="wlWriterEditableSmartContent" style="display:inline;float:none;margin:0;padding:0;">Technorati Tags: <a rel="tag" href="http://technorati.com/tags/Programming">Programming</a>, <a rel="tag" href="http://technorati.com/tags/visualstudio">VisualStudio</a></div>
]]></content:encoded>
			<wfw:commentRss>http://stefanoricciardi.com/2009/09/10/visual-studio-tip-no-regions-around-implemented-interfaces/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
