Swift up and running--integers and floating-point numbers for the Park Swift series

Source: Internet
Author: User
Tags arithmetic

Swift up and running--integers and floating-point numbers

The original study of the park

Parking Learning Techniques Video

In Swift , numbers are divided into integers (for example: 1/10/100/1000, etc.) and floating-point number (for example: 3.14/1.44/2.71, etc.). Let's look at integers first.

Integers-Int & UInt

According to the content space (8/16/32/64-bit) occupied by an integer variable and whether the integer is signed (unsinged),Swift defines 8 different integer types altogether:

Int8 Int16 Int32 Int64
UInt8 UInt16 UInt32 UInt64

The first row of four types, representing 8/16/32/64-bit signed integers , and the second line is the corresponding unsigned integer type.

In general, however, we do not use these specific integer types directly in our code, we only use Int to define signed integers, and we use UInt to define unsigned integers. The Swift compiler converts an Int or UInt to the corresponding integer type based on the target compilation platform. For example:

On our 64-bit platform, we use the min and max methods, respectively, to see the range of values that Int and Int64 can express:

Int.minInt.maxInt64.minInt64.max

  

From the above comparison, we can find thatInt and Int64, they can express the range of values, is the same.

Common expressions of integers

In Swift , we can use a number of ways to express an integer. Includes 10-in ,16 -,8 -,2 -binary:

: number Literallet Fifteenindecimal = 15let Fifteeninhex = 0xFlet Fifteeninoctal = 0o17let fifteeninbinary = 0b1111

  

Well, we can use separators in numbers:

: number Literallet million = 1_000_000

  

"Unless we have a particularly specific requirement for our hardware platform to use UInt to define unsigned integers, we should always use Int as much as possible to express all integer types, even if we determine that an integer must be a non-negative number." This will give us the hassle of reducing many unnecessary types of conversions. ”

Best Practices
Floating point float & Double

In Swift , there are two different types of floating-point numbers depending on the range of accuracy that can be expressed:

    • float: A floating-point number that expresses up to 6 bits of precision ;
    • double: A floating-point number that can represent at least 15 bits of precision ;

We print Float and Doubleseparately with print:

var onethirdinfloat:float = 1/3var onethirdindouble:double = 1/3print (onethirdinfloat) print (onethirdindouble)

  

In addition to using regular decimal expressions for floating-point numbers, we can use the scientific notation. For example, the floating-point pi is represented:

var PI = 0.314e1pi = 314e-2

  

"If there is no clear demand, we should use Double to define floating-point numbers uniformly. ”

Best Practices
The type inference associated with the number

In Swift , we use an integer, and the compiler infers it to Int, using a floating-point number, which the compiler infers as a Double, for example:

var three = 3three.dynamictypevar Zeropointforteen = 0.14zeropointforteen.dynamictype

  

We can use DynamicType to see the type of a variable, from the above results we can see that the type of three is Int, and the type of Zeropointforteen is Double.

In Swift , we can compute the literal values of different types of numbers directly:

PI = 3 + 0.14pi.dynamictype

  


We can see that the integer value 3 and the floating-point number 0.14 can be added directly,andSwift Converts the added result into a Double.

However, in Swift , we cannot directly perform arithmetic operations on variables of different numeric types , such as:

PI = three + Zeropointforteen

  

When you do arithmetic on a variable, the types of all variables must be the same, and if the variable types are different, we must explicitly convert some of the variables into the type. Like this:

PI = Double (three) + Zeropointforteen

  

As you can see from the code above, we use:

Double (Value)

Converts a three of type Int into a floating-point number. Here, the reason we want to quote the conversion is because we did not really convert the three type from Int to double, but instead initialized a new doublewith a value of 3 with the value of three, Add the new Double and Zeropointforteen.

Swift up and running--integers and floating-point numbers for the Park Swift series

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.