Friday, 15 January 2010

xcode - Range of doubles in Swift -



xcode - Range of doubles in Swift -

i writing swift application , parts of require making sure user inputs add together specified value.

a simplified example:

through programme interaction, user has specified totalvalue = 67 , turns = 2. means in 2 inputs, user have provide 2 values add together 67.

so lets on turn 1 user enters 32, , on turn 2 enters 35, valid because 32 + 35 = 67.

this works fine, moment verge more 1 decimal place, programme cannot add together numbers correctly. example, if totalvalue = 67 , on turn 1 user enters 66.95 , on turn 2 enters .05 programme homecoming error despite fact 66.95 + .05 = 67. problem not happen 1 decimal place or less (something turn 1 = 55.5 , turn 2 = 11.5 works fine), 2 decimal spots , beyond. storing values doubles. in advance

some illustration code:

var totalweights = 67 var input = double(mytextfield.text.bridgetoobjectivec().doublevalue) /*each turn button click*/ /*for turn 1*/ if inputvalid == true && turn == 1 && input < totalweights { myarray[0] = input } else { //show error string } /*for turn 2*/ if inputvalid == true && turn == 2 && input == (totalweights - myarray[0]) { myarray[1] = input } else { //show error string }

if want exact values floating point float/double types not work, ever approximations of exact numbers. using nsdecimalnumber class within swift, i'm not sure bridging should simple.

xcode double swift addition

No comments:

Post a Comment