Thread: what to use double or int ?

  1. #1
    Registered User sagar474's Avatar
    Join Date
    Jun 2011
    Location
    Kakinada
    Posts
    56

    what to use double or int ?

    to plot graphics the pixel data should be in ints

    if we use double for calculating. then we should perform casting. casting is very expansive.

    so can we use int ? what to use double or int.

  2. #2
    spaghetticode
    Guest
    I'm not clear about the problem you are having there.

  3. #3
    Registered User sagar474's Avatar
    Join Date
    Jun 2011
    Location
    Kakinada
    Posts
    56
    Code:
    double positin=0;
    double velocity=0;
    double accleartin.=0;
    
    //do some thing
    
    draw_object_at((int)positin);//argument should be int
    Code:
    
    
    int positin=0;
    int velocity=0;
    int accleartin.=0;
    
    //do some thing
    
    draw_object_at(positin);//no need of casting
    which is better first or second

  4. #4
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    Casting probably isn't taking up a significant amount of time compared to the time it takes to actually put the object on-screen.
    If you can represent your acceleration, velocity, and position as integers without losing precision, go ahead; I for one appreciate the precision of floating point numbers when it comes to physics-y motion like this.
    Consider this post signed

  5. #5
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    I would use floats and then cast to int when needed. Integers are not good when it comes to graphics b/c there is no allowance for fractional values and graphics code is full of fractional values in matrices, vectors, etc. Why do you need integers for rendering?

  6. #6
    Registered User inequity's Avatar
    Join Date
    Nov 2010
    Location
    Seattle, Washington
    Posts
    59
    If you need to draw pixel by pixel using only integers, I would suggest just rounding in whatever function you have actually setting those pixels. It will help keep your code clean and readable. The rest of your calculations should probably be done in floats because it suits most of the graphics code you'll have to write.

    However, if you're using almost any graphics API you shouldn't have to worry about this.

    Also, in relation to the speed of most computers around today, casting to a float isn't a very expensive operation.

    If you for some reason need to use ints (and only ints), you can do most 2d graphics with an extension of the Bresenham line algorithm called the Midpoint Line Algorithm. There's some info on that algorithm here. This kind of stuff is pretty heavy on math though, so if you want to avoid that, use a graphics API.

  7. #7
    Registered User sagar474's Avatar
    Join Date
    Jun 2011
    Location
    Kakinada
    Posts
    56
    However, if you're using almost any graphics API you shouldn't have to worry about this.
    I'm using Allegro.

  8. #8
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    Quote Originally Posted by sagar474
    I'm using Allegro.
    FWIW Allegro 5's graphics routines take floats as arguments, and version 5 is a *huge* improvement over previous versions anyway.
    Consider this post signed

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Double liked list and double pointer
    By saillesh.sabari in forum C Programming
    Replies: 1
    Last Post: 12-10-2010, 11:03 AM
  2. Replies: 4
    Last Post: 10-31-2009, 07:18 PM
  3. Replace double with long double in all my code.
    By boyfarrell in forum C Programming
    Replies: 8
    Last Post: 04-30-2007, 04:17 PM
  4. Changing double time to double cost
    By chrismax2 in forum C++ Programming
    Replies: 2
    Last Post: 04-24-2004, 10:29 AM
  5. the diffrence between static double and double
    By sayeem81 in forum C Programming
    Replies: 6
    Last Post: 02-18-2002, 12:12 PM