Integers and floating-point numbers

Source: Internet
Author: User

Integer

Integers are numbers that do not have fractional parts, such as 42 and -23 . Integers can be 有符号 (positive, negative, 0) or 无符号 (positive, 0).

Swift provides 8,16,32 and 64-bit signed and unsigned integer types. These integer types are similar to those of the C language, such as the 8-bit unsigned integer type UInt8 , which is the 32-bit signed integer type Int32 . As with other types of Swift, integer types are capitalized.

Integer range

You can access the and properties of different integer types min max to get the minimum and maximum values for the corresponding type:

let minValue = UInt8.min  // minValue 为 0,是 UInt8 类型let maxValue = UInt8.max  // maxValue 为 255,是 UInt8 类型

minand max the type of the returned value, exactly the type of the integer to which it is aligned (as in the example above, UInt8, the type returned is UInt8), which can be used next to the same type value in an expression.

Int

In general, you do not need to specifically specify the length of an integer. Swift provides a special integer type Int with the same length as the original word on the current platform:

    • On 32-bit platforms, the same as the Int Int32 length.
    • On 64-bit platforms, the same as the Int Int64 length.

Unless you need an integer of a specific length, Int it's generally enough. This can improve code consistency and reusability. Even on a 32-bit platform, the Int range of integers that can be stored can reach -2,147,483,648 ~ 2,147,483,647 , which is large enough for most of the time.

UInt

Swift also provides a special unsigned type UInt with the same length as the original word on the current platform:

    • On 32-bit platforms, the same as the UInt UInt32 length.
    • On 64-bit platforms, the same as the UInt UInt64 length.

Attention:
Try not to use it UInt unless you really need to store a unsigned integer that is the same as the current platform's native word length. In addition to this situation, it is best to use Int even if the value you want to store is known to be non-negative. Uniform use Int can improve the reusability of code, avoid conversions between different types of numbers, and match the type inference of numbers by reference to type safety and type inference.

Floating point number

Floats are numbers that have decimal parts, such as 3.14159 , 0.1 and -273.15 .

Floating-point types represent larger ranges than integer types and can store numbers that are larger Int or smaller than the type. Swift provides two types of signed floating-point numbers:

    • DoubleRepresents a 64-bit floating-point number. Use this type when you need to store large or very high-precision floating-point numbers.
    • FloatRepresents a 32-bit floating-point number. This type can be used if the accuracy requirement is not high.

Attention:
DoubleThe accuracy is high, at least 15 digits, and at least Float 6 digits. Which type to choose depends on the range of values that your code needs to handle.

Integers and floating-point numbers

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.