-1

Learning about the difference between Floats and Doubles in Swift. I can't think of any reasons to use Float. I know there are, and I know I am just not experienced enough to understand them.

So my question is why would you use float in Swift?

Joshua Dance
  • 8,847
  • 4
  • 67
  • 72
  • 4
    [Why would you use float over double, or double over long double?](https://stackoverflow.com/q/17603940/995714), [Should I use double or float?](https://stackoverflow.com/q/1074474/995714), [When to use a Float](https://stackoverflow.com/q/407970/995714)... – phuclv Oct 01 '20 at 03:47

3 Answers3

5

why would you use float in Swift

Left to your own devices, you likely never would. But there are situations where you have to. For example, the value of a UISlider is a Float. So when you retrieve that number, you are working with a Float. It’s not up to you.

And so with all the other numerical types. Swift includes a numerical type corresponding to every numerical type that you might possibly encounter as you interface with Cocoa and the outside world.

matt
  • 515,959
  • 87
  • 875
  • 1,141
1

Float is a typealias for Float32. Float32 and Float16 are incredibly useful for GPU programming with Metal. They both will feel as archaic someday on the GPU as they do on the CPU, but that day is years off.

https://developer.apple.com/metal/

0

Double

  1. Represents a 64-bit floating-point number.
  2. Has a precision of at least 15 decimal digits.

Float

  1. Float represents a 32-bit floating-point number.
  2. precision of Float can be as little as 6 decimal digits.

The appropriate floating-point type to use depends on the nature and range of values you need to work with in your code. In situations where either type would be appropriate, Double is preferred.

Debashish Das
  • 859
  • 4
  • 14