microsoft/qdk
Publicmirrored fromhttps://github.com/microsoft/qdkAvailable
samples/language/ArithmeticOperators.qs
84lines · modecode
| 1 | // # Sample |
| 2 | // Arithmetic Operators |
| 3 | // |
| 4 | // # Description |
| 5 | // Arithmetic operators in Q# are used to perform basic mathematical operations |
| 6 | // on numerical values. The arithmetic operators in Q# are `-` (negation), `+`, |
| 7 | // `-`, `*`, `/`, `%`, and `^`. |
| 8 | |
| 9 | function Main() : Unit { |
| 10 | |
| 11 | // `-`, when applied to a single value on its right, negates the value. |
| 12 | |
| 13 | // The integer value `-32`. |
| 14 | let integer = -32; |
| 15 | |
| 16 | // The integer value `32`. |
| 17 | let integer = --32; |
| 18 | |
| 19 | // The integer value `-32`. |
| 20 | let integer = ---32; |
| 21 | |
| 22 | // `+` adds two values together. |
| 23 | |
| 24 | // The integer value `42`. |
| 25 | let integer = 10 + 32; |
| 26 | |
| 27 | // `-` subtracts the right-hand value from the left-hand value. |
| 28 | |
| 29 | // The integer value `-22`. |
| 30 | let integer = 10 - 32; |
| 31 | |
| 32 | // `*` multiplies the two values together. |
| 33 | |
| 34 | // The integer value `320`. |
| 35 | let integer = 10 * 32; |
| 36 | |
| 37 | // `/` divides the left-hand value by the right-hand value. |
| 38 | // When the operands are both integers, the result is truncated to an integer. |
| 39 | |
| 40 | // The integer value `3`. |
| 41 | let integer = 32 / 10; |
| 42 | |
| 43 | // The integer value `-3`. |
| 44 | let integer = -32 / 10; |
| 45 | |
| 46 | // The double value `3.2`. |
| 47 | let double = 32.0 / 10.0; |
| 48 | |
| 49 | // `%` gives the modulo of the left-hand value by the right-hand value. |
| 50 | |
| 51 | // The integer value `2`. |
| 52 | let integer = 32 % 10; |
| 53 | |
| 54 | // The integer value `-2`. |
| 55 | let integer = -32 % 10; |
| 56 | |
| 57 | // The integer value `2`. |
| 58 | let integer = 32 % -10; |
| 59 | |
| 60 | // The integer value `-2`. |
| 61 | let integer = -32 % -10; |
| 62 | |
| 63 | // `^` gives the exponent of the left-hand value raised to the power of the right-hand value. |
| 64 | // When the operands are both integers, the right-hand value cannot be negative. This is |
| 65 | // checked at runtime. |
| 66 | |
| 67 | // The integer value `81`. |
| 68 | let integer = 3^4; |
| 69 | |
| 70 | // The integer value `-81`. |
| 71 | let integer = -3^4; |
| 72 | |
| 73 | // The double value `256.0`. |
| 74 | let double = 16.0^2.0; |
| 75 | |
| 76 | // The double value `-256.0`. |
| 77 | let double = -16.0^2.0; |
| 78 | |
| 79 | // The double value `4.0`. |
| 80 | let double = 16.0^0.5; |
| 81 | |
| 82 | // The double value `0.25`. |
| 83 | let double = 16.0^ -0.5; |
| 84 | } |
| 85 | |