Welcome to Swift (initial translation and annotations of Apple's official Swift document 10)---63~69 page (chapter II)

Source: Internet
Author: User

If you use an integral type with a floating-point type, the result will be considered a double type:

ET anotherpi = 3 + 0.14159

The type of ANOTHERPI is double

In the above code, the value of 3 is not explicitly stated, so the return value is double based on the remaining part of the floating-point type.

Numeric literals (binary representation of values)

An integer type can be represented as follows:

    • A decimal number, with no prefix//decimal value, no prefix symbol required;
    • A binary number, with a 0b prefix//binary value, using the 0b prefix;
    • An octal number, with a 0o prefix//Eight binary value, using the 0o prefix;
    • A hexadecimal number, with a 0x prefix//16 binary using the 0x prefix;

In the following code, all the integer values are in decimal 17:

Let Decimalinteger = 17

Let Binaryinteger = 0b10001//+ in binary notation

Let Octalinteger = 0o21//+ in octal notation

ET Hexadecimalinteger = 0x11//in hexadecimal notation "

Floating-point types can be either decimal or hexadecimal. You must have a value on both sides of the decimal point, and floating-point types can also use exponential form, in decimal, the exponent uses uppercase or lowercase e, in hexadecimal, uppercase or lowercase p is used.

For decimal, use exponential exp, which indicates that the value is multiplied by 10 exp power

1.25e2 means 1.25x102, or 125.0.

1.25e-2 means 1.25x10-2, or 0.0125.

Use exponential exp for hexadecimal numbers, which indicates that the value is multiplied by 2 exp power

0XFP2 means 15x22, or 60.0.

0xfp-2 means 15x2-2, or 3.75.

In the following code, all values are in decimal 12.1875:

Let decimaldouble = 12.1875

Let exponentdouble = 1.21875e1

Let hexadecimaldouble = 0xc.3p0

The representation of numeric values can be extended to make them more readable. Integer and floating-point types can be added 0 and underline to help improve readability. The extended format does not change the type of the numeric value:

Let paddeddouble = 000123.456

Let onemillion = 1_000_000

Let justoveronemillion = 1_000_000.000_000_1

Numeric type Conversion (numeric type conversion)

In code, defining integer constants and integer variables, int is more versatile, even if the value you use is non-negative. Using the default type of int means that your code can quickly match the type of an integer value.

The type you specify is recommended only when you need to handle a certain numeric integer, because the int type automatically adapts to the extended data for performance, memory usage, and efficiency.

Integer Conversion (integer conversion)

Constants or variables of different integer numeric types whose numeric valid range is different. A constant or variable of type Int8, the range of which is a constant or variable of type 128 to 127,uint8, and the range of values is 0 to 255. When the value exceeds the range of values that a constant or variable can store, the code will error when compiling:

Let cannotbenegative:uint8 =-1

UInt8 type cannot store negative numbers and therefore will error

Let toobig:int8 = Int8.max + 1

The Int8 type cannot store a number that exceeds the maximum value.

So this line of code will error.

Since the type of each numeric value can store a different range of values, you have to choose which type of value to convert.

Converting a numeric type can use a value that has already been defined when the new value is initialized. In the following code sample, the type of the constant Twothousand I is UInt16, and the constant one is type UInt8, and they cannot be added directly. Because they are not the same type. However, the code uses UInt16 (one) to create a new UInt16 type value instead of the original type value.

Let twothousand:uint16 = 2_000

Let One:uint8 = 1

Let Twothousandandone = Twothousand + UInt16 (one)

In this way, the numeric types on both sides of the plus sign are UInt16, so it is possible to perform operations. The type of the constant (Twothousandandone) will be UInt16, because the two values added are the UInt16 types.

In Swift, the default type initialization is to use SomeType (Ofinitialvalue) to pass an initial value. In the preceding code example, UInt16 can receive values of the Uint8 type, so you can use the defined UInt8 type value to produce a A new type value for the UInt16. No other type can be used.

Integer and floating-point Conversion (integer and floating-point conversions)

The conversion between an integer value and a floating-point value must be explicitly declared:

Let three = 3

Let Pointonefouronefivenine = 0.14159

Let pi = Double (three) + Pointonefouronefivenine

Pi equals 3.14159, and is inferred to be of type Double

In the code, double (three) uses the value of the constant three to create a new value of type Double, so the number on both sides of the plus sign is the same and can be evaluated. If you do not convert, the operation cannot be performed.

The integer type is loaded from a floating-point type and is the same as above. Integer values can be initialized with a double or a value of type float:

Let Integerpi = Int (PI)

Integerpi equals 3, the type is int

Floating-point class value-type values are removed from the fractional part. For example, a 4.75-turn integer is a 4;-3.9 conversion integer that is 3.

Watch out.

The use of numeric constants or variables differs from numeric literals. 3 of the numeric literal type can be added directly to the numeric literal type 0.14159, because the numeric text itself does not have an obvious type declaration. Their type is only determined when compiling processing.

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.