Skip to content

Arithmetic

Bill Hails edited this page Aug 13, 2024 · 20 revisions
1 + 1; // 2

Standard operators are

op name
+ addition
- subtraction and unary negation
* multiplication
/ division
% modulus
** exponentiation

F♮ supports the following numeric types:

  • arbitrary sized integers - ints are promoted individually to bigints if an arithmetic operation overflows.
  • rationals - precision-preserving rational numbers - division of integers where the result is not an integer produces a rational, and arithmetic on rationals produces a rational or an integer: 2 + 1/2 is 5/2, 1/2 + 1/2 is 1 etc.
  • irrationals - floating point numbers.
  • complex numbers - -1 ** (1/2) results in 1i, complex numbers can be input as i.e. (1 + 2i).

The standard formats for literal floating point and integer decimals are recognised, hexadecimal integers are prefixed by 0x or 0X, additionally imaginary numbers (decimal, hex or float) have the suffix i (no space) and you can use underscores _ within any sequence of digits to improve readability. It's probably worth pointing out that you have to write 1i to get i, i by itself is not treated specially (I may change this).

Coercion is as follows, somewhat complicated by complex numbers which can have integer, float or rational components.

  • int $\sqsubset$ rational $\sqsubset$ float $\sqsubset$ complex

But to be clear as far as type-checking is concerned, there is only the single type number.

Plans are afoot to support trig and log functions over the above, and there are already some built-in functions to help with complex number manipulation, see Builtins.

Next: Booleans

Clone this wiki locally