Chapter 4. Expressions
Prev |  | Next

# Chapter 4. Expressions

Expressions are mathematical statements. Mathematical questions are asked through expressions, which contains objects tied together with operators. The result of an expression may also be an expression, if the result is not a single object. Apples and oranges can be mixed, but the result will hold them apart. Qalculate! knows algebra.

## Objects

In Qalculate! mathematical entities, such as numbers and variables, are referred to as objects. The recognized object types are listed below.

Numbers

  These are the regular numbers composed by digits 0-9 and a decimal sign — a dot, or a comma if it is the default decimal point in the locale/language used. If comma is used as decimal sign, the dot is still kept as an alternative decimal sign, if not explicitly turned off in the preferences dialog with Ignore dots in number (to allow it to be used as thousand separator instead). Numbers include integers, real numbers, and complex numbers. The imaginary part of complex numbers is written as a regular number followed by the special variable “i” (can be changed to a “j”, placed in front of the imaginary part, in the preferences dialog), which represents the square root of -1 (e.g. “2 + 3i”). Spaces between digits are ignored (“5 5 = 55”). “E” (or “e”) can be considered as a shortcut for writing many zeroes and is equivalent to multiplication by 10 raised to the power of the right-hand value (e.g. “5E3 = 5000”).

  Sexagesimal numbers (and time) can be entered directly using colons (e.g. “5:30 = 5.5”). A number immediately preceded by “0b”, “0o”, “0d” or “0x” is interpreted as a number with base 2, 8, 12 or 16, respectively (if the default base is 10, e.g. “0x3f = 63”). The number base can also be selected, either by using the base(), bin(), oct(), hex() and roman() functions, or by setting the base used for all numbers in the whole expression from Mode → Number Base → Select Result and Expression Base.... For details about supported number bases see Table 5.1, “Supported Number Bases”.

Intervals

  A number interval can be entered using the interval() function (specifies the upper and lower limit of the interval), the uncertainty() function (specifies relative or absolute uncertainty), or using “±” or “+/-”, specifying the width of the interval after the mid value (e.g. “5±1 = uncertainty(5, 1, 0) = 5±20% = uncertainty(5, 0.2) = interval(4, 6)”. If activated, concise notation can also be used, e.g. “1.2345(67) = 1.2345±0.0067”. If the read precision option is activated, decimal numbers are interpreted as an interval between the numbers that are normally rounded to the entered number (e.g. “1.1 = 1.1±0.05”). If interval calculation using variance formula is activated (default), the interval represents the standard uncertainty (deviation) of the value.

Vectors and Matrices

  A matrix is a two-dimensional rectangular array of mathematical objects. Vectors are matrices with only one row or column, and thus one-dimensional sequences of objects. Vectors and matrices are generated by vector(), matrix() and similar functions, or using a syntax in the form of “[1 2 3 4]” and “[1 2; 3 4]”, with columns separated by space or comma and rows separated by semi-colon, or “(1, 2, 3, 4)” and “((1, 2), (3, 4))”. Vectors with a sequence of numbers can be input using “...” (e.g. “1...4”), or colon (e.g. “[1:4]”, or “[1:1:4]” where the second value specifies the increment). Vectors are generally considered as matrices with one row (row vector) in operations that expect a matrix (e.g. matrix multiplication).

  Matrices and vectors with many elements are easier to handle if stored in variables. A single element of vector variable can be selected using the element() function, or by placing the index (first index is 1) in parenthesis, e.g. “v(2)” or “v[2]” (the latter syntax can also be used for vector returning functions).

Variables/Constants

  See Chapter 7, Variables.

Functions

  See Chapter 8, Functions.

Units and Prefixes

  Qalculate! understands abbreviated, plural and singular forms of unit names and prefixes. Prefixes must be put immediately before the unit to be interpreted as prefixes — “5 mm = 0.005 m”, but “5 m m = 5 m^2”. Also, for convenience units allow the power operator to be left out. A number following immediately after a unit is interpreted as an exponent (e.g. “5 m2 = 5 m^2”). This does not apply to currencies, as they might be put in front of the quantity. More information in Chapter 9, Units.

Unknowns

  Unknowns are text strings without any associated value. These are temporary unknown variables with default assumptions. Unknowns can also be explicitly entered by placing a backslash (“\”) before a single character (e.g. “5\a + 2\b”) or using quotation marks (“"” or “'”) before and after a text string (e.g. “5 "apples" + 2 "bananas"”). If unknowns are activated (Mode+Enabled Objects → Unknowns) and Qalculate! finds a character that are not associated with any variable, function or unit in an expression, then it will be regarded as an unknown variable. See Chapter 7, Variables.

Date and Time

  Date/time values are specified using quoted text string (quotation marks are not needed for function arguments), using standard date and time format (YYYY-MM-DDTHH:MM:SS). Some local formats are also supported, but not recommended. The local time zone is used, unless a time zone is specified at the end of the time string (Z/UTC/GMT or +/-HH:MM). Date/time supports a small subset of arithmetic operations. The time units represents calendar time, instead of average values, when added or subtracted to a date.

Text

  This category represent a number of different function argument types, such as regular text and file names. They can, but do not need to be put in quotes except when containing the argument separator (“,” or “;”).

Comments

  All text after a hashtag (e.g. “(5×2)/2 #calculating triangle area”) is treated as a comment, which are added to the history. Use double hashtags (“##”) at the beginning of the expression to add a comment as a separate history item at the top.

To avoid confusion, functions, units, variables and unknown variables can independently be disabled.

Variables, functions and units are all accessible in the menus and in the variable, function and unit managers, If their names are not remembered. Functions accessed this way have some extra conveniences. If the function has at least one argument, a dialog will pop up where arguments can be entered and a description of the function and its arguments is available.

Qalculate! can handle most commonly used symbols for certain variables, functions and units, even though most are difficult to find on a keyboard. These include π for pi, √ for sqrt, € for euro, and so on. Most importantly it is possible to copy these symbols when used in the result.

For more information about variables, functions and units, see Chapter 7, Variables, Chapter 8, Functions and Chapter 9, Units.

## Operators

The following operators are defined in Qalculate! and may be used in expressions. Word operators (such as AND) must be surrounded by space (e.g. “5 mod 2”, not “5mod2”.

Table 4.1. Operators

Operation | Symbol | Description | Example | Result
--- | --- | --- | --- | ---
Addition | +, plus | Adds the right value to the left value. | 1 + 1 | 2
Subtraction | −, minus | Subtracts the right value from the left value. | 1 − 1 | 0
Multiplication | ×, ⋅, *, times | Multiplies the left value by the right value. | 2 × 2 | 4
Division | /, per | Divides the left value by the right value. | 2 / 2 | 1
Remainder | %, rem | Returns the remainder after (truncated) division. The result will have the same sign as the dividend. | 3%2 | 1
Modulo | %%, mod | Returns the remainder after (floored) division. The result will have the same sign as the divisor. | 3 mod -2 | -1
Integer Division | //, \, div | Divides the left value by the right value and rounds the result towards zero. | 5 // 2 | 2
Exponentiation | ^, ** | Raises the left value by the right value. Can also be typed as “**”. Note that x^y^z equals x^(y^z), and not (x^y)^z. Note also that for non-integer exponents with negative bases, the principal root is returned and not the real root (“(-8)^(1/3)” equals “1 + 1.73i” instead of -2). To calculate the real root for negative values, use the cbrt() and root() functions. | 2^3 | 8
10^x | E | Multiplies the left value with 10 raised to the power of the right value. Equivalent to the exponential number format in result display. E is as much an operator as part of numbers. | 1E3 | 1000
Factorial | ! | Returns the factorial of the value to the left of the operator. If the operator is repeated the corresponding multifactorial is returned. | 5! | 120
Parenthesis | ( and ) | Evaluates the expression in parenthesis first. | 5 × (1 + 1) | 10
Parallel sum | ∥, || | Returns the reciprocal value of a sum of reciprocal values. || is interpreted as parallel if units are used, otherwise as logical OR. | 10 Ω || 6 Ω | 3.75 Ω
Equals | = | Returns true if the left value equals the right value. Unknown variables (e.g. x) are isolated if the expression does not evaluate as true or false. | 1 = 2, 5x = 5 | 1, x=1
Not equals | ≠, != | Returns true if the left value does not equals the right value. Unknown variables (e.g. x) are isolated if the expression does not evaluate as true or false. | 1 != 2, x + 2 != 5 | 1, x != 3
Less than | < | Returns true if the left value is less than the right value. Unknown variables (e.g. x) are isolated if the expression does not evaluate as true or false. | 1 < 2 | 1
Greater than | > | Returns true, if the left value is greater than the right value. Unknown variables (e.g. x) are isolated if the expression does not evaluate as true or false. | 1 >2 | 0
Less than or equal | ≤, <= | Returns true if the left value is less than or equal the right value. Unknown variables (e.g. x) are isolated if the expression does not evaluate as true or false. | 1 <= 2 | 1
Greater than or equal | ≥, >= | Returns true if the left value is greater than or equal the right value. Unknown variables (e.g. x) are isolated if the expression does not evaluate as true or false. | 1 ≥ 2, x + 5 ≥ 7 | 0, x ≥ 2
Logical NOT | !, not | Returns true if the value to the right is false. | !(1>2) | 1
Logical OR | ||, or | Returns true if the right or left value is true. | 1>2 || 2>1 | true
Logical XOR | ⊕, xor | Returns for true if one, but not both, of the right or left value is true. | 1>2 ⊕ 2>1 | true
Logical NOR | nor | Returns true if both the right and left value is false. | 1>2 nor 2>1 | false
Logical AND | &&, and | Returns true if both the right and left value is true. | 1>2 && 2>1 | false
Logical NAND | nand | Returns true if the right or left value is false. | 1>2 nand 2>1 | true
Bitwise NOT | ¬, ~ | Equivalent to -1 − x. | ~(0010 | 1100) | -1111
Bitwise Shift Left | << | Shifts the bits of the left value x steps to the left, where x is the value on the right. Implemented as a shortcut for shift() | 0011 << 1 | 0110
Bitwise Shift Right | >> | Shifts the bits of the left value x steps to the right, where x is the value on the right. Implemented as a shortcut for shift() | 0011 << 1 | 0001
Bitwise OR | ∨, | | If a bit is 1 in one of the numbers set it to 1, otherwise 0. Also functions as elementwise logical operator on vectors. | 0010 | 1100 | 1110
Bitwise XOR | ⊻, ^^, xor | If a bit is 1 in one of the numbers and not in the other, set it to 1, otherwise 0. Can normally also be used as logical XOR. ⊻ can be input using Ctrl+^ (or just ^ if selected in preferences) on the keyboard. | 1010 ⊻ 1100 | 0110
Bitwise AND | ∧, & | If a bit is 1 in both numbers set it to 1, otherwise 0. Also functions as elementwise logical operator on vectors. | 1010 & 0011 | 0010
Dot Product | ., dot | Returns the dot product for two vectors. | [1, 2, 3].[4, 5, 6] | 32
Cross Product | ⨯, cross | Returns the cross product for two vectors. | [1, 2, 3] cross [4, 5, 6] | [-1, 6, -3]
Elementwise Multiplication | .×, .* | Multiplies each element of a vector/matrix with the corresponding element in another vector/matrix, or a scalar. | [1, 2, 3].*[4, 5, 6] | [4, 10, 18]
Elementwise Division | ./ | Divides each element of a vector/matrix by the corresponding element in another vector/matrix, or a scalar. | [2, 4, 6]./2 | [1, 2, 3]
Elementwise Exponentiation | .^ | Raises each element of a vector/matrix by the corresponding element in another vector/matrix, or a scalar. | [1, 2, 3].^2 | [1, 4, 9]
Transpose | .' | Returns the transpose of the matrix to the left of the operator. | [[1, 2, 3], [3, 4, 5]].' | [[1, 3], [2, 4], [3, 5]]
Combination | comb | Same as comb() function. | 5 comb 2 | 10
Permutations | perm | Same as perm() function. | 5 perm 2 | 20
Save as Variable/Function | :=, = | Saves the value or expression to the right of the operator as a variable or function (as save() function). If colon is omitted, the expression is calculated before it is assigned to the variable. | var1:=5 func1():=x+y var1=ln(5)+2 |

The multiplication sign can generally be left out. This is not true for numbers (“5(5) = 25” but “5 5 = 55”). Expressions can also generally be written with or without spaces with the same result (“2xsin(2)” equals “2 x sin(2)” which equals “2 × x × sin(2)”), but be careful. The vast number of functions and units means that without separating spaces, the result might not be obvious. To avoid confusion Qalculate! can limit the use of implicit multiplication (Mode → Limit Implicit Multiplication), so that space, operator or parenthesis must be put between functions, units and variables (in this mode “esqrt(5)” does not equal “e × sqrt(5)”). Also note that unit prefixes must be put immediately before the unit, to be interpreted as prefixes (“5 mm = 0.005 m”, but “5 m m = 5m^2”). You can see how the expression was interpreted in the history window.

Usually, mathematical expressions are written as normally expected. Standard operator precedence apply. Expressions are evaluated according to the following priorities:

1. Parenthesis

2. E (10^x)

3. Exponentiation (^, .^)

4. Functions (e.g. “sqrt(2)”)

5. Bitwise NOT (~)

6. Logical NOT (!)

7. Multiplication, division, integer division, remainder, modulo (*, /, //, %, %%, .*, ./, ., ⨯)

8. Parallel sum (∥)

9. Addition and subtraction (+, −)

10. Bitwise NOT (~)

11. Bitwise Shift (<<, >>)

12. Comparison (>, <, =, >=, <=)

13. Bitwise AND (&)

14. Bitwise XOR (⊻)

15. Bitwise OR (|)

16. Logical AND (&&)

17. Logical NAND

18. Logical NOR

19. Logical OR (||)

20. Logical XOR (⊕)

## Implicit Multiplication and Parsing Modes

The evaluation of short/implicit multiplication, without any multiplication sign (e.g. “5x”, “5(2+3)”), differs depending on the parsing mode. In the conventional mode implicit multiplication does not differ from explicit multiplication (“12/2(1+2) = 12/2×3 = 18”, “5x/5y = 5 × x/5 × y = xy”). In the “parse implicit multiplication first” mode, implicit multiplication is parsed before explicit multiplication (“12/2(1+2) = 12/(2 × 3) = 2”, “5x/5y = (5 × x)/(5 × y) = x/y”). The default adaptive mode works as the “parse implicit multiplication first” mode, unless spaces are found (“1/5x = 1/(5 × x)”, but “1/5 x = (1/5) × x”). In the adaptive mode unit expressions are parsed separately (“5 m/5 m/s = (5 × m)/(5 × (m/s)) = 1 s”). Function arguments without parentheses are an exception, where implicit multiplication in front of variables and units is parsed first regardless of mode (“sqrt 2x = sqrt(2x)”).

If the limit implicit multiplication option is activated, the use of implicit multiplication when parsing expressions and displaying results will be limited to avoid confusion. For example, if this mode is not activated and “integrte(5x)” is accidently typed instead of “integrate(5x)”, the expression is interpreted as “int(e × e × (5 × x) × gr × t)” (displayed in history window). The result will then without any error be “int(2.3940139x × km^2)” instead of “2.5x^2”. If limit implicit multiplication is activated, the mistyped expression would instead show an error telling that “integrte” is not a valid variable, function or unit (unless unknowns is enabled in which case the result will be “5 "integrate" × x”). When implicit multiplication is limited, variables, functions and units must be separated by a space, operator or parenthesis (“xy” does not equal “x × y”).

In addition there are two special parsing modes — RPN syntax (for details see the section called “The RPN Mode”) and chain syntax. The chain syntax interprets expressions in a manner similar to the immediate execution mode of a traditional calculator. Instead of using the standard order of operations, the expression is simply calculated from left to right (e.g. “1 + 2 × 3 = (1 + 2) × 3 = 9” instead of “1 + 2 × 3 = 1 + (2 × 3) = 7”). Functions, with a single argument, apply to the value immediate to the left of the function name (e.g. “1 + 2 sin = 1 + sin(2)”), unless parentheses are used.

## The “to” (and “where”) operators

Putting “ to ” (or a right arrow, e.g. “->” but not “>”) followed by an expression at the end of the mathematical expression is mainly used for unit conversion (see the section called “Conversion”). There are however also some convenient commands that can be typed after “ to ”. Here is a list of possible “to” values:

A unit or unit expression

  Convert to a unit or a unit expression (e.g. “5 ft + 2 in to meter = 1.5748 m” or “50 mph to km/h ≈ 80 km/h”). Prepend with a question mark (?) to request the optimal prefix. Modifiers in front of the question mark selects the type of prefixes used — 'b' for binary prefixes, 'd' for decimal prefixes, and 'a' for all decimal prefixes including centi, deci, etc. (e.g. “8 × 1024 bits to b?bytes = 1 kibibyte”). Prepend with + or - to force/disable use of mixed units (e.g. “5 m to + ft ≈ 5 yd + 1 ft + 4.9 in”).

A physical constant or a variable

  Convert to variable as unit (e.g. “500 km/ms to c ≈ 1.7 c”).

base

  Convert to base units (e.g. “1 lux to base = 1 cd/m2”).

optimal

  Convert to optimal unit (e.g. “(10 J)/(2 s) to optimal = 5 W”).

prefix

  Convert to optimal prefix (e.g. “€5000 to prefix = k€5”).

mixed

  Convert to mixed units (e.g. “90 s to mixed = 1 min + 30 s”.

bin, binary

  Show as binary number (e.g. “sqrt(900) to bin = 0001 1110”). Append an integer to specify the number of bits (e.g. “4 to bin16 = 0000 0000 0000 0100”).

oct, octal

  Show as octal number (e.g. “52 to octal = 64”).

duo, duodecimal

  Show as duodecimal number (e.g. “152 to duo = 108”).

hex, hexadecimal

  Show as hexadecimal number (e.g. “623 to hex = 026F”). Append an integer to specify the number of bits (e.g. “4 to hex16 = 0004”).

sexa, sexa2, sexa3, sexagesimal

  Show as sexagesimal number (e.g. “7.33 to sexagesimal = 7°19′48″”). For sexa2, arcseconds are hidden, and for sexa3 arcseconds are rounded.

longitude, longitude2, latitude, latitude2

  Show as sexagesimal latitude/longitude (e.g. “-7.33 to latitude = 7°19′48″S”). longitude2/latitude2 only shows degrees and arcminutes (e.g. “-7.33 to latitude2 = 7°19.8′S”).

bijective

  Show as bijective base-26 number (e.g. “731 to bijective = ABC”).

binary16 / fp16, binary32 / float / fp32, binary64 / double / fp64, fp80, binary128 / fp128

  Show as binary representation of IEEE 754 16-bit (half precision), 32-bit (single precision), 64-bit (double precision), 80-bit (x86 extended format), or 128-bit (quadruple precision) floating-point number.

time

  Show in time format (e.g. “7.25 to time = 7:15”.

roman

  Show as Roman numerals (e.g. “1984 to roman = MCMLXXXIV”).

Unicode

  Show as Unicode character(s) (uses UTF-32 for conversion, e.g. “0x178 to Unicode = Ÿ”).

base #

  Show using the specified base (e.g. “523 to base 20 = 163” or “circumference(1) to base pi = 20”).

bases

  Show as binary, octal, decimal, duodecimal, hexadecimal and Roman number (opens convert bases dialog with the mathematical expression).

rectangular, cartesian

  Show complex number in rectangular form (e.g. “0.28i − 2 to complex = 0.28i − 2”).

exponential

  Show complex number in exponential form (e.g. “0.28i − 2 to exponential ≈ 2e^(3i)”).

polar

  Show complex number in polar form (e.g. “0.28i − 2 to polar ≈ 2(cos(3) + i × sin(3))”).

angle, phasor

  Show complex number in angle/phasor notation (e.g. “0.28i − 2 to angle ≈ 2∠3”).

cis

  Show complex number in cis form (e.g. “0.28i − 2 to angle ≈ 2 cis 3”).

fraction, 1/n

  Show as mixed or simple (prepend with “-”) fraction (“1.25 to fraction = 1 + 1/4”).

1/#

  Show as mixed or simple (prepend with “-”) fraction with a specific denominator (“2.7 to 1/3 ≈ 2 + 2/3”, “2.7 to -1/3 ≈ 8/3”).

sci, scientific

  Show with scientific notation (“123456 to sci = 1.234 56 × 105”

eng, engineering

  Show with engineering notation (“1011 to eng = 100 × 109”

simple

  Show with non-scientific notation (“1015 to simple = 1 000 000 000 000 000”

partial fraction

  Show expanded partial fractions (e.g. “1 / (x2 + 2x − 3) to partial fraction = 1 ∕ (4x − 4) − 1 ∕ (4x + 12)”).

factors

  Show factorized (algebraic or integer factorization, e.g. “3 645 678 to factors = 857 × 709 × 3 × 2” or “x2 + 4x + 4 to factors = (x + 2)2”).

calendars

  Show date in different calendars (opens calendar conversion dialog).

UTC

  Show date and time using UTC time zone.

UTC+/-hh[:mm]

  Show date and time using specified time zone (e.g. UTC+08).

If “to” is not preceded by an expression, the previous result will be converted.

Similarly “where” (or alternatively “/.”) can be used at the end (but before “to”), for variable assignments, function replacements, etc. (e.g. “x+y where x=1 and y=2”, “x^2=4 where x>0”, and “sin(5) where sin()=cos()”). Variables assignments can also be placed before the expression, separated by comma, e.g. “x=1, y=2, x+y”, but this syntax is more strict.

Note that “to” and “where” can only be applied to the whole expression. Everything before the operator is always treated as the expression to convert (or apply replacement to), and everything after as the conversion/replacement expression, regardless of any parentheses.

Prev |  | Next
Chapter 3. User Interface | Home | Chapter 5. Calculator Modes