Public Group

Rounding numbers problem in c++

This topic is 4495 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hi, I'm making a console RPG game in Dev-C++, and I'm having a problem with the rounding function I made. Actually the function works, but when I output the result of the function and return it, it changes. For example... If I declare... double a = 1.2; // Number to be rounded DOWN int roundNumber(double number); // Function for rounding the number Then the main() function int main(int argc, char *argv[]){ roundNumber(a); cout<<a; // Outputs as 1.2 getch(); // headers are included in actual code in my project return 0; } Here's the actual function code int roundNumber(double number){ double temp1, temp2; temp1 = (int)number; // Typecast to truncate decimals (1.2 becomes 1) temp2 = number - temp1; // Singles out the value after the decimal (so the 1.2 becomes .2) if(temp2 < 0.5){ // Round down temp2 = 0; } else if(temp2 >= 0.5){ // Round up temp2 = 1; } number = temp1 + temp2; cout<<number<<" "; getch(); // For debugging purposes (outputs as 1) return number; // Here's where the number seems to change for no reason; } so the actual output of the program is... 1 1.2 Remember, I outputted the value INSIDE the function first. Someone please help explain why the number changes back to it's original value.

Share on other sites
It's because you are only passing a value (i.e., copy), not a reference.

Absolute simplest (though not necessarily the best) fix:

Declaration:
int roundNumber(double& number); // Function for rounding the number

Definition:
int roundNumber(double& number){/*blah, blah*/}

Share on other sites
Wow, that worked! Thanks Pixel Tamer! So what exactly happened. Did the &number thing passed as the parameter return the value 'number' to the address of 'a'?

Share on other sites
Quote:
 Original post by GuitarPlayer0912Wow, that worked! Thanks Pixel Tamer! So what exactly happened. Did the &number thing passed as the parameter return the value 'number' to the address of 'a'?

Well, there are two issues here. In your original example you aren't capturing the return value. You wrote:

roundNumber(a);

To capture the return value you would write something like:

int b;
b = roundNumber(a);
cout << b;

Pixel Tamer's solution ignores the return value as in your original post, and modifies the passed variable directly (ie. by reference).

Share on other sites
Quote:
 Original post by HollowerPixel Tamer's solution ignores the return value as in your original post,...

Aye, that's true. I just looked at his call and figured that's what he was trying to do. I didn't actually look at his function's implementation. My bad.

Share on other sites
I think you should assert that the passed value is positive, as your method won't work with negative numbers.

a simpler way to do this is

int rounded = (int)(number + 0.5f);

I guess one that works with negative numbers would be something like:

int rounded = (int)(number + (number > 0.f ? 0.5f : (number < 0.f ? -0.5f : 0.f)));

but this doesn't seem too clear, so I'd avise you to use something like:

int roundNumber(const double in_Number)
{
if(in_Number > 0.f)
{
return (int)floor(in_Number + 0.5f);
}
else
{
return (int)ceil(in_Number - 0.5f);
}
}

Hope this helps

Eric

Share on other sites
Thanks guys. So after reading all of this I guess an easier way all along would have been to use ceil() and floor(). They weren't working before maybe because I did ceil(a) or floor(a) instead of

int num;
num = ceil(a) (or floor(a) either one)

but I haven't tried that yet

Setting aside all of that, I did fix my problem. Thanks again.

1. 1
Rutin
41
2. 2
3. 3
4. 4
5. 5

• 9
• 12
• 10
• 13
• 104
• Forum Statistics

• Total Topics
632983
• Total Posts
3009705
• Who's Online (See full list)

There are no registered users currently online

×