Rounding up to the nearest integer

Sep 19, 2020 at 3:32am
Hello everyone, back with the simple stuff. I got some an issue with rounding up. Could you please explain how this works? Thank you for your time coders. Here is what I need to do.

Write a program that prompts the user to input the decimal value of a double and outputs the number rounded to the nearest integer.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//Programming Exercise 2-7
#include <iostream>
using namespace std;

int main()
{
    double num1 = 5;
    double num2 = 0.1;
    double num3 = 1.5;
    double num4 = 30980234.5001928;

    cout << num1 << endl;
    cout << num2 << endl;
    cout << num3 << endl;
    cout << num4 << endl;

    return 0;
}
Sep 19, 2020 at 3:46am
an oversimplified approach is to just do it the way you would do it in math without any shortcuts.

x rounded can be written:
int rounded = x/(abs(x)) * (abs(x)+0.5);

there are better ways to do it:
http://www.cplusplus.com/reference/cmath/round/
Last edited on Sep 19, 2020 at 3:48am
Sep 21, 2020 at 12:49am
Sep 21, 2020 at 1:05pm
@OP,

@jonning and @markyrocks gave you answers to 2 different questions. Your original post is ambiguous enough that it contains both questions.

The title says "Rounding up to the nearest integer". This implies the ceil function. In other words, 3.1 would round to 4 (it is rounded up to the nearest integer). This is the answer that @markyrocks provided.

your second paragraph simply says "outputs the number rounded to the nearest integer." In other words: 3.1 -> 3, 3.5 -> 4, 3.9 -> 4. This is what is normally called "rounding". This is the answer that @jonnin provides.

I would argue that @jonnin's answer is not correct for negative numbers. If we are to round "up" at x.5, then round(-3.5) should be -3. However, @jonnin's solution yields -4. I submit that correct answer would be the simpler int rounded = int(x + .5);.
Sep 21, 2020 at 2:22pm
my equation is the same as round(). From the sample code on our site:
(see that -5.5 -> -6)

Output:

value round floor ceil trunc
----- ----- ----- ---- -----
2.3 2.0 2.0 3.0 2.0
3.8 4.0 3.0 4.0 3.0
5.5 6.0 5.0 6.0 5.0
-2.3 -2.0 -3.0 -2.0 -2.0
-3.8 -4.0 -4.0 -3.0 -3.0
-5.5 -6.0 -6.0 -5.0 -5.0

if you want a different result, of course, you can do it a different way. Yours matches ceil().

Last edited on Sep 21, 2020 at 2:24pm
Sep 21, 2020 at 3:05pm
round() doesn't state 'round up'. It states that it will round to 'the nearest integer'. so -2.3 rounds to -2 and -3.5 rounds to -4
Sep 21, 2020 at 3:08pm
Yours matches ceil().


That's not true. The ceil() function rounds ALL decimal places up to the next integer.

We differ in how x.5 is rounded for negative numbers. The question says "round up". I am just pointing out that your algorithm (which may or may not match round(), and I have no reason to doubt your claim that it does) rounds DOWN for negative x.5 numbers.

So, if x.5 is to be rounded away from 0, then your code is absolutely correct. If x.5 is to be rounded up (for both positive and negative values), it is not.

Of course, it depends on what "up" means for negative numbers. I assume "up" means in the positive direction. If it means "greater magnitude" or "greater absolute value", then you are correct.
Sep 21, 2020 at 3:27pm
The question says:

Write a program that prompts the user to input the decimal value of a double and outputs the number rounded to the nearest integer.


This doesn't mention round-up/down. The OP in the first post is assuming that round means round-up when it doesn't.

Sep 21, 2020 at 4:10pm
@seeplus

The title says "up".
Sep 21, 2020 at 4:23pm
But this isn't the exercise requirement as stipulated.


Write a program that prompts the user to input the decimal value of a double and outputs the number rounded to the nearest integer.

Sep 21, 2020 at 4:33pm
i was just giving out more options. What the op wrote was contradictory so i just gave him more information. I know i find myself using floor() and ceil() mainly bc normally when i need to round the direction is important. I'd think just converting from a float to an int would just automatically round to the nearest int. I'm actually going to check it out bc i have no idea. I'd have to assume not if this is even necessary.

edit it looks like it rounds down for a positive, rounds up for a negative. thats kinda weird.
Last edited on Sep 21, 2020 at 4:41pm
Sep 21, 2020 at 4:37pm
I'd think just converting from a float to an int would just automatically round to the nearest int.


If you're talking about implicitly/explicitly casting a floating point number to an integer, then there is no rounding involved, any fractional part is just discarded.

Sep 21, 2020 at 9:03pm
I'd think just converting from a float to an int would just automatically round to the nearest int.

C, inherited into C++, originally was built on the least amount of work / performance first design. It costs to do this for every single float to int, comparisons and whatnot hidden from the coder, slowing things down. Not so big a deal on our terraflop laptops in 2020, but it mattered at 10hz in 1980.
Topic archived. No new replies allowed.