View Full Version : Quick code question (newb)
ryans79
May 28, 2009, 09:30 PM
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 + 0.5f
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?
°°
Guiyon
May 28, 2009, 09:41 PM
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 NSLog(@"slider value is: %f",slider.value); since it is a float.
ryans79
May 28, 2009, 09:47 PM
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 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?
Guiyon
May 28, 2009, 09:48 PM
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.
jsw
May 28, 2009, 09:49 PM
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.
ryans79
May 28, 2009, 09:52 PM
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?
Guiyon
May 28, 2009, 10:04 PM
...but sometimes the red dot has a smaller black dot/circle in it... why is that? what does it mean?
That means there are unsaved changes in the current window.
BlackWolf
May 28, 2009, 10:04 PM
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
ryans79
May 28, 2009, 10:08 PM
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
°°
vBulletin® v3.8.6, Copyright ©2000-2012, Jelsoft Enterprises Ltd.