Quick code question (newb)

Discussion in 'iOS Programming' started by ryans79, May 28, 2009.

  1. ryans79 macrumors regular

    Joined:
    Apr 12, 2009
    #1
    Hello!

    going through my book's examples i see this bit of code:

    Works as it should, but i dont understand the last bit of
    I have tested it with:
    NSLog(@"slider value is: %d",(int)slider.value);

    and it seems to be giving me the correct answer everytime, can someone tell me if that "+ 0.5f" is really needed? or do you use the slider the way i used it in my NSLog call?

    °°
     
  2. Guiyon macrumors 6502a

    Joined:
    Mar 19, 2008
    Location:
    North Shore, MA
    #2
    It's doing a cheap round operation. When typecasting a float down to an int it simply cuts off the decimal. By adding 0.5 to the value it will effectively round to the nearest integer value.

    For example:
    (int)(2.4 + 0.5) -> (int)(2.9) -> 2
    (int)(2.6 + 0.5) -> (int)(3.1) -> 3

    Also, the proper way to print the value property is
    Code:
    NSLog(@"slider value is: %f",slider.value);
    since it is a float.
     
  3. ryans79 thread starter macrumors regular

    Joined:
    Apr 12, 2009
    #3
    Thanks!
    but what if its a proper 2.0, the +0.5 will be 2.5, it kind of brings up the problem of rounding to 2 or 3, or what am i missing?
     
  4. Guiyon macrumors 6502a

    Joined:
    Mar 19, 2008
    Location:
    North Shore, MA
    #4
    Nope, no issues there. It simply truncates off everything to the right of the decimal point so 2.0 + 0.5 when typecasted to an int will be 2, which is correct.
     
  5. jsw Moderator emeritus

    jsw

    Joined:
    Mar 16, 2004
    Location:
    Andover, MA
    #5
    No, it just hacks off the stuff after the decimal point, so that 2.0 + 0.5 => 2.5, which is still just "2" after you remove anything past the decimal point. You need a value of 2.5 or higher to "round" to 3.
     
  6. ryans79 thread starter macrumors regular

    Joined:
    Apr 12, 2009
    #6
    Thanks again.

    Since these questions are total newbie ones, i think i'll ask another without starting a separate thread,

    I'm also new to the Mac and one thing is a bit puzzling, on the top left there are three dots, red, yellow/orangey and green... red is to kill the window etc, i know what they all do but sometimes the red dot has a smaller black dot/circle in it... why is that? what does it mean?
     
  7. Guiyon macrumors 6502a

    Joined:
    Mar 19, 2008
    Location:
    North Shore, MA
    #7
    That means there are unsaved changes in the current window.
     
  8. BlackWolf macrumors regular

    Joined:
    Apr 9, 2009
    #8
    I think that appears if the window has unsaved content ...

    edit: toooo sloooow
     
  9. ryans79 thread starter macrumors regular

    Joined:
    Apr 12, 2009
    #9
    Ahh thanks guys!

    @blackwolf, slow but still appreciated :)
    this is all new territory for me, better more answers than less... and the same answer just confirms the first... see? my glass is half full! hehe

    °°
     

Share This Page