Closed Thread
Results 1 to 5 of 5

Thread: Typecasting and Truncation

  1. #1
    Namesake is offline Newbie
    Join Date
    Feb 2010
    Posts
    21
    Rep Power
    0

    Red face Typecasting and Truncation

    I have been researching on how to truncate a given (double/floating point) number, and it seems the easiest way to do so is through type casting, i.e.:

    Code:
    	double bob = 4.123999;
    	int b =int(bob);
    Although, it seems that a lot of sources on the net use rounding, the use of floors and ceiling to truncate these numbers. Type casting seems to be far easier than all of this.

    Are there any downsides to int typecasting of doubles/floating points ?

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    dcs
    dcs is offline Guru
    Join Date
    Mar 2008
    Posts
    775
    Rep Power
    23

    Re: Typecasting and Truncation

    I suppose it depends on your desired output.
    Code:
    #include <iostream>
    
    void foo(double value)
    {
       int trunc = int(value);
       std::cout << "value = " << value << ", trunc = " << trunc << "\n";
    }
    
    int main(void)
    {
       foo(4.123999);
       foo(4.823999);
       foo(-4.123999);
       foo(-4.823999);
       return 0;
    }
    
    /* my output
    value = 4.124, trunc = 4
    value = 4.824, trunc = 4
    value = -4.124, trunc = -4
    value = -4.824, trunc = -4
    */
    If you felt that one of the results should have been 5 or -5, then truncation is not what you want. Thus the other options.

    Also, you are going to run into issues if the double value is outside of the range of a int.

  4. #3
    Namesake is offline Newbie
    Join Date
    Feb 2010
    Posts
    21
    Rep Power
    0

    Re: Typecasting and Truncation

    This is exactly what I wanted
    Could you expand what you mean by:

    Also, you are going to run into issues if the double value is outside of the range of a int.

    Thanks !

  5. #4
    Join Date
    Jul 2006
    Posts
    16,486
    Blog Entries
    75
    Rep Power
    143

    Re: Typecasting and Truncation

    An int has a maximum value (such as 65,545) that it can store. This is MUCH less than a typical maximum value for a float (such as 9.3e20).
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  6. #5
    dcs
    dcs is offline Guru
    Join Date
    Mar 2008
    Posts
    775
    Rep Power
    23

    Re: Typecasting and Truncation

    Aye.
    Code:
    #include <stdio.h>
    
    int main()
    {
       double d = 1234.56789e+15;
       int i = d;
       printf("d = &#37;g, i = %d\n", d, i);
       return 0;
    }
    
    /* my output
    d = 1.23457e+018, i = -2147483648
    */

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Typecasting lParam to PKBDLLHOOKSTRUCT
    By Buttacup in forum C and C++
    Replies: 2
    Last Post: 12-01-2009, 01:40 PM
  2. Typecasting
    By Khaotic in forum C Tutorials
    Replies: 2
    Last Post: 05-25-2009, 01:40 PM
  3. Typecasting
    By dargueta in forum C and C++
    Replies: 9
    Last Post: 02-25-2008, 07:28 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts