Search Unity

Mathf.RoundToInt()

Discussion in 'Scripting' started by koZis, Aug 29, 2017.

  1. koZis

    koZis

    Joined:
    Jun 1, 2017
    Posts:
    8
    Hi,
    I found, that rounding methods (Round and RoundToInt) probably have some issue when rounding threshold .5 value. Every line in example gives different result.

    Debug.Log( Mathf.RoundToInt( 10.5f ) ); // result 10
    Debug.Log( Mathf.RoundToInt( 11.5f ) ); // result 12

    ( Unity 5.6.1f1 )

    Does anyone have some explanation please? Is this property or error?
    Thanks,
    T.
     
    Tonymotion and jakob- like this.
  2. tonemcbride

    tonemcbride

    Joined:
    Sep 7, 2010
    Posts:
    1,089
    On the docs it says: "if the number ends in .5 so it is halfway between two integers, one of which is even and the other odd, the even number is returned." That's why it always jumps to 10,12,14 etc...

    To be honest I find this really odd, I would have completely assumed it would round to the closest - most floating point units will round upwards if .5 or above
     
    azooziiv likes this.
  3. LaireonGames

    LaireonGames

    Joined:
    Nov 16, 2013
    Posts:
    705
    Interesting, does this also apply to just System.Math.Round? I have a feeling it doesn't
     
  4. koZis

    koZis

    Joined:
    Jun 1, 2017
    Posts:
    8
    Thanks your for replies, mentioning of even and odd I overlooked. But this is bit strange behavior.
    System.Math.Round do the same.
     
  5. LaireonGames

    LaireonGames

    Joined:
    Nov 16, 2013
    Posts:
    705
    Lame, it is indeed strange! I guess you could force it to Floor or Ceil whenever your number ends in 0.5 to get a consistent result
     
  6. tonemcbride

    tonemcbride

    Joined:
    Sep 7, 2010
    Posts:
    1,089
    Yeah, I used to just do:

    int RoundedValue = (int)( myFloatValue + 0.5f );

    That would make sure it would round up if .5 or above and round down otherwise
     
    jakob- likes this.
  7. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,350
    Rounding to the nearest even number is the way banks do it; it minimizes the accrued error by rounding. For example:

    1.5 + 2.5 + 3.5 + 4.5

    The actual result without rounding would be 12.
    If you always round up, it becomes 2 + 3 + 4 + 5, or 14.
    If you round to the nearest even, it becomes 2 + 2 + 4 + 4, or 12.

    Rounding to the nearest even gets you closer to the actual value than always rounding up does.
     
    Tonymotion, Novack, Peter77 and 3 others like this.
  8. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    @makeshiftwings is right. This is a feature. (I'm generally no Microsoft fanboy, but the C#/.NET team was very, very careful and got a lot of little details like this right.)
     
  9. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Mathf.Round and RoundToInt merely call System.Math.Round, so that's expected.

    --Eric
     
  10. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    EsmeThunder likes this.