Basic Understanding: ints/floats

Discussion in 'iOS Programming' started by nashyo, Apr 19, 2012.

  1. nashyo macrumors 6502

    nashyo

    Joined:
    Oct 1, 2010
    Location:
    Bristol
    #1
    Code:
    - (void)scrollViewDidScroll:(UIScrollView *)theScrollView
    {
        CGFloat width = self.scrollView.bounds.size.width;
        int currentPage = (self.scrollView.contentOffset.x + width/2.0f) / width;
        self.pageControl.currentPage = currentPage;
    }
    The x property here 'contentOffset.x' is listed in the documentation as geomagnetic data expressed as microteslas???

    If this is the case, then dividing by a float promotes this unit value to a float also?

    If this is the case, then how is an int returned? are the figures after the decimal point simply dropped?
     
  2. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #2
    The calculation is carried out using the float type. The result is then converted to an int when it's stored in your currentPage variable (or any other use that involves an int type).

    The conversion to an int truncates the fractional part; "round toward zero" is the specific floating-point rounding mode, IIRC.
     
  3. nashyo thread starter macrumors 6502

    nashyo

    Joined:
    Oct 1, 2010
    Location:
    Bristol
    #3
    there to save the day again chown! i owe you
     

Share This Page