The differences between Float, Double, and CGFloat
Swift has three different data types to represent floating point numbers, which are numbers with a decimal points - each with their own characteristics and contexts in which they are used.
The choice between Float, Double, and CGFloat depends on your specific use case, but some of the main differences are:
- Floats are more efficient on memory usage, yet are less precise.
- Doubles consume more memory than floats, but provide a wider range of representable values and greater accuracy.
- CGFloats primary usage is for graphics and UI code. Since Swift 5.5 and onwards CGFloat and Double’s can be used interchangeably.