Chapter 3. Expressions

Table of Contents

Objects
Operators
Implicit Multiplication and Parsing Modes
The to operator

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 explicitely 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 are written with as regular number followed by the special variable i, which represents the square root of -1 (ex. 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 (ex. 5E3 = 5000).

Sexagesimal numbers (and time) can be entered directly using colons (ex. 5:30 = 5.5). A number immediately preceeded 0b, 0o or 0x are interpreted as a number with base 2, 8 or 16, respectively (if the default base is 10, ex. 0x3f = 63). The number base in 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 ModeNumber BaseSelect Result and Expression Base....

Vectors and Matrices

A matrix is a two-dimensional rectangular array of mathematical objects. Vectors are matrices with only one row or column, thus one-dimensional sequences of objects, Vectors and matrices are generated by vector(), matrix() and similar functions, or stored in a variable. Syntax in the form of [1, 2, 3, 4] and [[1, 2], [3, 4]] can also be used.

Variables/Constants

See Chapter 5, Variables.

Functions

See Chapter 6, 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 (ex. 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 7, Units.

Unknowns

Unknowns are text strings without any associated value. If 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. These are temporary unknown variables with default assumptions. Unknowns can also be explicitly entered by putting a text string in quotes. This is also necessary for undefined unknown variables with more than one character (ex.xy is just one object, while xy means x * y). See Chapter 5, Variables.

Text

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

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 has 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 5, Variables, Chapter 6, Functions and Chapter 7, Units.

Operators

The following operators are defined in Qalculate! and may be used in expressions.

Table 3.1. Operators

Operation

Symbol

Description

Example

Result

Addition

+

Adds the right value to the left value.

1 + 1

2

Subtraction

Subtracts the right value from the left value.

1 − 1

0

Multiplication

⋅ or *

Multiplies the left value by the right value.

2 * 2

4

Division

/

Divides the left value by the right value.

2 / 2

1

Exponent

^

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.

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

Parenthesis

( and )

Evaluates the expression in parenthesis first.

5 * (1 + 1)

10

Less than

<

Returns 1 for true, if the left value is is less than the right, and 0 for false.

1 < 2

1

Greater than

>

Returns 1 for true, if the left value is greater than the right, and 0 for false.

1 >2

0

Less than or equal

≤ or <=

Returns 1 for true, if the left value is less than or equal the right, and 0 for false.

1 <= 2

1

Greater than or equal

≥ or >=

Returns 1 for true, if the left value is greater than or equal the right, and 0 for false.

1 >= 2

0

Equals

=

Returns 1 for true, if the left value equals the right, and 0 for false.

1 = 2

0

Not equals

≠ or !=

Returns 1 for true, if the left value not equals the right, and 0 for false.

1 != 2

1

Logical NOT

!

Returns 1 for true, if the value to the right is false, and 0 for false.

!(1>2)

1

Logical OR

|| or OR

Returns 1 for true, if the right or left value is true, and 0 for false.

1>2 || 2>1

1

Logical AND

&& or AND

Returns 1 for true, if the right and left value is true, and 0 for false.

1>2 && 2>1

0

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 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


The operator names plus, minus, times, per, AND and OR may also be used, surrounded by space, for the corresponding operation (ex. 5 plus 2, but not 5plus2, equals 5 + 2). These operator names are localized, but AND and OR are always available. In addition to these operators there are a couple of shortcuts for certain functions, such as 5! which equals factorial(5).

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 (ModeLimit 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 to 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. Exponents (x^y)

  4. Functions (ex. sqrt(2))

  5. Multiplication and division (*, /)

  6. Addition and subtraction (+, −)

  7. Bitwise NOT (~)

  8. << and >>

  9. Bitwise AND (&)

  10. Bitwise OR (|)

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

  12. Logical NOT (!)

  13. Logical AND (&&)

  14. Logical OR (||)

Implicit Multiplication and Parsing Modes

The evaluation of short/implicit multiplication, without any multiplication sign (ex. 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 options is activated, the use of implicite 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 turned on to 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).

The to operator

Putting to 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

Convert to unit (e.g. meter, prepend with ? to request the optimal prefix).

A unit expression

Convert to units (e.g. km/h).

base

Convert to base units.

optimal

Convert to optimal unit.

mixed

Convert to mixed units (e.g. 5 ft + 8 in).

bin / binary

Show as binary number.

oct / octal

Show as octal number.

hex / hexadecimal

Show as hexadecimal number.

bases

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

fraction

Show as number in combined fractional format.

factors

Show factorized (algebraic or integer factorization).