Swift basic integer and floating point numbers

One: integer

Integers are numbers without fractional parts, such as 42 and -23. Integers can be signed (positive, negative, zero) or unsigned (positive, zero).

Swift provides 8, 16, 32, and 64-bit signed and unsigned integer types. These integer types are very similar to the C language naming method, for example, the 8-bit unsigned integer type is UInt8, and the 32-bit signed integer type is Int32. Like other types in Swift, integer types use uppercase nomenclature.

1. Integer range

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

let minValue = UInt8.min // minValue 为 0,是 UInt8 类型的最小值
let maxValue = UInt8.max // maxValue 为 255,是 UInt8 类型的最大值

2. Int

Generally speaking, you do not need to specify the length of the integer. Swift provides a special integer type Int with the same length as the native word length of the current platform:

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

Unless you need an integer of a specific length, Int is generally sufficient. This can improve code consistency and reusability. Even on a 32-bit platform, the integer range that Int can store can reach-2147483648~2147483647, which is large enough most of the time.

3. UInt

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

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

Note : Try not to use UInt, unless you really need to store an unsigned integer with the same length as the native word length of the current platform
. Except for this situation, it is best to use Int, even if the value you want to store is known to be non-negative. The unified use of Int can improve the reusability of the code, avoid the conversion between different types of numbers, and match the type speculation of numbers. Please refer to
Type Safety and Type Speculation.

Two: floating point number

Floating point numbers are numbers with fractional parts, such as 3.14159, 0.1 and -273.15.

The floating-point type represents a larger range than the integer type, and can store larger or smaller numbers than the Int type. Swift provides two types of signed floating-point numbers:

  • Double represents a 64-bit floating point number. Please use this type when you need to store large or very high-precision floating-point numbers.
  • Float represents a 32-bit floating point number. This type can be used if the precision is not high.

Note : Double has high accuracy, at least 15 digits, while Float has at least 6 digits. Which type you choose depends on the range of values ​​that your code needs to handle.

Welcome to follow the official account [Swift Community]:

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_36478920/article/details/103473248