• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by KonstantinMaximus · Dec 16, 2014 at 08:02 PM · rounding

Silly Rounding Question

I searched around, but didnt find any straightforward answers. So, my problem is this:

I have values that come from positions and I am mapping them to a grid position. Values could be anything, but are generally ending in .5, and this is where the problem arises. If I use regular Mathf.Round it gives completely asinine results, if I use Math.Round and specify MidpointRounding.AwayFromZero it doesn't round in the right direction when it comes to negative numbers... like this:

 -3.5 -> default: -4 awayFromZero: -4 (should be -3)
 -2.5 -> default: -2 awayFromZero: -3 (should be -2) 
 -1.5 -> default: -2 awayFromZero: -2 (should be -1)
 -0.5 -> default:  0 awayFromZero: -1 (should be  0)
  0.5 -> default:  0 awayFromZero:  1 (should be  1)
  1.5 -> default:  2 awayFromZero:  2 (should be  2)
  2.5 -> default:  2 awayFromZero:  3 (should be  3)
  3.5 -> default:  4 awayFromZero:  4 (should be  4)

So, I am no mathmatician, but default seems completely broken because it only gives even numbers. How does that make sense? Away From Zero rounds in the wrong direction when number is less than zero, because 0 > -1, -1 > -2 and 0.5 should round to a larger number (but i guess that's what it is supposed to do). Is there a rounding function that actually does what I want?

EDIT: I guess my questions is confusing. I have bunch of data points, which are floats, and I want to find closest ints. If value is x.000000...-x.49999999... i want it to round to x and if value is x.5 to x.9999999 i want to round to x+1. x.5 should always round x + 1. if x is -1, it should round 0. I have no problem with AwayFromZero, that makes sense. It does exactly what I would expect. But default behavior is odd to me, but I did read documentation and I understand what it is doing. Is there a way to round numbers in the manner I described? I imagine I would have to write my own code to do "normal" rounding, but I figured I would ask first.

EDIT2: Imagine a grid with 4 elements position in world space:

 [-0.5, 0.5] [0.5, 0.5]
 [-0.5, -0.5] [0.5, -0.5]

I would imagine that [-0.5, -0.5] would map to [0,0] in grid space and [0.5, 0.5] to [1,1]. That's basically what I have. Objects dont always line up to 0.5, so i have to round.

Thanks!

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image NoseKills · Dec 16, 2014 at 08:26 PM 0
Share

Can you reproduce that with a test case?

With this code

         Debug.Log ($$anonymous$$ath.Round(-3.5f, $$anonymous$$idpointRounding.AwayFromZero));
         Debug.Log ($$anonymous$$ath.Round(-2.5f, $$anonymous$$idpointRounding.AwayFromZero));
         Debug.Log ($$anonymous$$ath.Round(-1.5f, $$anonymous$$idpointRounding.AwayFromZero));
         Debug.Log ($$anonymous$$ath.Round(-0.5f, $$anonymous$$idpointRounding.AwayFromZero));
         Debug.Log ($$anonymous$$ath.Round(0.5f, $$anonymous$$idpointRounding.AwayFromZero));
         Debug.Log ($$anonymous$$ath.Round(1.5f, $$anonymous$$idpointRounding.AwayFromZero));
         Debug.Log ($$anonymous$$ath.Round(2.5f, $$anonymous$$idpointRounding.AwayFromZero));
         Debug.Log ($$anonymous$$ath.Round(3.5f, $$anonymous$$idpointRounding.AwayFromZero));

I do get the expected results -4,-3,-2,-1,1,2,3,4.

How do you know the numbers you are rounding are exactly .5 ? Common mistake is to assume that if Debug.Log(someFloat); prints "0.5", that's the exact value of the float when in fact Debug.Log already rounds the numbers for you to be more readable.

EDIT. Oh u did indeed get the expected results as well... so just your expectations were wrong :)

avatar image KonstantinMaximus · Dec 16, 2014 at 08:45 PM 0
Share

I used this code:

 float[] numbers = { -3.5f, -2.5f, -1.5f, -0.5f, 0.5f, 1.5f, 2.5f, 3.5f };
 
 for ( int i = 0; i < numbers.Length; i++ )
 {
     float number = numbers[ i ];
     Debug.Log ( ": " + number + "Default: " + $$anonymous$$ath.Round ( number ) + "AwayFromZero: " + $$anonymous$$ath.Round ( number, $$anonymous$$idpointRounding.AwayFromZero ) );
 }

I guess my expectation is wrong, but that is my question. How do you round to the nearest largest number if data ends with 0.5.

avatar image Jeff-Kesselman · Dec 16, 2014 at 08:48 PM 0
Share

We gave you two ways to do it below :)

4 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Jeff-Kesselman · Dec 16, 2014 at 08:22 PM

Im not sure what you mean?

The answers are correct, your expectations are apparently wrong..

Away from zero in negative numbers means lower. Away from zero in positive number means higher.

What do YOU want to happen on 0.5? Tell us your goal and we'll tell you how to do it.

edit Addl, it sounds to me like you want 0.5 to always round up. if this is the case, then you can accomplish that this way:

 public float RoundItUp(float f){
     return Mathf.Floor(f+0.5f);
 }

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image NoseKills · Dec 16, 2014 at 08:41 PM 0
Share

$$anonymous$$idpointRounding only has 2 options so if you @$$anonymous$$onstantin$$anonymous$$aximus want to round always to the greater number, The quickest way would just be to make your own rounding function

 using System;
 public class $$anonymous$$y$$anonymous$$ath {
 
     public static int Round(float a)
     {
         if (a < 0) a += 0.5f;
         return (int)$$anonymous$$ath.Round(a, $$anonymous$$idpointRounding.AwayFromZero);
     }
 }


avatar image Jeff-Kesselman · Dec 16, 2014 at 08:44 PM 0
Share

$$anonymous$$y way is simpler :P

avatar image NoseKills · Dec 16, 2014 at 08:47 PM 0
Share

applause =)

avatar image KonstantinMaximus · Dec 16, 2014 at 08:56 PM 0
Share

thanks for your reply, but here is the problem: i have value -0.9444. If add 0.5 to it, it will round to 0, which it should round to -1. So, that won't work. I think I have to check for 0.5 specifically.

avatar image Jeff-Kesselman · Dec 16, 2014 at 08:59 PM 0
Share

your not being clear then as your expected examples above always rounded up, but now you say THIS case needs to round down.

State exactly what your rules for rounding are and we'll tell you how to do it.

Are you saying you always want to round +/-0.5 up but otherwise round to the closest up or down?

Thats a mathematically bizzare (and probably unjustifiable) function but if thats what you want we can ry to figure out how to do it. But you are right, since in that case -.5 acts in a way different from all other numbers it probably has to be special cased.

Show more comments
avatar image
0

Answer by Eric5h5 · Dec 16, 2014 at 08:30 PM

Mathf.Round isn't broken; the way it works is intentional and described in the docs. It's the same for System.Math.Round (which Mathf.Round is just a wrapper for anyway). The default rounding is MidPointRounding.ToEven because it's statistically less biased. AwayFromZero is rounding in the correct direction and also behaves exactly as described in the docs..."away from zero" means exactly that; -4 is farther away from 0 than -3 is.

If you want rounding to behave in a non-standard way, you'd have to write your own function.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by tanoshimi · Dec 16, 2014 at 08:50 PM

Sounds like you want Mathf.Ceil.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jeff-Kesselman · Dec 16, 2014 at 08:51 PM 0
Share

Ah you're right, thats even simpler.

bow

Edit: Oh wait, actually no he doesnt

Ceil will ALWAYS give the higher number.

Eg Ciel(1.1) = 2

What he wants is

F(1.1) = 1

F(1.5) = 2

F(-1.5) = -1

Ergo my answer

avatar image KonstantinMaximus · Dec 16, 2014 at 08:58 PM 0
Share

wouldn't this round 0.4 to 1, which should be 0?

avatar image tanoshimi · Dec 16, 2014 at 09:32 PM 0
Share

Well, this worked for all the test cases in your original question :)

avatar image
0

Answer by KonstantinMaximus · Dec 16, 2014 at 10:58 PM

So, i found a solution that works, but is also a bit slow. I read about rounding here: rounding -0.5

And what i got is this. You can imagine rounding to be like this: take x.y, add 0.5 to it and floor it. That should give you the proper results. So, 1 -> 1.5 -> 1. Or 0.5 -> 1 - > 1. Or 1.2 -> 1.7 -> 1. Or -0.5 -> 0 - > 0. Or -0.4 -> 0.1 -> 0. Or -0.7 -> -0.2 -> -1; Works great.

Now, we are dealing with floats here, so some precision problems arise. For example, 1/10 of the time i would get this issue where Mathf.Floor ( -3.0 ) would be -4, which means it was more like -3.00000000000000000001. So, here is what I did and it works:

 Mathf.Floor ( 1000.0f * ( x + 0.5f ) ) / 1000.0f

It's not ideal. I don't like it, but at least it gives me answers I expect.

I used this rounding to determine grid position of any world object, whose position could be anything. Often than not, position were x.5 type. So, given a Vector3 position, this is how I would find my grid location:

 int x = (int)( Mathf.Floor ( 1000.0f * (position.x / this.cellSize + 0.5f ) ) / 1000.0f ) + this.width / 2 - 1;
 int z = (int)( Mathf.Floor ( 1000.0f * (position.z / this.cellSize + 0.5f ) ) / 1000.0f ) + this.depth / 2 - 1;

Where this.cellSize is the size of each grid cell. This.depth and this.width are the dimensions of the grid. Like I said, I don't like this solution, so let me know if I can improve it somehow.

Thanks everyone!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

how do you round decimals to the nearest integer? 2 Answers

Debug.Log() question 1 Answer

Rounding a position against the surface of an object. 1 Answer

rounding up numbers - rotation 0 Answers

Rounding transform.position? With parents 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges