Jump to content
  • Advertisement
Sign in to follow this  
GuitarPlayer0912

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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*/}

(Note the added ampersand.)

Share this post


Link to post
Share on other sites
Quote:
Original post by GuitarPlayer0912
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'?


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 this post


Link to post
Share on other sites
Quote:
Original post by Hollower
Pixel 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 this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!