Swift Language Guide (v) digital literal and numeric type conversions in Swift language

Source: Internet
Author: User
Tags constant constructor integer numeric lowercase numeric value range ranges

Number literal amount

The integer digital surface is written as follows:

· decimal digits, no prefix

· Binary number, prefixed by 0b

· Octal number, prefixed by 0o

· hexadecimal digits, prefixed by 0x

All the following integer literal values are 17 of the decimal number:

1 Let Decimalinteger = 17
2 Let Binaryinteger = 0b10001//172 binary ID
3 Let Octalinteger = 0o21//178 binary ID
4 Let Hexadecimalinteger = 0x11//176 binary ID

The floating-point literal can be binary (no prefix) or hexadecimal (prefixed by 0x) and must have a number (or hexadecimal number) on either side of the decimal point. They can all have an optional exponential power, decimal decimal is marked as uppercase or lowercase e, and hexadecimal is marked with uppercase or lowercase p.

The exponent power is the decimal number of exp, and its cardinality is multiplied by 10exp:

· 1.25e2 means 1.25x102, or 125.0.
· 1.25e-2 means 1.25x10-2, or 0.0125.

The exponent power is the hexadecimal number of exp, and its cardinality is multiplied by 2exp:

· 0XFP2 says 15x22, or 60.0.
· 0xfp-2 says 15x2-2, or 3.75.

The literal values for all the following floating-point numbers are decimal value 12.1875:

1 Let decimaldouble = 12.1875
2 Let exponentdouble = 1.21875e1
3 Let hexadecimaldouble = 0xc.3p0

Numeric literals can contain other formats for easy reading. Both integers and floating-point numbers can add extra 0 or underscores to improve readability. Both formats do not affect the actual value of the literal:

1 Let paddeddouble = 000123.456
2 Let onemillion = 1_000_000
3 Let justoveronemillion = 1_000_000.000_000_1

Numeric type conversions

The Int type should be used as the type of integer constants and variables for all general purposes, even if they are non-negative. In daily use, using the default integer type means that these integer constants and variables can be immediately interacting with each other and can match the types inferred from the integer value.

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

They are used only when the task in hand must use other integer types, such as the external data source to provide a clear width of the data, or for performance, memory footprint, and other necessary optimizations. Using a width-specific type in these cases helps to uncover accidental numerical overflows and capture raw information about the data that is used.

Integer conversions

The range of ranges that can be stored by different numeric types is different. A Int8 constant or variable can store numbers between 128 and 127, while a UInt8 constant or variable can store numbers from 0 to 255. A number that cannot be stored in a constant or variable of an integral type will have an error when compiling:

1 Let cannotbenegative:uint8 =-1
2//UInt8 cannot save negative numbers, so there will be an error.
3 Let Toobig:int8 = Int8.max + 1
4//Int8 cannot save numbers that exceed their maximum range.
5//So there will be an error.

Because different data types can store different ranges, specific problems need to be treated in data conversion. This actual selection process avoids the problem of implicit conversions and reinforces the intent of type conversions in your code.

To convert the type of a number to another, you should first initialize the existing value to a new number of the desired type. In the following example, the type of the constant Twothousand is UInt16, and the type of constant one is UInt8. They cannot be added directly because the types are different. Therefore, this example invokes UInt16 (one) to create a new UInt16 number, initializes a numeric value, and replaces the original (the original value is UInt8) with the new initialized value (the new value is UInt16):

1 Let twothousand:uint16 = 2_000
2 Let One:uint8 = 1
3 Let Twothousandandone = Twothousand + UInt16 (one)//initialize constant one to UInt16

Now both sides of the plus sign are UInt16 types, so the addition is valid. The inferred type of the output constant (twothousandandone) is UInt16 because it is the sum of two UInt16 values.

Some types (initial values) are the default method of calling the Swift type constructor and passing the initial value. Behind the scenes, UInt16 has a constructor that accepts the UInt8 value, so the constructor is used to create a new UInt16 based on an existing UInt8. However, it is not possible to pass any type here--only incoming UInt16 provide a type of constructor. To extend an existing type to provide a constructor that accepts a new type, including its own defined type, see the Chapter Extension (later translated).

Conversions between integers and floating-point numbers

Conversions between integers and floating-point types must be explicitly specified:

1 Let three = 3
2 Let Pointonefouronefivenine = 0.14159
3 Let pi = Double (three) + Pointonefouronefivenine
4//pi equals 3.14159, so the inferred type is double

In the example above, the value of the constant three is used to create a new Double type so that the types on both sides of the plus sign are consistent and are not allowed to add if the type has no conversion.

Conversely, the conversion of floating-point numbers to integers is equally feasible, and integer types can be initialized with Double or float values:

1 Let Integerpi = Int (PI)
2//Integerpi equals 3, type inferred to int

When this is initialized to a new integer with floating-point numbers, the floating-point values are always truncated. That is, 4.75 becomes 4,-3.9 becomes-3.

Attention:

The binding rules of numeric constants or variables are different from the binding rules of numeric literals. Literal 3 can be added directly to the literal 0.14159, because the number literal does not explicitly specify the type, nor does it have an explicit type. The type is inferred only when evaluated by the compiler.

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.