Swift randomFloat issue: '4294967295' is not exactly representable as 'Float'
As you know Float
has only 24-bits of significand, so 32-bit value 0xFFFFFFFF would be truncated. So, Swift is warning to you that Float
cannot represent the value 0xFFFFFFFF
precisely.
The short fix would be something like this:
let randomValue: CGFloat = CGFloat(Float(arc4random()) / Float(0xFFFFFFFF))
With using Float.init
explicitly, Swift would not generate such warnings.
But the preferred way would be using random(in:)
method as suggested in matt's answer:
return CGFloat(Float.random(in: from...to))
or simply:
return CGFloat.random(in: from...to)
Simplest solution: abandon your code and just call https://developer.apple.com/documentation/coregraphics/cgfloat/2994408-random.