microsoft/qdk

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.29.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

samples/language/ArithmeticOperators.qs

84lines · modeblame

ce8303b0Alex Hansen1 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 `^`.
1d8e82fcScott Carda2 years ago8
ce8303b0Alex Hansen1 years ago9function Main() : Unit {
1d8e82fcScott Carda2 years ago10
ce8303b0Alex Hansen1 years ago11// `-`, when applied to a single value on its right, negates the value.
1d8e82fcScott Carda2 years ago12
ce8303b0Alex Hansen1 years ago13// The integer value `-32`.
14let integer = -32;
1d8e82fcScott Carda2 years ago15
ce8303b0Alex Hansen1 years ago16// The integer value `32`.
17let integer = --32;
1d8e82fcScott Carda2 years ago18
ce8303b0Alex Hansen1 years ago19// The integer value `-32`.
20let integer = ---32;
1d8e82fcScott Carda2 years ago21
ce8303b0Alex Hansen1 years ago22// `+` adds two values together.
1d8e82fcScott Carda2 years ago23
ce8303b0Alex Hansen1 years ago24// The integer value `42`.
25let integer = 10 + 32;
1d8e82fcScott Carda2 years ago26
ce8303b0Alex Hansen1 years ago27// `-` subtracts the right-hand value from the left-hand value.
1d8e82fcScott Carda2 years ago28
ce8303b0Alex Hansen1 years ago29// The integer value `-22`.
30let integer = 10 - 32;
1d8e82fcScott Carda2 years ago31
ce8303b0Alex Hansen1 years ago32// `*` multiplies the two values together.
1d8e82fcScott Carda2 years ago33
ce8303b0Alex Hansen1 years ago34// The integer value `320`.
35let integer = 10 * 32;
1d8e82fcScott Carda2 years ago36
ce8303b0Alex Hansen1 years ago37// `/` 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 ago39
ce8303b0Alex Hansen1 years ago40// The integer value `3`.
41let integer = 32 / 10;
1d8e82fcScott Carda2 years ago42
ce8303b0Alex Hansen1 years ago43// The integer value `-3`.
44let integer = -32 / 10;
1d8e82fcScott Carda2 years ago45
ce8303b0Alex Hansen1 years ago46// The double value `3.2`.
47let double = 32.0 / 10.0;
1d8e82fcScott Carda2 years ago48
ce8303b0Alex Hansen1 years ago49// `%` gives the modulo of the left-hand value by the right-hand value.
1d8e82fcScott Carda2 years ago50
ce8303b0Alex Hansen1 years ago51// The integer value `2`.
52let integer = 32 % 10;
1d8e82fcScott Carda2 years ago53
ce8303b0Alex Hansen1 years ago54// The integer value `-2`.
55let integer = -32 % 10;
1d8e82fcScott Carda2 years ago56
ce8303b0Alex Hansen1 years ago57// The integer value `2`.
58let integer = 32 % -10;
1d8e82fcScott Carda2 years ago59
ce8303b0Alex Hansen1 years ago60// The integer value `-2`.
61let integer = -32 % -10;
1d8e82fcScott Carda2 years ago62
ce8303b0Alex Hansen1 years ago63// `^` 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 ago66
ce8303b0Alex Hansen1 years ago67// The integer value `81`.
68let integer = 3^4;
1d8e82fcScott Carda2 years ago69
ce8303b0Alex Hansen1 years ago70// The integer value `-81`.
71let integer = -3^4;
1d8e82fcScott Carda2 years ago72
ce8303b0Alex Hansen1 years ago73// The double value `256.0`.
74let double = 16.0^2.0;
1d8e82fcScott Carda2 years ago75
ce8303b0Alex Hansen1 years ago76// The double value `-256.0`.
77let double = -16.0^2.0;
1d8e82fcScott Carda2 years ago78
ce8303b0Alex Hansen1 years ago79// The double value `4.0`.
80let double = 16.0^0.5;
1d8e82fcScott Carda2 years ago81
ce8303b0Alex Hansen1 years ago82// The double value `0.25`.
83let double = 16.0^ -0.5;
1d8e82fcScott Carda2 years ago84}