Results 1 to 10 of 10

Thread: How do I check the value of a variable for rounding?

  1. #1

    Thread Starter
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772
    Right now, I have something like:

    --
    int x, y, z = 0

    cin>>x;
    x = ((y * 3 + z) / 4);
    --

    I am getting a decimal that I want rounded to a whole number. My question is, how can i see what the value is to know if i round up or down?

  2. #2
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    You could just add 0.5 and round down.
    Harry.

    "From one thing, know ten thousand things."

  3. #3
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Code:
    long round(float value) {
    	// Round to nearest integer
    	if((long)value == (long)(value + 0.5f))
    		return (long)value;
    	else
    		return (long)(value+1);
    }
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  4. #4

    Thread Starter
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772
    Harry, how would i go about rounding down? Right now I have

    x=x+0.5;
    floor(x);

    but it isn't working. Can you help at all?

  5. #5
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    To round down, simply cast to an integer type - rounding down is the same as truncating the fractional portion:
    Code:
    float fNum = 4.6;
    int iNum = (int)fNum; // =4
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  6. #6

    Thread Starter
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772
    That should go to 5 though, not 4.

  7. #7
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Rounding 4.6 down gives 4.
    Rounding 4.6 up gives 5.
    Rounding 4.6 to the nearest integer gives 5.

    Rounding 4.3 down gives 4.
    Rounding 4.3 up gives 5.
    Rounding 4.3 to the nearest integer gives 4.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  8. #8
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    Looks like you're confusing each other Wynd, use something like this:

    Code:
    long RoundToNearest(float fNum)
    {  return( (long)(fNum + 0.5f) );
    }
    Then use it like:
    Code:
    lRoundedNum = RoundToNearest(fNum)
    Harry.

    "From one thing, know ten thousand things."

  9. #9

    Thread Starter
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772
    Yeah, the "round to nearest integer" was what I was trying to do, but I couldn't figure out how to say it. Thanks!

  10. #10
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    If you really need the speed you can do it the nasty macro way:
    Code:
    #define RoundToNearest(x) ((long)(x + 0.5f))
    That's fairly safe, though, since x is only used once.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

Posting Permissions

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



Click Here to Expand Forum to Full Width