Jump to content
  • Advertisement
Sign in to follow this  
FreJa

weird Math.round

This topic is 4578 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

Hey, I just needed to use the Math.round(..) func from Java. But I get wrong results. If I do: Math.round(0.5), I get 1 ... all good so far. But with Math.round(-0.5) I get 0.0 ... why?!! shouldnt it be -1 ? thanks

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Posted by Instruo (next post)
its probably roudning .0 to .5 (non-inclusive) down and .5 (inclusive) to 1.0 (non inclusive) up. in which case, -0.5 would round to 0.0


If you want to round like that you could use some function like this pseudo code (sorry, I don't know Java and I'm not sure how close this is to a Java function):


int myrounding(float number)
{
if(number >= 0)
return Math.round(number);

number *= -1;
int roundedNumber = Math.round(number);
return (roundedNumber *= -1);
}






Something like that will make your negative numbers positive, round them, and make them negative again. This way when you put -0.5 in, it'll take it, turn it into 0.5, round up to 1 and then return -1.

Share this post


Link to post
Share on other sites
I wouldn't think so. its probably roudning .0 to .5 (non-inclusive) down and .5 (inclusive) to 1.0 (non inclusive) up. in which case, -0.5 would round to 0.0

Share this post


Link to post
Share on other sites
Quote:
Original post by Instruo
I wouldn't think so. its probably roudning .0 to .5 (non-inclusive) down and .5 (inclusive) to 1.0 (non inclusive) up. in which case, -0.5 would round to 0.0


That's probably more true than my post. I'll go edit that.

Either way the proposed function in my post should still get it to do what you want it to do.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
-0.5 would be rounded UP to the nearest whole number. -1 would mean rounding DOWN since -1 <

Share this post


Link to post
Share on other sites
Let:
- f be you float
- N the greatest integer such as N <= f
- M the smallest integer such as f <= M
(so: N <= f <= M)

then round(f) = N if and only if (f-N) < (M-f) (strictly)
otherwise round(f) = M

Think about your case:

f = -0.5
N = -1, M = 0

f - N = 0.5
M - f = 0.5

is (f-N) < (M-f) ? No. Then round(-0.5) = M = 0

I hope this mathematical view clears things up.

Share this post


Link to post
Share on other sites
Quote:
Original post by FreJa
Hey,

I just needed to use the Math.round(..) func from Java. But I get wrong results.
If I do: Math.round(0.5), I get 1 ... all good so far. But with Math.round(-0.5) I get 0.0 ... why?!! shouldnt it be -1 ?

No, it shouldn't. From the documentation:

Quote:
Java docs for Math.round(float)

Returns the closest int to the argument. The result is rounded to an integer by adding 1/2, taking the floor of the result, and casting the result to type int.

Adding 1/2 to -0.5 produces 0; taking the floor of 0 yields 0; casting 0 to an int yields 0. Thus the answer is 0. If you'd used -0.6, you would have gotten -1, since adding 1/2 to -0.6 produces -0.1; taking the floor of this yields -1; then casting to an int yields -1, so the the result is -1.

To get the behavior you want, use the absolute value of the argument in round() instead. Then multiply the result by arg/|arg| (-1 for negative numbers, +1 for positive numbers). For instance, suppose the argument is arg = -0.5, as in your example. Then take |-0.5| = 0.5; round as usual, giving 1. Now multiply by arg/|arg| == -0.5/0.5 == -1, giving 1 * -1, which is -1, the desired value.

hope that helps,

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!