microsoft/qdk
Publicmirrored from https://github.com/microsoft/qdkAvailable
samples/language/ArithmeticOperators.qs
84lines · modeblame
ce8303b0Alex Hansen1 years ago | 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 `^`. | |
1d8e82fcScott Carda2 years ago | 8 | |
ce8303b0Alex Hansen1 years ago | 9 | function Main() : Unit { |
1d8e82fcScott Carda2 years ago | 10 | |
ce8303b0Alex Hansen1 years ago | 11 | // `-`, when applied to a single value on its right, negates the value. |
1d8e82fcScott Carda2 years ago | 12 | |
ce8303b0Alex Hansen1 years ago | 13 | // The integer value `-32`. |
| 14 | let integer = -32; | |
1d8e82fcScott Carda2 years ago | 15 | |
ce8303b0Alex Hansen1 years ago | 16 | // The integer value `32`. |
| 17 | let integer = --32; | |
1d8e82fcScott Carda2 years ago | 18 | |
ce8303b0Alex Hansen1 years ago | 19 | // The integer value `-32`. |
| 20 | let integer = ---32; | |
1d8e82fcScott Carda2 years ago | 21 | |
ce8303b0Alex Hansen1 years ago | 22 | // `+` adds two values together. |
1d8e82fcScott Carda2 years ago | 23 | |
ce8303b0Alex Hansen1 years ago | 24 | // The integer value `42`. |
| 25 | let integer = 10 + 32; | |
1d8e82fcScott Carda2 years ago | 26 | |
ce8303b0Alex Hansen1 years ago | 27 | // `-` subtracts the right-hand value from the left-hand value. |
1d8e82fcScott Carda2 years ago | 28 | |
ce8303b0Alex Hansen1 years ago | 29 | // The integer value `-22`. |
| 30 | let integer = 10 - 32; | |
1d8e82fcScott Carda2 years ago | 31 | |
ce8303b0Alex Hansen1 years ago | 32 | // `*` multiplies the two values together. |
1d8e82fcScott Carda2 years ago | 33 | |
ce8303b0Alex Hansen1 years ago | 34 | // The integer value `320`. |
| 35 | let integer = 10 * 32; | |
1d8e82fcScott Carda2 years ago | 36 | |
ce8303b0Alex Hansen1 years ago | 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. | |
1d8e82fcScott Carda2 years ago | 39 | |
ce8303b0Alex Hansen1 years ago | 40 | // The integer value `3`. |
| 41 | let integer = 32 / 10; | |
1d8e82fcScott Carda2 years ago | 42 | |
ce8303b0Alex Hansen1 years ago | 43 | // The integer value `-3`. |
| 44 | let integer = -32 / 10; | |
1d8e82fcScott Carda2 years ago | 45 | |
ce8303b0Alex Hansen1 years ago | 46 | // The double value `3.2`. |
| 47 | let double = 32.0 / 10.0; | |
1d8e82fcScott Carda2 years ago | 48 | |
ce8303b0Alex Hansen1 years ago | 49 | // `%` gives the modulo of the left-hand value by the right-hand value. |
1d8e82fcScott Carda2 years ago | 50 | |
ce8303b0Alex Hansen1 years ago | 51 | // The integer value `2`. |
| 52 | let integer = 32 % 10; | |
1d8e82fcScott Carda2 years ago | 53 | |
ce8303b0Alex Hansen1 years ago | 54 | // The integer value `-2`. |
| 55 | let integer = -32 % 10; | |
1d8e82fcScott Carda2 years ago | 56 | |
ce8303b0Alex Hansen1 years ago | 57 | // The integer value `2`. |
| 58 | let integer = 32 % -10; | |
1d8e82fcScott Carda2 years ago | 59 | |
ce8303b0Alex Hansen1 years ago | 60 | // The integer value `-2`. |
| 61 | let integer = -32 % -10; | |
1d8e82fcScott Carda2 years ago | 62 | |
ce8303b0Alex Hansen1 years ago | 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. | |
1d8e82fcScott Carda2 years ago | 66 | |
ce8303b0Alex Hansen1 years ago | 67 | // The integer value `81`. |
| 68 | let integer = 3^4; | |
1d8e82fcScott Carda2 years ago | 69 | |
ce8303b0Alex Hansen1 years ago | 70 | // The integer value `-81`. |
| 71 | let integer = -3^4; | |
1d8e82fcScott Carda2 years ago | 72 | |
ce8303b0Alex Hansen1 years ago | 73 | // The double value `256.0`. |
| 74 | let double = 16.0^2.0; | |
1d8e82fcScott Carda2 years ago | 75 | |
ce8303b0Alex Hansen1 years ago | 76 | // The double value `-256.0`. |
| 77 | let double = -16.0^2.0; | |
1d8e82fcScott Carda2 years ago | 78 | |
ce8303b0Alex Hansen1 years ago | 79 | // The double value `4.0`. |
| 80 | let double = 16.0^0.5; | |
1d8e82fcScott Carda2 years ago | 81 | |
ce8303b0Alex Hansen1 years ago | 82 | // The double value `0.25`. |
| 83 | let double = 16.0^ -0.5; | |
1d8e82fcScott Carda2 years ago | 84 | } |