• Home
  • About Me
  • Musings
  • Programming
  • Computers
  • Open Source
  • Society
  • Books
  • Design
  • Movies
  • Archive for February, 2009

    Math.Round

    Math.Round has been improved in C#.
    Consider the below piece of code:

    1
    2
    3
    
    Console.WriteLine(Math.Round(10.4)); // Rounds to 10.0
    Console.WriteLine(Math.Round(10.7)); // Rounds to 11.0
    Console.WriteLine(Math.Round(10.5)); // Rounds to 10.0

    There is nothing surprising about the first two statements.
    In the third statement however, 10.5 is rounded to 10 not 11. C# provides for a way to specify how the middle point has to be treated.
    A enumeration ‘MidpointRounding’ that defines how mid points are treated.

    1
    2
    3
    4
    5
    
    Console.WriteLine(Math.Round(10.5, MidpointRounding.AwayFromZero)); // Rounds to 11.
    Console.WriteLine(Math.Round(10.5, MidpointRounding.ToEven)); // Rounds to 10.
    Console.WriteLine(Math.Round(11.5)); //Rounds to 11.
    Console.WriteLine(Math.Round(11.5, MidpointRounding.AwayFromZero)); // Rounds to 12.
    Console.WriteLine(Math.Round(11.5, MidpointRounding.ToEven)); // Rounds to 12.

    ‘AwayFromZero’ – Rounds the number to the next highest value.
    ‘ToEven’ – Rounds the number to Even number.

    So for a odd fraction, the default round will round it to the lesser number and any of the above overloads will take it to the Next number.

    Implicit Variable in C# 2008

    C# 2008 allows for creating implicit variables using the ‘var’ keyword. But the usage of ‘var’ cannot be truly justified for just declaring a ‘int’ or ‘string’ in code as shown in example below.

    1
    2
    3
    4
    
    var intNum = 5;
     
    Console.WriteLine("intNum is a: {0}", intNum.GetType().Name);
    Console.WriteLine("intNum is defined in: {0}", intNum.GetType().Namespace);

    The output of the above program is

    1
    2
    
    intNum is a: Int32
    intNum is defined in: System

    Here instead of using int in the declaration, we have used ‘var’ keyword. There is no difference between using ‘int’ or ‘var’ in the above example.
    The real usage of ‘var’ comes in LINQ. Consider the following code.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    
    int[] numbers = { 10, 20, 30, 40, 8, 7, 6, 2 };
    var resultSet = from i in numbers where i < 10 select i;
     
    foreach (var i in resultSet)
    {
    Console.Write("{0}", i);
    }
     
    Console.WriteLine("resultSet is a: {0}", resultSet.GetType().Name);
    Console.WriteLine("resultSet is defined in: {0}", resultSet.GetType().Namespace);

    Here resultSet is declared as a ‘var’. From the code, we understand that anytime, the resultSet will be an array of integers. But ‘resultSet.GetType().Name’ gives a surprising result.

    1
    2
    
    resultSet is a: d__0`1
    resultSet is defined in: System.Linq

    So ‘var’ has its best usage in LINQ. So why use it in a normal program when the datatype can be used in itself.

    Barath Sister’s Marriage @ Tanjore

    Last weekend, I and my college mates have been to Tanjore to attend, our friend Barath sister’s marriage. He had been to Australia to pursue his MBA degree and had turned up for the event. So we wanted it to be a get together after a long time. I was not able to board the bus on Friday night. So a group of 12 had travelled leaving me out to Tanjore.

    During the day, Barath and others had been to the Big Temple @ Tanjore, Sivagangai Park and to movies ‘Siva Manasula Sakthi’ and ‘Vennila Kabadi Kulu’.

    Myself, Srikanth and Sakthi took to travel on Saturday. Since it was an auspicious day on Sunday (Muhurtha Naal, when a lot of marriages and ceremonies take place), all buses towards Tanjore and other places were full. We didn’t have our tickets reserved. We waited from 9.30 pm till 11.30 pm without seats. Atlast, when all hopes of getting a decent bus was shattered, we got a AC bus to travel. God Save! Begin the journey.

    We reached Tanjore at 6.30 am on Sunday. The marriage function was from 9.00 am to 10.30pm. ‘Long time no see’ wishes, curses for not coming on friday and welcome chats later, we were in the marriage. Lot of people had come to attend the marriage, most of them Barat’s relatives. After the marriage, the lunch was really a feast. Nearly 25 different items served on ‘Banana Leaf’. Great sumptous meal.

    Then we relaxed a bit in the afternoon chatting with Barat, pulling each others legs. At 4 pm, we had tiffin. That was when we saw a group of small boys (school kids) playing in a ground nearby. Ratish, our captain wanted a bet match. Considering the fate of those small kids we chose to have a friendly match.

    It was great fun, with us getting thrashed by them for runs and our players failing in all parts of the game. It was really like Chennai 28 team getting beaten up by the Beach Boys (Chennai 600028 movie). In the true spirit of the game, actually all our players played using left hand so that the kids could have all the fun.

    Then we took to ‘TN07 AL 4777′ movie and then bucked ourself into SETC bus at 10.30 pm. One more trip to be frozen in memory. Really good time spent with friends after a very long time.

    Avoid flickering in dynamically rendered control in Windows App

    Here is a tip shared by Kannan, a colleague of mine on how to avoid flickering when rendering controls dynamically in a Windows Application.

    Enable double buffering, so that the flickering does not happen.

    Add the following after the ‘InitializeComponent’ method.

    1
    2
    3
    
          SetStyle(ControlStyles.UserPaint, true);
          SetStyle(ControlStyles.AllPaintingInWmPaint, true);
          SetStyle(ControlStyles.DoubleBuffer, true);

    That’s it.

    My PC

    Here’s a list of what is there in my PC, after what Yuvi had posted on his.

    • WAMP (For Apache, MySQL and PHP).
    • WordPress Local Install where I play with PHP.
    • TextPad.
    • VS 2008 Express Edition.
    • Ruby (I rarely use it).
    • SQLYog as a front end MySQL client
    • FireFox & Chrome.
    • VLC  Media Player.
    • Lots of movies for my room-mates viewing pleasure.

    That’s it.

    DOCTYPE blues

    Yesterday I was working on the layout for a new site. I missed the DOCTYPE declaration in the HTML. The page had the main content included inside a div.

    Following is the CSS that I used.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    body {
          font-family: Georgia;
          font-size: 14px;
    }
    #main-content p{
          margin: 0;
          padding: 0;
          padding-bottom: 15px;
    }

    This worked perfectly and rendered well on Firefox and Chrome. But when I opened the same page in IE, the font was too large for all content inside the ‘main-content’ div. Also in order to center the page, I had used ‘margin: auto’ on the page container that encompasses all content in the body as below

    1
    2
    3
    4
    
    #page-container {
          width: 760px;
          margin: auto;
    }

    This too didn’t seem to work in IE as the content was left aligned instead of centered, but rendered ‘centered’ in Firefox and Chrome.
    Looking out for a solution on the net, I found that missing the DOCTYPE declaration was the cause for the issue. Here is the reason.

    Without the DOCTYPE declaration, the browser has rendered the page in quirks mode. Adding the DOCTYPE made the browser render the page following the rules set by the standard.

    So never forget your doctype declarations.

    Dealing with Computer Overheating : Thermal Issues

    Here I am about to share, some gyaan I got from when my computer failed due to thermal issues.

    Computer Overheating symptoms are when your system shutdown abruptly, after running for quite some time. Modern BIOS are so designed to prompt the user with messages on the next boot, to indicate problems relating to over heating. In my case, BIOS POST message clearly as

    ‘Previously the system was shutdown due to a thermal breakdown. Please service your unit’

    The first step to fix this was to go for cleaning the dust that had accumulated over the heat sink and so the other parts of the computer too. Then, service the CPU by cleaning it and then before placing the heat sink  back, apply a litte thermal grease, so that the contact is made properly between the heat sink and the CPU.

    In chennai, we don’t get thermal paste in computer service shops. You need to look into petty shops in Ritchie Street, Mount Road. I bought one from New Version Computers, Meeran Sahib Street. You could get one for Rs. 5/- to Rs. 10/-

    Once done, its time to boot. Viola! the system is up and running and I am happily posting about taking care of the my PC.

    The White Tiger

    I just completed reading ‘The White Tiger’, a novel by Aravind Adiga. It is really fast. Interesting point about the book is the way Adiga had portrayed India, Delhi and Bangalore. There are controversies surrounding the way he had treated Nepalis, Tamilians in his novel. Forgiving those, I found it to be entertaining.

    The story is about a person moving from rags to riches, in a crooked way. It is written as a narrative by a person living in the backyard slums of Delhi, moving to Delhi to become a car driver and then a entrepreneur in Bangalore. He discusses the pitiful state of the drivers, the poor in the towns and cities of India.

    ‘Anything is possible, provided you find a way’, is the gist of the book.