Chapter 3. Expressions
Prev |  | Next

# Chapter 3. 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 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 (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 preceeded “0b”, “0o” or “0x” are interpreted as a number with base 2, 8 or 16, respectively (if the default base is 10, e.g. “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 Mode → Number Base → Select Result and Expression Base.... Note that duodecimal (base 12) numbers use 'X' and 'E' (or upside down 2 and 3), instead of 'A' and 'B', for the tenth and eleventh digit. When input bases higher than ten, excluding twelve, are used, functions, variables and units are not parsed.

Intervals

  A number interval can be entered using the interval() function (specifies the upper and lower limit of the interval), or using “±” or “+/-”, specifying the width of the interval after the mid value (e.g. “5±1 = interval(4, 6)”). 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 arithmetic is deactivated, the interval interpreted as 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, 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 6, Variables.

Functions

  See Chapter 7, 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 8, 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 6, 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 are 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 calender 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 “;”).

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 6, Variables, Chapter 7, Functions and Chapter 8, 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. 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
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 (e.g. “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 (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 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 (e.g. “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 (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”).

## 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).

A physical constant

  Convert to variable as unit (e.g. c).

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.

duo / duodecimal

  Show as duodecimal number.

hex / hexadecimal

  Show as hexadecimal number.

sexa / sexagesimal

  Show as sexagesimal number.

time

  Show in time format.

roman

  Show as Roman numerals.

base #

  Show using the specified base (e.g. base 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.

exponential

  Show complex number in exponential form.

polar

  Show complex number in polar form.

fraction

  Show as number in combined fractional format.

partial fraction

  Show expanded partial fractions.

factors

  Show factorized (algebraic or integer factorization).

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.

Prev |  | Next
Chapter 2. User Interface | Home | Chapter 4. Calculator Modes