Updates from February, 2008 Toggle Comment Threads | Keyboard Shortcuts

  • Ali BaderEddin 8:37 am on February 13, 2008 Permalink | Reply
    Tags: , ceil, floor, , math.h, round   

    C Round Function 

    It’s really weird that the C math library (math.h) doesn’t support the round function. It only includes the floor function which rounds down a float to an integer (can also be done by implicit or explicit casting) and the ceil function which rounds the value up.

    For example,

    int x;
    x = floor(1.2);   //  x is set to 1
    x = floor(1.8);   //  x is set to 1
    x = (int)1.8;     //  x is set to 1 (Explicit Narrowing Conversion)
    x = 1.8;          //  x is set to 1 (Implicit Narrowing Conversion)
    x = ceil(1.2);    //  x is set to 2
    x = ceil(1.8);    //  x is set to 2

    The round function is supposed to round the float value to the nearest integer.

    x = round(1.2);    //  x is set to 1
    x = round(1.8);    //  x is set to 2

    This can be done adding a 0.5 to the value and then truncating it.

    x = (int)(1.2 + 0.5);  //  x is set to 1
    x = (int)(1.8 + 0.5);  //  x is set to 2

    We also have to take negative values into consideration by adding -0.5 and then truncating.

    x = (int)(-1.2 - 0.5);  //  x is set to -1
    x = (int)(-1.8 - 0.5);  //  x is set to -2

    Thus, here is the resulting C function:

    int round(double number)
        return (number >= 0) ? (int)(number + 0.5) : (int)(number - 0.5);

    Note that you might want to use long rather than int to include support for larger numbers and avoid integer overflow.

    That’s it, pretty much primitive, but fun!

    Ali B

    • davidhilton 6:12 pm on February 28, 2008 Permalink | Reply

      Two other options; just use floor(x+.5) or make a macro- #define round(X) floor((X)+.5)

    • alibad 6:38 pm on February 28, 2008 Permalink | Reply

      You are right. floor(x+.5) is just the same as (int)(x+.5). But you forgot to take negative values into consideration.

    • Paul 4:08 am on November 6, 2008 Permalink | Reply

      Interestingly, I even nedumala about it …

    • Cork 4:40 pm on September 5, 2009 Permalink | Reply

      Very useful – thanks. Also, I like davidhilton’s idea of turning your function into a macro.

    • atrujillo 3:26 pm on February 16, 2010 Permalink | Reply

      I like to see the code applied to rounding of number to the nearest decimal place or to adjustable decimal places.

      • alibad 7:04 pm on February 16, 2010 Permalink | Reply

        Someone has already shared this function in the comments to the CodeProject copy of this post. Enjoy :)

        Round(1.234,2) = 1.23
        Round(1.234,0) = 1.0
        Round(123.4,-1) = 120.0

  • Ali BaderEddin 4:56 am on February 8, 2008 Permalink | Reply
    Tags: guid, guidgen, newid, uniqueidentifier, uuid   

    Generating a GUID 

    A GUID is a 128-bit integer (16 bytes) that can be used across all computers and networks wherever a unique identifier is required. Such an identifier has a very low probability of being duplicated. The term is usually referred to as GUID (Globally Unique Identifier) when working with Microsoft technologies. Otherwise, it is referred to as UUID (Universally Unique Identifier).

    Here are multiple ways to generate a GUID:

    Visual Studio

    Run guidgen.exe. There are two ways to run guidgen:

    1. Open Visual Studio IDE, go to Tool menu, and select the Create Guid menu item. This option is available in VS 2008 but not in VS 2010. So use method 2 if you have VS 2010 installed.
    2. Open Visual Studio Command Prompt (from start menu) and enter the following command: guidgen.

    Using any of the methods above, you get the following:


    You can use any of the 4 formats available, but usually the Registry format (currently selected) is the mostly used one. To generate a new GUID, click New GUID. To copy the GUID to the clipboard, click Copy.


    You can generate a Guid using any .Net language. Here is how you can do it with C#.

    Guid myGuid = System.Guid.NewGuid();

    SQL Server

    A GUID is represented as type uniqueidentifier in T-SQL. It is generated as shown below.

    SELECT newid()


    If none of the above methods are available for you, you can get one generated online at http://www.guidgenerator.com/.

  • Ali BaderEddin 9:39 am on February 1, 2008 Permalink | Reply
    Tags: Apache, Eclipse, MySQL, Notepad++, PDT, PHP, PhpMyAdmin, WAMP, Web, XAMPP   

    Starting PHP 

    It’s been some long time since I touched upon PHP, and I wanted to write a small web application using PHP and MySQL. It took me almost half a day just to setup my environment. I first started by individually installing each of PHP, Apache Web Server, MySql and PhpMyAdmin, and then modifying some configuration files to make them work together. This took some time, and I still had some problems. So I gave up on it and decided to use one of those integrated packages (WAMPs) that get everything setup for you, and it was well worth it.

    Follow the below instructions to save yourself some research time and be ready in half an hour.

    Preparing PHP Environment

    Install latest version of XAMPP, which contains:

    • Apache HTTPD Web Server + Openssl
    • MySQL Database Server
    • PHP
    • phpMyAdmin for managing the MySql database Server
    • FileZilla FTP Server
    • Mercury Mail Transport System

    Why XAMPP? Click here for a Comparison of the available WAMPs (Windows, Apache, MySQL, PHP).

    Using XAMPP

    • Run XAMPP Control Panel and start the Apache and MySql services.
    • Open Url http://localhost.
    • The main folder for all WWW documents is at \xampp\htdocs. To test for a PHP page, create new file phpinfo.php with only the following content <?PHP phpinfo(); ?> and place it under \xampp\htdocs. Then go to web browser, and type http://localhost/phpinfo.php in the address bar.

    That’s all you need to know before you put yourself into development mode. Click here if you want to explore other installation methods.

    Editing PHP Files

    If you are already familiar with Eclipse, I suggest you download the Eclipse plugin for PHP in order to use Eclipse as your PHP IDE. Otherwise, Notepad++ is a nice multi-tabbed editor. Don’t worry, both are GREAT and FREE.

    Ali B

Compose new post
Next post/Next comment
Previous post/Previous comment
Show/Hide comments
Go to top
Go to login
Show/Hide help
shift + esc

Get every new post delivered to your Inbox.

%d bloggers like this: