Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

ryans79

macrumors regular
Original poster
Apr 12, 2009
226
0
Hello!

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

-(IBAction)sliderChanged: (id)sender
{
UISlider *slider = (UISlider *)sender;
int progressAsInt = (int)(slider.value + 0.5f);

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?

°°
 
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.
 
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.

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?
 
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?

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.
 
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?
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.
 
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?
 
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?

I think that appears if the window has unsaved content ...

edit: toooo sloooow
 
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

°°
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.