Search Unity

trying to create own rectbounds but its returning -7.152557e-09??

Discussion in 'Scripting' started by frekiidoochmanz, May 2, 2016.

  1. frekiidoochmanz

    frekiidoochmanz

    Joined:
    Feb 7, 2016
    Posts:
    158
    I dont get it

    Code (csharp):
    1.  
    2.  
    3.     private void UpdateBoundsPos()
    4.     {
    5.  
    6.         RectLeftX = Self_Transform.position.x + ((Self_SpriteRenderer.sprite.textureRect.xMin / 2) / 100) ;
    7.         RectRightX = Self_Transform.position.x + ((Self_SpriteRenderer.sprite.textureRect.xMax / 2) / 100);
    8.         RectTopY = Self_Transform.position.y + ((Self_SpriteRenderer.sprite.textureRect.yMin / 2) / 100); //i just added this because it is 0 anyway
    9.         RectBottomY = Self_Transform.position.y - ((Self_SpriteRenderer.sprite.textureRect.yMax / 2) / 100);
    10.     }
    11.  
    12.  
    pixel units default settings, it gives me what I want, but suddenly it hits 0 and breaks?

    the math checks out?

    -0.16 + ((32 / 2) / 100) == -0.16 + ((16) / 100) == -0.16 + 0.16 == 0

    I cant even begin to comprehend what im at fault for, and I google it and cant find anything about it. I'm sure its the fact I am getting 0.0000000 when it wants 0 which cant be a decimal? Maybe?
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,537
    that value is:

    -0.000000007152557

    It's ever so nearly 0.

    It's called 'floating point error'.

    Google about it, there's tons of articles on why floating point values are fuzzy and can have slight errors in its arithmetic of fractional values.

    http://lmgtfy.com/?q=floating+point+error
     
  3. frekiidoochmanz

    frekiidoochmanz

    Joined:
    Feb 7, 2016
    Posts:
    158
    Ah, thanks for telling me how to read the way computers show you scientific notation. That'll come in handy.. but I, just thought this would work since it.. seems like it would...

    guess I'll create a if statement to take care of this.
     
  4. frekiidoochmanz

    frekiidoochmanz

    Joined:
    Feb 7, 2016
    Posts:
    158
    In case anyone is wondering I solved this by converting the number to a rounded int value (in simplest units) before solving to find your rect bound and then converting back after solving

    Code (csharp):
    1.  
    2.     private int OneUnitValue(float _posFloat )
    3.     {
    4.        
    5.         float roundFloatpos_;
    6.         roundFloatpos_ = Mathf.Round(_posFloat * 100);
    7.        
    8.         int returnFloatpos;
    9.         returnFloatpos = Mathf.RoundToInt(roundFloatpos_);
    10.        
    11.         return returnFloatpos;
    12.        
    13.     }
    14.  
    15. private void UpdateBoundsPos()
    16.     {
    17.          
    18.         RectLeftX = (OneUnitValue(Self_Transform.position.x) - (Self_SpriteRenderer.sprite.textureRect.xMax / 2)) / 100 ;
    19.         RectRightX = (OneUnitValue(Self_Transform.position.x) + (Self_SpriteRenderer.sprite.textureRect.xMax / 2)) / 100;
    20.         RectTopY = (OneUnitValue(Self_Transform.position.y) + (Self_SpriteRenderer.sprite.textureRect.yMax / 2)) / 100;
    21.         RectBottomY = (OneUnitValue(Self_Transform.position.y) - (Self_SpriteRenderer.sprite.textureRect.yMax / 2)) / 100;
    22.     }
    23.  
    24.  
    probably not the most elegant solution but it works