Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift Difference Between Double and Float64

Tags:

swift

In Swift Programing language official documentation, It says

Double represents a 64-bit floating-point number.
Float represents a 32-bit floating-point number.

Link: https://docs.swift.org/swift-book/LanguageGuide/TheBasics.html#ID321

Then, Why is there Float64? What is the difference between them? Or Are they same?

like image 663
Saleh Enam Shohag Avatar asked Apr 25 '26 07:04

Saleh Enam Shohag


2 Answers

The headers, found by hitting command+shift+o and searching for Float64, say:

/// A 64-bit floating point type.
public typealias Float64 = Double
/// A 32-bit floating point type.
public typealias Float32 = Float

and

Base floating point types 

    Float32         32 bit IEEE float:  1 sign bit, 8 exponent bits, 23 fraction bits
    Float64         64 bit IEEE float:  1 sign bit, 11 exponent bits, 52 fraction bits  
    Float80         80 bit MacOS float: 1 sign bit, 15 exponent bits, 1 integer bit, 63 fraction bits
    Float96         96 bit 68881 float: 1 sign bit, 15 exponent bits, 16 pad bits, 1 integer bit, 63 fraction bits

Note: These are fixed size floating point types, useful when writing a floating
      point value to disk.  If your compiler does not support a particular size 
      float, a struct is used instead.
      Use of of the NCEG types (e.g. double_t) or an ANSI C type (e.g. double) if
      you want a floating point representation that is natural for any given
      compiler, but might be a different size on different compilers.

As a general rule, unless you’re writing code that is dependent on binary representations, you should use the standard Float v Double names. But if you are writing something where binary compatibility is needed (e.g. writing/parsing binary Data to be exchanged with some other platform), then you can use the data types that bear the number of bits in the name, e.g. Float32 vs. Float64 vs. Float80.

like image 128
Rob Avatar answered Apr 27 '26 20:04

Rob


Go to the definiation of Float64

/// A 64-bit floating point type.
public typealias Float64 = Double

/// A 32-bit floating point type.
public typealias Float32 = Float
like image 39
William Hu Avatar answered Apr 27 '26 21:04

William Hu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!