Thread: rounding the number

  1. #1
    Registered User
    Join Date
    Nov 2015
    Posts
    82

    rounding the number

    In Deitel's C book I read this:

    (Rounding Numbers) An application of function floor is rounding a value to the nearest
    integer. The statement
    y = floor( x + .5 );
    will round the number x to the nearest integer and assign the result to y. Write a program that reads
    several numbers and uses the preceding statement to round each of these numbers to the nearest
    integer. For each number processed, print both the original number and the rounded number.
    and I tried to write a program:

    Code:
    #include <stdio.h>
    
    int main(void) {
        float x;
        scanf("%f",&x);
        printf("%d",floor(x + 0.5));
        return 0;
    }
    but this doesn't work (prints 0 either when x is a floating number or an integer).

    What's wrong with my program?

    And please explain me how does this kind of rounding work? I don't understand it.

    Thanks.
    Last edited by lmanukyan; 12-14-2015 at 08:37 AM. Reason: typo

  2. #2
    Guest
    Guest
    printf("%d", ...) expects and integer but you're giving it a floating point type. You need to cast the return value of floor to an integer, e.g.:
    Code:
    printf("%d\n", (int)floor(x + 0.5));

  3. #3
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    The claim
    Quote Originally Posted by lmanukyan View Post
    The statement
    y = floor( x + .5 );
    will round the number x to the nearest integer and assign the result to y.
    is not exactly true: it only does so when x is greater than -0.5.

    For negative values (or values less than +0.5), you need to use y = ceil(x - 0.5) instead. Or, if you have a truncate function (like casting to an integer type in C), you can use it instead of floor() or ceil() here.

    When x is greater than -0.5 but less than +0.5, the two give the same result, 0.



    How and why does this work? Consider the number line. floor() picks the nearest integer on the left, ceil() the nearest integer on the right, and truncation picks the nearest integer towards zero. (If the number is already an integer, then that integer is the closest one; all these functions return that integer itself.)

    Rounding, in this context, means that if we have a positive integer N, then all values greater than or equal to N-0.5 but less than N+0.5 round to N. For a negative integer N, all values greater than N-0.5 but less than or equal to N+0.5 round to N. In other words, half rounds away from zero.

    If you work back from that definition of rounding, and you consider and examine the number line, you'll find that you arrive at the two rules (or one rule, if you consider only positive (or nonnegative) numbers) above to implement the rounding.

    It's not that important, though, because just about all floating-point implementations have all four (truncation, rounding towards nearest integer, ceiling, and floor) as built-in functions. Oh, except in C, where rounding (and explicit truncation, as opposed to integer conversion) was added to the C standard in 99 (C99: round(), lround(), trunc(), nearbyint(), rint(), and lrint()).

  4. #4
    Registered User
    Join Date
    Nov 2015
    Posts
    82
    Thanks a lot for explaining and for telling about the way of rounding the numbers less than -0.5, that was important.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-27-2015, 08:30 AM
  2. Rounding help
    By kalubalu in forum C Programming
    Replies: 4
    Last Post: 01-16-2014, 03:30 PM
  3. Rounding
    By mdoland in forum C# Programming
    Replies: 2
    Last Post: 10-04-2007, 01:18 AM
  4. Help with rounding a number
    By nickk in forum C Programming
    Replies: 3
    Last Post: 06-02-2004, 11:44 AM
  5. rounding
    By Kings in forum C++ Programming
    Replies: 3
    Last Post: 02-11-2003, 03:14 PM