05Operators
Arithmetic
Addition, subtraction, multiplication, division, remainder and negation.
Expected output
၂၂
၁၂
၈၅
၃
၂
-၁၇
၂၂
၁၂
၈၅
၃
၂
-၁၇
| Sign | Words | Reads as | Operation |
|---|---|---|---|
a + b | a နှင့် b ၏ ပေါင်းလဒ် | "the sum of a and b" | addition |
a - b | a နှင့် b ၏ နုတ်လဒ် | "the difference of a and b" | subtraction |
a * b | a နှင့် b ၏ မြှောက်လဒ် | "the product of a and b" | multiplication |
a / b | a ကို b ဖြင့် စားသော စားလဒ် | "a over b: the quotient" | division |
a % b | a ကို b ဖြင့် စားသော စားကြွင်း | "a over b: the remainder" | remainder |
-a | အနုတ် a | "minus a" | negation |
Dividing integers
Dividing two integers gives an integer. The fraction is dropped, so ၁၇ / ၅ is ၃, not
၃.၄. The remainder % gives what is left over: 17 = 3 × 5 + 2.
To keep the fraction, divide decimal numbers: ၁၇.၀ / ၅ is ၃.၄.
Expected output
၃
၃.၄
-၃
-၁
Division with zero as the divisor has no answer. If the compiler can see the zero, the program does not run:
Expected error
error[ZK0602]: / with the constant zero as divisor
If the zero first appears while the program runs, the program stops at that point:
Expected output
အစ
the program stopped: integer division or remainder with zero as divisor
Mixing kinds
Two integers, or two decimal numbers, combine with no extra step. The smaller kind
is widened to the larger one. An ၈ ဘစ် ကိန်းပြည့် plus a ကိန်းပြည့် gives a ကိန်းပြည့်. But a plain
integer and a decimal number need an explicit conversion, because a
large integer could lose digits as a decimal:
Expected error
error[ZK0404]: cannot combine isize and f64 without a conversion
Numbers written straight into the program bend to fit: ၃ + ၀.၅ is ၃.၅.
Too big for the kind
Each integer kind has a largest and a smallest value (see Integers). If a result goes past the edge while the program runs, it wraps around to the other end, like a car's odometer rolling over:
Expected output
-၁၂၈
၂၅၅
Pick a kind big enough for the numbers at hand. Plain ကိန်းပြည့် is a safe choice.
Hands on
- Work out the average of 4, 7 and 10 as a decimal number.
- Use
%to print whether 2026 over 4 leaves a remainder.
Next: Comparison.