microsoft/qdk

Public

mirrored from https://github.com/microsoft/qdkAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.8.0

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

samples/language/ArithmeticOperators.qs

87lines · modeblame

1d8e82fcScott Carda2 years ago1/// # 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 `^`.
8namespace MyQuantumApp {
9
10@EntryPoint()
efd6847eManvi-Agrawal1 years ago11function Main() : Unit {
1d8e82fcScott Carda2 years ago12
13// `-`, when applied to a single value on its right, negates the value.
14
15// The integer value `-32`.
16let integer = -32;
17
18// The integer value `32`.
19let integer = --32;
20
21// The integer value `-32`.
22let integer = ---32;
23
24// `+` adds two values together.
25
26// The integer value `42`.
27let integer = 10 + 32;
28
29// `-` subtracts the right-hand value from the left-hand value.
30
31// The integer value `-22`.
32let integer = 10 - 32;
33
34// `*` multiplies the two values together.
35
36// The integer value `320`.
37let 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`.
43let integer = 32 / 10;
44
45// The integer value `-3`.
46let integer = -32 / 10;
47
48// The double value `3.2`.
49let 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`.
54let integer = 32 % 10;
55
56// The integer value `-2`.
57let integer = -32 % 10;
58
59// The integer value `2`.
60let integer = 32 % -10;
61
62// The integer value `-2`.
63let 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`.
19dcfa5fScott Carda2 years ago70let integer = 3^4;
1d8e82fcScott Carda2 years ago71
72// The integer value `-81`.
19dcfa5fScott Carda2 years ago73let integer = -3^4;
1d8e82fcScott Carda2 years ago74
75// The double value `256.0`.
19dcfa5fScott Carda2 years ago76let double = 16.0^2.0;
1d8e82fcScott Carda2 years ago77
78// The double value `-256.0`.
19dcfa5fScott Carda2 years ago79let double = -16.0^2.0;
1d8e82fcScott Carda2 years ago80
81// The double value `4.0`.
19dcfa5fScott Carda2 years ago82let double = 16.0^0.5;
1d8e82fcScott Carda2 years ago83
84// The double value `0.25`.
19dcfa5fScott Carda2 years ago85let double = 16.0^ -0.5;
1d8e82fcScott Carda2 years ago86}
87}