Swift enables all numeric types to support the basic arithmetic:
- Addition (
+
)
- Subtraction (
-
)
- Multiplication (
*
)
- Division (
/
)
1 + 2 // 等于 35 - 3 // 等于 22 * 3 // 等于 610.0 / 2.5 // 等于 4.0
Unlike the C language and objective-c, Swift defaults to not allowing overflow in numeric operations. But you can use Swift's overflow operator to reach your purposeful overflow (for example a &+ b
). See overflow operators for details.
The addition operator is also used for String
stitching:
"hello, " + "world" // 等于 "hello, world"
Two Character
values or one String
and one Character
value, the addition generates a new String
value:
let dog: Character = "d"let cow: Character = "c"let dogCow = dog + cow// 译者注: 原来的引号内是很可爱的小狗和小牛, 但win os下不支持表情字符, 所以改成了普通字符// dogCow 现在是 "dc"
See the concatenation of characters and strings for details.
To find the remainder operation
The remainder operation ( a % b
) is the number of times that the calculation is b
just good enough to be able a
to fill, returning the part that is more than that.
Attention:
The redundancy operation ( %
) is also called modulo operation in other languages. Strictly speaking, however, we look at the result of the operator's operation on negative numbers, and the "redundancy" is more appropriate than "modulo".
Let's talk about how the remainder is calculated 9 % 4
, and 4
How many times you calculate it will just fit 9
in:
Twice times, very good, that remainder is 1 (marked in orange)
In Swift, this is the expression:
9 % 4 // 等于 1
To get a % b
The result, the %
following equation is computed and output 余数
as the result:
A = (bx multiples) + remainder
When 倍数
the maximum value is taken, it will just fit into the a
box.
To put 9
and 4
substitute into the equation, we have to 1
:
9 = (4 × 2) + 1
The same method that I came to calculate -9 % 4
:
-9 % 4 // 等于 -1
To put -9
and 4
substitute the equation, -2
is the largest integer taken:
-9 = (4 × -2) + -1
The remainder is -1
.
When you balance negative numbers b
, b
the symbols are ignored. This means that a % b
the a % -b
results are the same as the result.
Calculation of floating-point number for remainder
Unlike the C language and objective-c,swift, floating-point numbers can be redundant.
8 % 2.5 // 等于 0.5
In this example, the 8
2.5
3
0.5
result is a Double
value, except for equal to remainder 0.5
.
Self-increment and self-increment operations
Like the C language, Swift also provides an operator that facilitates the self-increment ( ++
) and decrement () of the variable itself by 1 or minus 1 --
. Its operands can be shaped and floating-point.
var i = 0++i // 现在 i = 1
++i
i
The value is added 1 per call. In fact, ++i
it's i = i + 1
shorthand, and --i
it's i = i - 1
shorthand.
++
and is --
both pre-and post-op. ++i
, i++
, --i
and i--
both are valid formulations.
We need to note that these operators are modified i
with a return value. If you only want to modify i
the value, then you can ignore the return value. But if you want to use the return value, you need to be aware that the return value of the pre-and post-operation is different.
When ++
it comes to the front, it comes back again.
When ++
the back is placed, the first return to the self-increment.
For example:
var a = 0let b = ++a // a 和 b 现在都是 1let c = a++ // a 现在 2, 但 c 是 a 自增前的值 1
For the above example, let b = ++a
a
add 1 and return a
the value first. So a
and b
all are new values 1
.
Instead let c = a++
, it returns a
the value first, and then a
adds 1. So c
get a
The old value 1, and a
add 1 to become 2.
Unless you need to use i++
the features, it is recommended that you use ++i
and --i
, because the first modification after the return of such behavior is more in line with our logic.
unary minus
The sign of a value can be -
toggled with a prefix (that is, a unary minus):
let three = 3let minusThree = -three // minusThree 等于 -3let plusThree = -minusThree // plusThree 等于 3, 或 "负负3"
A unary minus ( -
) is written before the operand with no spaces in the middle.
Unary Plus
Unary Plus ( +
) returns the value of the operand without any change.
let minusSix = -6let alsoMinusSix = +minusSix // alsoMinusSix 等于 -6
Although a dollar +
does not work hard, but when you use a unary minus to express negative numbers, you can use a unary plus to express positive numbers, so your code will have symmetrical beauty.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Swift Numerical operations