From Fōrmulæ wiki
Jump to: navigation, search

This page is a tutorial to do arithmetic in Fōrmulæ


There are two kind of numbers in Fōrmulæ: Integer and decimal numbers. Of course, the first category is to manage integer numbers exclusively, but the second can hold both integer and decimal numbers.

Normally, integer numbers are easily distinguishable from decimal ones, because the latter contain a decimal separator and fractional part (for example 12.34), while the former one does not (for example 50).

How could we distinguish a given integer number from the decimal number with the same value? this is, to differentiate the (integer) number 50 from the decimal number 50 ? The answer is easy, a decimal number always shows the decimal separator, even if it contains an integer number.


There are two visualization of the Number expression, one of them uses the notation X. to represent a decimal number containing an integer, while the other uses the notation X.0. The choice depends on personal preference. You can select which of them to use.

In Fōrmulæ, the visualization of numbers depends on your locale. In some places (like United States) the decimal separator is a dot (.), in other places is a comma (,).

Numbers are also shown using a group separator. In some places (like United States) it is a comma (,), in other places (such as Italy) is a dot (.), while others (like in France) is just a space.

By default, Fōrmulæ uses the local according to the installed operating system of the computer where it runs. You can also define the local to be used, as it is noted in the installing and using the Desktop fornt-end tutorial.

Please notice that we are using United States, France and Italy as examples, because they are common places that use different notations. If you use Fōrmulæ in any other locale, the visualization of numbers will be accordingly to it.

The following table, borrowed from the Number page shows examples of different visualization of both integer and decimal numbers in different locales, for the two visualization styles X. and X.0:

Number type Visualization in United States Visualization in France Visualization in Italy
Style X. X.0 X. X.0 X. X.0
Integer number 1,234 1 234 1.234
Decimal number with fractional part 1,234.567 1 234,567 1.234,567
Decimal number with no fractional part 1,234. 1,234.0 1 234, 1 234,0 1.234, 1.234,0
Decimal number with no integer part 0.567 0,567 0,567
Integer zero 0 0 0
Decimal zero 0. 0.0 0, 0,0 0, 0,0

In the examples of this page, the United States and the X.0 notation will be used.


When you select an edition to create a Number (usually pressing the KeyN.png key), you are requested to enter it in a dialog box such the following:

Arithmetic0 1.png

In order to differentiate an integer number from a decimal number holding an integer value, the rule is the same that visualization: A decimal number must contain a decimal separator, even if it holds an integer value.

The decimal separator also depends of the locale. It is not required that the entered text contains group separators.

Number type Input in United States Input in France Input in Italy
Integer number 1234 1234 1234
Decimal number with fractional part 1234.567 1234,567 1234,567
Decimal number with no fractional part 1234. or 1234.0 1234, or 1234,0 1234, or 1234,0
Decimal number with no integer part .567 or 0.567 ,567 or 0,567 ,567 or 0,567
Integer zero 0 0 0
Decimal zero 0. or 0.0 0, or 0,0 0, or 0,0


A Number expression, as it is expected, does not reduce to any other expression:


Simple arithmetic operations

You can do simple arithmetic operation, like with a calculator


Unlike a calculator, you can use numbers arbitrarily big, or arbitrarily small:


Even if the numbers involved are big or contain many digits, arithmetic operations such as the addition, subtraction or multiplication are performed without truncating or rounding the numbers, producing exact results.

Unfortunately, some other mathematical operations such as the division, the exponentiation (in several specific cases), trigonometric or hyperbolic functions (such as sine, cosine, etc.), or trascendental functions (such as logarithms) produce numbers that cannot be represented in an exact form, because it would require an infinite numbers of digits.

In this article, we will refer these kind of numbers as numbers with infinite representation.

The following table shows examples of numbers with infinite representation.

Examples of numbers with infinite representation
Number Truncated representation Comments
The result of the division 1 ÷ 3 0.3333333333 ... Exact representation would require an infinite number of 3's
π 3.141592654 ... Exact representation would require an infinite number of digits
Square root of 2 1.414213562 ... Exact representation would require an infinite number of digits
Sine of 1 radian 0.8414709848 ... Exact representation would require an infinite number of digits

Accuracy preservation

If you make the division 1 ÷ 3 in a calculator, you surely will get something like "0.3333333333" (the number of 3's depends of the precision supported by the calculator). This result is not exact, because if is later multiplied by 3, you might not get back the number 1, but something like "0.9999999999". It is said that truncating or rounding a number with infinite representation up to a finite number of digits does not preserve accuracy. This is because the correct answer has an infinite number of digits, but this is not very manageable by humans (and computers).

So, is there a way to represent the result of the division 1 ÷ 3 as an exact form? Yes, it is just the fraction 13.

Is there a form to represent the number π? It is precisely π, this is why it was invented!

Is there a way to represent using a finite set of symbols? Yes, it is precisely .

These representations are exact and they preserve accuracy. They are also known as symbolic or algebraic, and they are very common in symbolic systems such as Fōrmulæ.

So, in Fōrmulæ:


This is so because they already are in their exact forms.

Hence they preserve accuracy:

Arithmetic3 1.png

Now, let us do the following operation in Fōrmulæ:


Although the input expression 69 is exact, there is another expression having the same mathematical value: 23 (because they are equivalent fractions). In fact, the fraction 69 has infinitely many equivalent fractions, but the simplest one is 23

Fōrmulæ tries to give the exact and simplest result. This is a rule of several contained in something that we call the Arithmetic canon.

See the following examples:


Fōrmulæ is able to compute expressions with many operations:


Notice that Fōrmulæ will try to give an exact number as result, if it is possible:


Overriding accuracy

There are situations where the user wants to get or manage non-exact values or results.

The accuracy and simplicity is the default behavior that can be overridden. There are two methods to do that.

Method 1. Using decimal numbers

If you specify at least one decimal number in most of the arithmetic operations, it means that you want the result as a decimal number too (and therefore inexact):


There are cases (such as the 1 ÷ 3 operation), that the result has a infinite number of digits, in such a case, the number of calcluated digits is the current value of the significant figures, or significat digits. By default, it is 10 digits.

Some of the previous operations can now be performed, with decimal but inexact results:



The truncation of numbers to significant digits is only used for numbers with infinite representation. In the following example, there is no results that are numbers with infinite representation, so the calculation are done with all the precision of the numbers:

Arithmetic9 1.png

Notice that if you use this method, even if the result of the mathematical operation fit as an integer number, the result is always decimal:



There are operations that do not follow the rule. It is not an error, this is because it is natural these expression to do so. see the following examples:

The expression Sign always returns -1, 0 or 1 (integers) to indicate the sign (negative, zero or positive) of the number given as its parameter. It does not matter if the input is an integer number or decimal number:

Arithmetic10 1.png

The expression Truncate always returns an integer, because it removes the fractional part of the number given as input, it does not matter if the input is an integer number or decimal number:

Arithmetic10 2.png

The Ceiling expression (see ceiling) also returns always an integer number (if its input is an integer of decimal number):

Arithmetic10 3.png

The IntegerPart and FractionalPart expression return the integer and fractional part of a number respectively, ignoring the sign. The former always returns an integer number, the latter always returns a decimal number:

Arithmetic10 4.png

The ToInteger always returns an integer number:

Arithmetic10 5.png

While the ToDecimal always returns a decimal number:

Arithmetic10 6.png

Method 2. Using the Numeric expression

The Numeric expression takes any expression as its input. It converts all the integer numbers into the expression as decimal, and so the operations can be performed as in the first method.

It also convert mathematical constants such as π to numbers, truncated according to the current value of the significant digits.


The Numeric expression also has a two-parameter version. In this, the second parameter specifies a value to the number of significant digits


In the following example, we can get the numeric value of π with 5,000 digits of precision. The image was cut because it is too large.



Remember that truncation of numbers to significant digits only applies for numbers with infinite representation. In the following example, the number of significant digits is not used, because the operation does not produce numbers with infinite representation:

Arithmetic13 2.png

Ensuring accuracy

Sometime the inverse problem is presented. We have a non-integer number involved in an operation, but we want to use or to produce exact results.

One of the most useful tool for it is rationalization. It is the conversion of a decimal number to its rational form, which is exact and preserves accuracy.

The Rationalize expression

This expression reduces a decimal number to its rational equivalent expression. see the following examples:


Of course, irrational and trascendental numbers cannot be rationalized:


Sometimes, we have a number that has infinitely many decimal digits, but they are periodic, this is, there is a pattern that repeats indefinitely, such as 0.3333333333... It is common to represent the repeating set of digits with a line over them, for example 0.0012636363... = 0.001263

The Rationalize expression has a two-parameter form. The first expression is the number to convert expressed a String, and the second one is the number of repeating digits, at the end of such that String. This number must be less or equal to the number of the fractional part of the number.

Let us test the rationalization of 0.3333333333... = 0.3


Example: rationalize the number 12.34567878787878... = 12.345678


To show that the result is the rational representation of the starting number:


The following shows that the number 0.999999999... = 0.9 is actually the number 1



In Fōrmulæ, you do not have to write parentheses in order to specify the order of operations, they are automatically put when they are needed:


Relational operations

In Fōrmulæ, you can do relational operation with numbers, such as comparisons:


The Compare expression is used to perform a comparison between two numbers.

How is -34 compared against 58 ?


The Max expression finds the maximum among several numbers:


Other operations

There are many other operations you can perform with numbers, see the following examples:

The IsPrime expression returns whether the given numbers is prime or not.


The Digits expression produces an array containing the digits of a number:


You can set the base as the second parameter. The following returns the binary digits of a number:


With the Random expression you can get a (pseudo) random decimal number between 0.0 and 1.0:


Or you can get a (pseudo) random integer number between two integer numbers:


There are operations, such as exponentiation, that can return complex numbers: