1.2.9

Numbers

Three types of numbers are available in Archetype: integers, naturals and rationals

Naturals

Naturals are positive integers. They are defined as follows:

1

effect {

2

var n1 : nat = 345;

3

var n2 : nat = 9999999999999999999999999999999999999999999999999999999;

4

var n3 : nat = 10_000_000;

5

}

Copied!

Note that it is possible to structure large numbers by packs of 3 digits using the underscore character.

3 arithmetics operations *difference* operator returns an *integer* value (see integers section below).

`+ * %`

and the 6 comparison operators `= <> < > <= >= `

are available. Note however that the 1

effect {

2

var a : nat = 5;

3

var b : nat = 7;

4

var c : int = a - b; /* -2 typed as intger */

5

var d : int = b - a; /* 2 typed as integer */

6

var e : nat = sub_nat(a,b); /* fails with "NegResult" */

7

var f : nat = sub_nat(b,a); /* f = 2 */

8

}

Copied!

Note that the *euclidean* division and returns a nat value.

`div`

operator is the The

`abs`

function is used to convert a integer value to a natural value.1

effect {

2

var a = 5i; /* a is typed 'int'*/

3

var b = abs(5); /* b is typed 'nat' */

4

}

Copied!

Integers

Integers are defined as follows:

1

effect {

2

var n1 : int = 3_458i;

3

var n2 : int = -5i;

4

var n3 : int = 99999999999999999999999999999999999999999999999999999999i;

5

var n4 : int = 10_000_000i;

6

}

Copied!

Note that the literal syntax of positive integers uses the

`i`

suffix.Integer values are *big integers, *meaning there is no real constraint on the value and it can be negative.* *

The 5 arithmetics operations

`+ * - div %`

and the 6 comparison operators `= <> < > <= >= `

are available.Tezis

1

effect {

2

var t1 := 1tz; // one tezis

3

var t2 := 1mtz: // one 0.001 tezis

4

var t3 := 1utz; // one mutez

5

}

Copied!

It is more constrained than integers since you can only add and subtract *tez* values. All comparison operators are available.

Rationals

A rational is the quotient or fraction of two integers. You can define a rational with :

- a quotient of two integers
- a floating point notation
- a percentage notation

The following presents an example for each:

1

effect {

2

var r1 := 6/4;

3

var r2 := 1.5;

4

var r3 := 150%;

5

}

Copied!

`r1`

`r2`

and `r3`

represent the same rational, $3/2$

. These values are transcoded to the pair of integers `(3,2)`

. A fractions is simplified during transcoding process. Operations

Archetype provides the 4 arithmetic operations

`+ - * /`

and the minus sign on rationals. They all return a rational. For example:1

effect {

2

var r1 := 8 / 6; // transcoded to (4,3)

3

var r2 := 1.8; // transcoded to (9,5)

4

var rpl := r1 + r2; // will execute to (6,15)

5

var rmi := r1 - r2; // will execute to (-7,15)

6

var rmu := r1 * r2; // will execute to (4,5)

7

var rdi := r1 / r2: // will execute to (20,27)

8

var rms := -r1 // will execute to (-4,3)

9

}

Copied!

It is also possible to mix integers and rationals:

1

effect {

2

var r := 5 / 3;

3

var n := 4;

4

var rtn := r * n; // will execute to (20/3)

5

var rpn := r + n; // will execute to (17/3)

6

}

Copied!

It is also possible to mix rationals and *tezies* values, in that order. However, the result is a value in *tezies*.

1

effect {

2

var r := 80%;

3

var a := 56tz;

4

var res := r * a;

5

}

Copied!

In the example above, the

`res`

value is `44800000utz`

. The process is the following:- 1.convert a to
*utez*(smallest unit) - 2.compute the rational (here 4 * 56000000 / 5 = 224000000 / 5)
- 3.execute the euclidean division (44800000)

Note that the term

`a * r`

is not accepted as `res`

value (line 4 above): rationals come first.Comparison

Rationals are comparable with

`= <> < <= > >=`

operators.1

effect {

2

var r1 := 3/12;

3

var r2 := 0.25;

4

var r3 := 0.5;

5

if r1 = r2 and r3 > r2 then transfer 1tz to coder;

6

}

Copied!

It is possible to compare rationals and integers. It is not possible to compare rationals and *tez* values.

Conversion to integers

Rational are converted to integers with the

`floor`

and `ceil`

operators with the expected behaviour.Conversion to other types

There is no explicit cast (conversion operator) from a rational to a tez value, a duration or a date. You may just multiply by 1tez for example.

1

effect {

2

var a := 2.5;

3

transfer (a * 1tz) from source to dest;

4

}

Copied!

Last modified 2mo ago

Export as PDF

Copy link

Edit on GitHub