Swift Language Guide (iii) Swift language basics: integers and floating-point numbers

Source: Internet
Author: User
Tags integer min naming convention range uppercase letter

Integer

An integer is an integer without a decimal number, such as 42,-23. Integers can be signed (positive, 0, negative), or unsigned (positive, 0).

Swift provides a 8,16,32,64-bit form of signed and unsigned integers that follow a naming convention similar to the C language. The type of a 8-bit unsigned integer is the type of the uint8,32-bit signed integer, which, like other types of the Swift language, is named with an uppercase letter. Int32

Bounds of integers

You can specify a minimum or maximum value for each integer type by using the min or Max attribute:

1 Let minimum = uint8.min//Minimum value equals 0, type is UInt8
2 Let maximum = Uint8.max//maximum equals 255, type is UInt8

The values of these properties (max and Min, etc.) are consistent with the corresponding number width (size) (Unit8 in the previous example), so you can use the expression in conjunction with other values of the same type.

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/extra/

Int

In most cases, you don't need to select the integer width used in the code. Swift provides an additional integer type--int that is consistent with the long width of the original words of the current platform (native word size).

· In 32-bit platform, INT is consistent with Int32 width.

· In 64-bit platform, INT is consistent with Int64 width.

Unless you need to handle integers of a certain width, you should stick int in the code to represent integers. This ensures the consistency and interoperability of the code. Even on a 32-bit platform, an INT can store any number between 2,147,483,648 and 2,147,483,647, which is already large enough for many integer intervals.

UInt

Swift also provides an unsigned integer type--uint that is consistent with the long width of the original words of the current platform (native word size).

· In 32-bit platform, INT is consistent with Int32 width.

· In 64-bit platform, INT is consistent with Int64 width.

Attention:

It is only necessary to use a UInt without integer type when the width is in line with the platform's native word length. Otherwise, use Int, even if the value to be stored must be non-negative. Sticking with Int to represent integer values helps ensure code interoperability, avoids conversions of different data types, and matches integer type inference, see type safety and type inference (the next chapter will be translated).

Floating point numbers

A float is a number that has a small number of parts, such as 3.14159, 0.1, and-273.15.

Floating-point numbers can represent a broader range of ranges than integers, and can hold values larger or smaller than integers. Swift provides two types of signed floating-point numbers:

· A Double represents a 64-bit floating-point number. Use it when floating-point values are very large or very precise.

· Float represents a 32-bit floating-point number. Use it when a floating-point number does not require 64-bit precision.

Attention:

Double has the precision of 15 decimal valid digits, while Float has a precision of 6 decimal digits. The appropriate floating-point number type should be chosen according to the character and range of the numerical value required by the code.

Author: cnblogs Joe.huang

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.