Removing Digital Points (for all c programes)

Discussion in 'Mac Programming' started by Quboid, Feb 20, 2007.

  1. macrumors 6502

    Joined:
    Oct 16, 2006
    Location:
    everywhere
    #1
    Hey guys/girls, just a quick question. Does anyone have an idea as to what function i need to use to remove decimal points from a number in C. I tried the round function but the compiler is not recognising it (i am using mat.h library). Any help would be greatly appreciated.
    THanks

    ps. I am on a windows machine.
     
  2. macrumors member

    SamMiller0

    Joined:
    Aug 17, 2004
    Location:
    San Jose, CA
  3. macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #3
    It's math.h ;).

    One way is to multiply the number up, add 0.5 and convert to an int/long and then multiply back down, though that adds inaccuracies to your calculations.
     
  4. macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #4
    Can we go back to this? Can you post the error message? Your code snippet?
     
  5. macrumors 6502a

    Joined:
    Sep 3, 2005
    Location:
    Cramlington, UK
    #5
    I'm not sure exactly what you want to do, but if all you want to do is to convert, say, a float to the nearest int then roundf() is the right way to do it. Failing that you could do something like this:

    Code:
    
    int int_value = float_value > 0.0f ? float_value + 0.5f : float_value - 0.5f ;
    
    
    b e n
     
  6. macrumors G4

    Joined:
    Jan 5, 2006
    Location:
    Redondo Beach, California
    #6
    The simplest way to round a float to an int is like this


    somefunct()
    {
    double f = 2.3;
    int i;

    i = (int)(f+0.5);
    }

    Adding .5 and then truncating has the effect of rounding but saves having to do a test and branch
     
  7. thread starter macrumors 6502

    Joined:
    Oct 16, 2006
    Location:
    everywhere
    #7
    Thats the simpliest way?
     

Share This Page