# Why a double is preferred over float?

**URL:** <https://forums.swift.org/t/why-a-double-is-preferred-over-float/42187>\
**Category:** Using Swift\
**Created:** [November 21, 2020, 12:52am UTC](https://forums.swift.org/t/why-a-double-is-preferred-over-float/42187 "2020-11-21T00:52:07Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![jmgawecki](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jmgawecki/32/33325_2.png) [@jmgawecki](https://forums.swift.org/u/jmgawecki)\
**Post date:** [November 21, 2020, 12:52am UTC](https://forums.swift.org/t/why-a-double-is-preferred-over-float/42187/1 "2020-11-21T00:52:07Z")

</div>

Hi all, question same as the topic of that post. Many thanks for help

 ![Screenshot 2020-11-21 at 00.51.12](https://global.discourse-cdn.com/swift/original/2X/6/69e9bf36f1517de78db5118a2e2aee7f3da3c7b5.png)

---

<div class="post-metadata">

**Author:** ![navdeepmahajan](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/navdeepmahajan/32/13772_2.png) [@navdeepmahajan](https://forums.swift.org/u/navdeepmahajan)\
**Post date:** [November 21, 2020, 2:24am UTC](https://forums.swift.org/t/why-a-double-is-preferred-over-float/42187/2 "2020-11-21T02:24:07Z")

</div>

I see that you have posted the answer in your question - it is because of higher precision of Double. Swift is doing type inference and if given a choice it will choose something that is more precise. Of course you can change to type annotation and use Float instead of Double. Same is the case with single characters in double quotes, they are defaulted to Strings rather than characters.. More details here

[![](https://global.discourse-cdn.com/swift/original/3X/4/4/44d8be2fa8c04a9ebdb1c5eb7dc0e1c8483e1eea.jpeg "Swift Define Constants And Variables") ](https://www.youtube.com/watch?v=oHHnkmWw0wo)

---

<div class="post-metadata">

**Author:** ![jmgawecki](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jmgawecki/32/33325_2.png) [@jmgawecki](https://forums.swift.org/u/jmgawecki)\
**Post date:** [November 21, 2020, 1:03pm UTC](https://forums.swift.org/t/why-a-double-is-preferred-over-float/42187/3 "2020-11-21T13:03:26Z")

</div>

Hey, thanks for the answer. But does Double not require more memory than a Float type? To me it would be logical to prefer the type that requires less memory. 🙂 thanks

---

<div class="post-metadata">

**Author:** ![navdeepmahajan](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/navdeepmahajan/32/13772_2.png) [@navdeepmahajan](https://forums.swift.org/u/navdeepmahajan)\
**Post date:** [November 21, 2020, 2:28pm UTC](https://forums.swift.org/t/why-a-double-is-preferred-over-float/42187/4 "2020-11-21T14:28:02Z")

</div>

You are right, Double does requires extra memory. But then swift had to choose between precision vs memory and the default choice was made towards precision. Eg: Int16 exists but is not defaulted for integer types - this is not from precision perspective for integers, just that there is a trade off. In case of Double it is between precision and memory and precision was chosen.

---

<div class="post-metadata">

**Author:** ![jmgawecki](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jmgawecki/32/33325_2.png) [@jmgawecki](https://forums.swift.org/u/jmgawecki)\
**Post date:** [November 21, 2020, 5:29pm UTC](https://forums.swift.org/t/why-a-double-is-preferred-over-float/42187/5 "2020-11-21T17:29:22Z")

</div>

thanks for clarification

---

<div class="post-metadata">

**Author:** ![Lantua](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lantua/32/8868_2.png) [@Lantua](https://forums.swift.org/u/Lantua)\
**Post date:** [November 21, 2020, 6:07pm UTC](https://forums.swift.org/t/why-a-double-is-preferred-over-float/42187/6 "2020-11-21T18:07:05Z")

</div>

Note that they say (emphasis added):

> In situations where **either type would be appropriate** , `Double` is preferred

You should have already determined that the memory footprint and precision of both types are acceptable at that point (which would be 99% of the cases).

Swift just likes to be opinionated and nudge you toward a common choice. There are merits if most of the program uses the same type. You don't need to bother with data casting for one.

PS  
Note also that the _preferred_ choice for integer is `Int` and not `Int64`.

---

<div class="post-metadata">

**Author:** ![jmgawecki](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jmgawecki/32/33325_2.png) [@jmgawecki](https://forums.swift.org/u/jmgawecki)\
**Post date:** [November 21, 2020, 6:12pm UTC](https://forums.swift.org/t/why-a-double-is-preferred-over-float/42187/7 "2020-11-21T18:12:07Z")

</div>

forgive me for asking, but what would be the difference between Int and Int64? I assumed that would be the same.

---

<div class="post-metadata">

**Author:** ![Lantua](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lantua/32/8868_2.png) [@Lantua](https://forums.swift.org/u/Lantua)\
**Post date:** [November 21, 2020, 6:14pm UTC](https://forums.swift.org/t/why-a-double-is-preferred-over-float/42187/8 "2020-11-21T18:14:52Z")

</div>

`Int` is 32bit on 32bit machine, and is 64bit on 64bit machine. It is again usually the _right choice_ if your data range fits. It is the _native_ int and is generally faster _if_ there's any noticeable difference in speed between types.

---

<div class="post-metadata">

**Author:** ![anon9791410](https://avatars.discourse-cdn.com/v4/letter/a/9fc348/32.png) [@anon9791410](https://forums.swift.org/u/anon9791410)\
**Post date:** [November 21, 2020, 11:48pm UTC](https://forums.swift.org/t/why-a-double-is-preferred-over-float/42187/9 "2020-11-21T23:48:47Z")

</div>

The better question is, why do we _specifically_ use `Float64` instead of _any_ other choice.

> <https://stackoverflow.com/questions/417568/float-vs-double-performance/417591#417591>

And why, in Swift, when `Int` is `Int64`, is `Float` not `Float64`. 😔

---

<div class="post-metadata">

**Author:** ![jonprescott](https://avatars.discourse-cdn.com/v4/letter/j/e36b37/32.png) [@jonprescott](https://forums.swift.org/u/jonprescott)\
**Post date:** [November 22, 2020, 4:35am UTC](https://forums.swift.org/t/why-a-double-is-preferred-over-float/42187/10 "2020-11-22T04:35:21Z")

</div>

Float64 is a typealias to Double, at least according to the Apple documentation. There is also a Float32 that is typealiased to Float. Float and Double are defined by their IEEE-754 definitions implemented in the hardware.

---

<div class="post-metadata">

**Author:** ![QuinceyMorris](https://avatars.discourse-cdn.com/v4/letter/q/d26b3c/32.png) [@QuinceyMorris](https://forums.swift.org/u/QuinceyMorris)\
**Post date:** [November 22, 2020, 5:24am UTC](https://forums.swift.org/t/why-a-double-is-preferred-over-float/42187/11 "2020-11-22T05:24:03Z")

</div>

Actually, the answer is probably: "Because C". 🙂

Specifically, in C, the natural floating point type is `double` and for some intermediate period in history there were CPUs that had `double` registers but no `float` registers. In other words, `float` was a storage type, not a calculation type — hence the weird C rules that promote `float` to `double` in some contexts.

With a blank slate, it would have made sense for Swift's IEEE 64-bit floating point type to be called `Float` and the 32-bit version `Float32`, analogously to `Int` vs. `Int32`. I guess that was thought to be too confusing for developers coming from C or Obj-C.

Still, there's some reason to prefer that (Swift) `Float` and `Double` should be freestanding basic types, rather than related to each other. 32-bit floats are too limited in precision to be a good _general purpose_ calculation type. They are generally better suited to being a compact storage type.

I'd say that's the real answer to the OP's question. Unless you are prepared to very carefully manage error accumulation due to loss of precision, `Double` is a better choice because you don't usually have to sweat the details over _every_ floating point expression.

Of course, that's all too sweeping to be really true — `Float` has its uses as a calculation type too — but I think that's the flavor of why the floating point types don't follow the integer naming pattern, and why `Double` is preferred.

---

<div class="post-metadata">

**Author:** ![wilshipley](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/wilshipley/32/10165_2.png) [@wilshipley](https://forums.swift.org/u/wilshipley)\
**Post date:** [November 23, 2020, 9:59am UTC](https://forums.swift.org/t/why-a-double-is-preferred-over-float/42187/12 "2020-11-23T09:59:20Z")

</div>

In general `Double` is preferred because it’s SO MUCH more precise than `Float`, and on 64-bit platforms (eg, everything but watchOS) it’s usually as fast (sometimes faster) to work with.

So unless you are an architecture that’s EXTREMELY memory-limited, you should default to using `Double` so you don’t have to keep swatting bugs that come from having low precision.

Specifically graphics code is a lot easier to deal with with `Double` — I was originally using `Float` for my 3D code but found that chains of matrix operations would end up pretty far off with `Float` matrices.

So, yes, if you’re allocating a gigantic buffer of a million floating-point numbers, you might stop and think if you want a `Float16` or `Float` or `Double`. But if you’re just adding up some non-integer numbers in a loop, use a `Double`!

-Wil

---

<div class="post-metadata">

**Author:** ![scanon](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/scanon/32/2445_2.png) [@scanon](https://forums.swift.org/u/scanon)\
**Post date:** [November 23, 2020, 5:29pm UTC](https://forums.swift.org/t/why-a-double-is-preferred-over-float/42187/13 "2020-11-23T17:29:24Z")

</div>

> [@wilshipley](#):
>
> In general `Double` is preferred because it’s SO MUCH more precise than `Float` , and on 64-bit platforms (eg, everything but watchOS) it’s usually as fast (sometimes faster) to work with.

(Going into the weeds a bit) Double-precision arithmetic is basically just as fast\* as single-precision; this is true even on most 32b platforms, including all iOS and watchOS hardware that is still supported (and all but the earliest unsupported ones too). There are some _extremely_ limited CPUs where this is not the case, but Swift doesn't target them.

The only real exception to this is that you can fit more Floats in a given amount of space than Double, which applies to both memory and SIMD vectors, so you will get a speedup from reduced cache traffic and vectorization when it's possible--simple scenarios like summing two arrays can easily be twice as fast using Float for this reason, while more complex algorithms may not have any performance change.

[\*] The semi-exceptions to this are divide and square root, which have somewhat longer latency for double than float on many CPUs, but should be rare in most programs.

---

<div class="post-metadata">

**Author:** ![anon9791410](https://avatars.discourse-cdn.com/v4/letter/a/9fc348/32.png) [@anon9791410](https://forums.swift.org/u/anon9791410)\
**Post date:** [November 24, 2020, 12:59am UTC](https://forums.swift.org/t/why-a-double-is-preferred-over-float/42187/14 "2020-11-24T00:59:51Z")

</div>

> [@scanon](#):
>
> There are some _extremely_ limited CPUs where this is not the case, but Swift doesn't target them.

But whether Swift "targets" the GPU next to it is true or false depending on your point of view.

[Reducing Shader Bottlenecks](https://developer.apple.com/documentation/metal/optimizing_performance_with_the_gpu_counters_instrument/reducing_shader_bottlenecks):

> [@](#):
>
> When a half-float has the necessary range and precision for your calculations, use half-floats instead of floats. Avoid implicit conversions between floats and half-floats.

Dropping precision is going to help some on the way to final delivery. As to where, it's not easy to find guidance. Boring to test it, too; documentation would be great!
