1

When trying to convert a String or NSString to float or double I get inaccurate answers in debugger. Why is this ?

let x = "2.3"
let y = Double(x)
let z = Float(x)

let xx : NSString = "2.3"
let yy = xx.floatValue
let zz = xx.doubleValue

I have run this on XCode 7.3 and XCode 8 gm seed

enter image description here

Bista
  • 7,869
  • 3
  • 27
  • 55
Ryan Heitner
  • 13,119
  • 6
  • 77
  • 119
  • I accept that this may be for the same reason as "Is floating point math broken? ". However I think that the fact that since it is a string conversion to double/float it would not be obvious to most people that the cause is the same. I think that it would be of value to let the question stand and to answer it with a reference to the duplicate. – Ryan Heitner Sep 13 '16 at 15:26
  • 2
    Your question is related to very broad and well known theme, floating point representation and calculations in discrete (computer) systems. Since this there is no sense to discuss topic in terms of concrete ios platform. Google it, and you will find many information. For example: https://en.wikipedia.org/wiki/IEEE_floating_point – Yury Sep 13 '16 at 15:38
  • Shadow of, You are correct I will still mark an answer correct if someone feels like answering it below. – Ryan Heitner Sep 13 '16 at 16:06

0 Answers0