Unexpected division result in Swift -
i trying create metronome application in swift, , i'm having awkward results. here code :
@ibaction func playstop(sender: anyobject) { if !timer.valid{ println(double(60/tempo)) timer = nstimer(timeinterval: nstimeinterval(double(60/(tempo))), target: self, selector: "update", userinfo: nil, repeats: true) nsrunloop.currentrunloop().addtimer(timer, formode: nsdefaultrunloopmode) } else{ timer.invalidate() } } func control(_control: nscontrol, textshouldendediting fieldeditor: nstext) -> bool { if (tempolabel.integervalue <= 0) { homecoming false } tempo = tempolabel.integervalue timer.invalidate() println(tempo) playstop(self) homecoming true }
the tempo set user nstextfield
number formatter, , set 60 default. when value of text field changed, console explicitly writes right value tempo
variable, , programme works fine if tempo set 60. when value isn't 60, result double(60/tempo)
equals 0, every time, , update
function gets called if in infinite loop. using double(60/tempo)
, float(60/tempo)
or 60/tempo
doesn't alter anything. don't understand why such result, or how can solve it.
cast numerator or denominator double.
do 60.0/tempo or 60/double (tempo)
swift division
No comments:
Post a Comment