Archive for July, 2009
To round ‘up’, or to round ‘down’: that is the question
Jul 28th
In troubleshooting a rounding discrepancy a couple of years ago in a software component, I discovered what may be a lesser known fact – the Microsoft programming languages (VBScript, VBA, VB6, VB.NET, C#, etc.) implement what is commonly referred to as “banker’s rounding” or “rounding to the nearest” – which follows the IEEE Standard 754, section 4. My reason for posting about this is that in some cases, this can yield unexpected results.
As we all know, traditional arithmetic rounding (the kind we all learned in grade school) says .5 always rounds up. However, the IEEE Standard 754, section 4 does not implement this form of symmetrical arithmetic rounding.
Here’s the explanation:
Given the range 0 – 1
[.1 .2 .3 .4] – always rounds down
[.6 .7 .8 .9] – always rounds up
However, .5 is the midpoint between 0 and 1 and always rounding .5 in the same direction results in a bias that grows with the more numbers you add together.
So, the IEEE Standard says that one way to minimize the bias is with “banker’s rounding” which is as follows:
Banker’s rounding rounds .5 up sometimes…and down sometimes. The convention is to round to the nearest even number, so that both 1.5 and 2.5 round to 2, and 3.5 and 4.5 both round to 4. Banker’s rounding is symmetric.
Here are a few sample results using the IEEE Standard (banker’s rounding):
Value |
Rounded to 2 Decimal Places |
| 0.1250 | 0.12 (Rounds down when the number to the left of the 5 is even) |
| 0.1350 | 0.14 (Rounds up when the number to the left of the 5 is odd) |
| 0.1251 | 0.13 (Rounds up because the 1 to the right of the 5 since it is not exactly the midpoint between .12 and .13) |
| 0.125000001 | 0.13 (Rounds up for the same reason as the one above) |
|
|
You may already be aware of this seemingly odd behavior, but many of you may be surprised by this result (as I was).
In any case, I wanted to share my findings in case you were expecting a rounding implementation different than that which is actually implemented in the products/programming languages you are using.
Microsoft acknowledges that their rounding implementation is inconsistent across products (VBScript, VBA, VB6, .NET, SQL Server, EXCEL) and says that this is only done for historical reasons. Here is the link to the Microsoft KB article that details the different rounding implementations in their products and how to implement a custom rounding method based on your need: http://support.microsoft.com/?kbid=196652
Here’s the breakdown of the rounding implementations for Microsoft Products:
| Product | Implementation |
| VBScript, VBA, VB 6.0 | Banker’s Rounding |
| Excel Worksheet | Symmetric Arithmetic Rounding |
| SQL Server | Either Symmetric Arithmetic Rounding or Symmetric Round Down (Fix) depending on arguments |
| .NET 1.0, 1.1 | Banker’s Rounding |
| .NET 2.0 (or greater) | Banker’s Rounding but provides additional overloads to the Math.Round() method for specifying desired midpoint rounding behavior. |
So, the good news is that if you are using SQL Server or Excel rounding, you are fine – you’re gonna get good ‘ol traditional arithmetic rounding (.5 always rounds up).
Also of interest, is that the Java Math library (including JavaScript) implements Asymmetric Arithmetic Rounding.
The key difference between this and Symmetric Arithmetic Rounding (the kind we learned in grade school) is as follows:
Symmetric Arithmetic Rounding always rounds up, where “up” is defined as “away from zero.”
So, 0.1250 rounds to 0.13 and -0.1250 rounds to -0.13.
Asymmetric Arithmetic Rounding always rounds up, where “up” is defined as “heading towards positive infinity.”
So, 0.1250 rounds to 0.13 and -0.1250 rounds to -0.12 (i.e. in a positive direction).
As for my case, I wanted Symmetric Arithmetic Rounding and followed the instructions in the KB article above for implementing my own custom Round method to achieve this result.
Here is the final implementation:
/// <summary>
/// Implements alternate rounding methods, in contrast to Decimal.Round()
/// and Math.Round(), which implement banker's rounding (5 rounds to even).
/// See http://support.microsoft.com/?kbid=196652 for other methods.
/// </summary>
public static class MathExt
{
/// <summary>
/// Rounds using arithmetic (5 rounds up) symmetrical (up is away from zero) rounding
/// </summary>
/// <param name="d">d: A System.Decimal number to be rounded.</param>
/// <param name="decimals">The number of significant fractional digits (precision) in the return value.</param>
/// <returns>The number nearest d with precision equal to decimals. If d is halfway between two numbers, then the nearest whole number away from zero is returned.</returns>
public static decimal Round(decimal d, int decimals)
{
decimal factor = Convert.ToDecimal(Math.Pow(10, decimals));
int sign = Math.Sign(d);
return Decimal.Truncate(d * factor + 0.5m * sign) / factor;
}
}
I would encourage you to review any rounding code in your apps and verify that the rounding implementation is the one you expected.
Happy Rounding!
Recreating the MobileMe Gallery in Silverlight 3
Jul 23rd
Background:
Even though I am an avid fan of many Microsoft products (especially their developer tools), Apple has just done certain things “right” for the home user where it seems Microsoft has struggled or been late to the table. As you may have read on my About Me page, I use Macs at home. They have their own set of issues, but for most of the “home stuff” I need a computer for (photos, movies, music) they do the job and they do it well.
One of my favorite tools is iPhoto (Aperture too) and the one-click publishing to MobileMe Gallery – Apple’s web-based photo/movie gallery tool that creates good looking, easy-to-use, web-based galleries that I use to share photos and movies with friends and family. Take a look for yourself and visit the MobileMe sample gallery for the ficticious user “Emily Parker.”
Problem:
So for the most part, the features here meet my needs with one exception – movies require the Apple QuickTime plug-in to view them. Well, you may say, ‘no big deal’ and you’re right…unless you’re one of my many family members who is still on Windows 2000 (or earlier) and can’t install the latest version of QuickTime because it requires Windows XP. So, needless to say, a fair amount of people are unable to view any of the movies that I publish to MobileMe Gallery. So, I end up having to also publish them to YouTube as well (which is a kind of a hassle). Not to mention that they don’t get the same focused user experience of my photos and movies in one nice and neat presentation and not a bunch of other junk detracting from the experience. I know, I know, my family members should upgrade, but the simple fact of the matter is that many of them are happy with their computers and they’re not going to upgrade just so they can view my measly ol’ MobileMe Gallery.
Solution:
So, now for the solution. I attended a Guerilla.NET event back in March (put on by DevelopMentor) and while at the class I got up to speed on Silverlight 2 as well what features were coming in Silverlight 3. Voila! Silverlight 3 was going to support additional media formats including MPEG-4, which is the format used by iTunes and MobileMe Gallery. So, you see where I’m going. My solution would be to use the features of Silverlight 3 to create a solution that would allow my family to view the MPEG-4 format movies stored on MobileMe Gallery using the Silverlight 3 plug-in (once it became available). I knew it would be sometime before it came out of beta and I knew it would take me awhile to develop, so I knew I had some time.
But remember, I didn’t want to sacrifice the clean, simple, focused UX of MobileMe Gallery by simply giving my family a silverlight media player as an alternate location to view movies. Plus, there are already Silverlight media players that would handle the job once Silverlight 3 was available. I wanted my family to have the full, beautiful MobileMe Gallery experience in all its glory. So, I decided I would recreate the MobileMe Gallery using Silverlight. I figured it would be a good learning experiment since I hadn’t played with Silverlight much before the class.
So, this is the introductory post of a multi-part series where I will detail out step-by-step the process of Recreating the MobileMe Gallery in Silverlight 3. I hope you’ll follow along as I go down to meticulous detail to ensure my Silverlight Gallery looks exactly like Apple’s MobileMe Gallery.
Key Features and Success Criteria:
1. Play MPEG-4 (.m4v) files using Silverlight without the need for QuickTime
2. Photo Album and Movie “Scrubbing” (if you view the sample above, you’ll see what I mean when you mouse over an album or movie)
3. Full featured photo album with all the features (and views) of the one built-in to MobileMe Gallery
4. Utilize the photos/movies already stored on MobileMe Gallery (no separate upload or storage required…we’ll do this by tapping into the existing APIs and RSS/JSON feeds that MobileMe uses itself by using a little Fiddler detective work)
5. 100% Silverlight implementation
The goal in one sentence is by the time we finish we should have an exact replica, feature-for-feature match of the MobileMe Gallery using Silverlight 3 (see below for a preview).

As for legal disclaimer, I should note that Apple, the Apple logo, MobileMe, the MobileMe logo, and MobileMe Gallery are registered trademarks of Apple.
Also, I am a multi-Mac owner, multi-iPhone owner, paying member of the iPhone developer program and MobileMe subscriber, so Apple don’t sweat this too much. I’m still a big fan, but needed a good project to develop my Silverlight skills and figured recreating the beautiful MobileMe Gallery would be an excellent challenge.
Here’s the breakdown:
- Part 1: Getting Started – WCF and the initial plumbing required to retrieve the data from the MobileMe Gallery
- Part 2: The Silverlight Project (Coming Soon) – creating the Silverlight project to call our WCF service and start building the home page
- …
Please check back for updates as I’ll continue to update this post with the links to new segments as I complete them.


Recent Comments