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” (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 preceeded “0b”, “0o”, “0d” or “0x” are 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 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.... For details about supported number bases see Table 4.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, 0.2) = 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 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, 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 “;”).

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 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. Word operators (such as AND) must be surrounded by space (e.g. “5 mod 2”, not “5mod2”.

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 | ×, ⋅, * | Multiplies the left value by the right value. | 2 × 2 | 4
Division | / | Divides the left value by the right value. | 2 / 2 | 1
Modulo | %, mod | Divides the left value by the right value. | 3 mod 2 | 1
Integer Division | //, div | Divides the left value by the right value and rounds the result towards zero. | 5 // 2 | 2
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 | ≤, <= | 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 | ≥, >= | 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 | ≠, != | 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 | Returns 1 for true, if the right or left value is true, and 0 for false. | 1>2 || 2>1 | 1
Logical AND | &&, 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 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

Operator names “plus”, “minus”, “times”, “per”, surrounded by space, for the corresponding operation (e.g. “5 plus 2” equals “5 + 2”). These operator names are localized.

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. Bitwise NOT (~)

6. Logical NOT (!)

7. Multiplication, division, and modulo (*, /, mod)

8. Addition and subtraction (+, −)

9. Bitwise NOT (~)

10. << and >>

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

12. Bitwise AND (&)

13. Bitwise XOR (⊻)

14. Bitwise OR (|)

15. Logical AND (&&)

16. 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” (and “where”) operators

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 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 incluing 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/m²”).

optimal

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

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

  Show as sexagesimal number (e.g. “7.33 to sexagesimal = 7°19′48″”).

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) (using the Unicode base, 1114112, 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

  Show as number in combined fractional format “1.25 to fraction = 1 + 1/4”.

partial fraction

  Show expanded partial fractions (e.g. “1 / (x² + 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 “x² + 4x + 4 to factors = (x + 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.

Similarily “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()”).

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