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