Janet 1.10.1-9cfc3d9 Documentation
Numbers and Arithmetic
Any programming language will have some way to do arithmetic. Janet is no exception, and supports the basic arithmetic operators.
# Prints 13 # (1 + (2*2) + (10/5) + 3 + 4 + (5 - 6)) (print (+ 1 (* 2 2) (/ 10 5) 3 4 (- 5 6)))
Just like the
returns the remainder of division. For example,
(% 10 3) is 1, and
(% 10.5 3) is 1.5. The lines that begin with
# are comments.
All Janet numbers are IEEE 754 double precision floating point numbers. They can be used to represent both integers and real numbers to a finite precision.
Numeric literals can be written in many ways. Numbers can be written in base 10,
with underscores used to separate digits into groups. A decimal point can be
used for floating point numbers. Numbers can also be written in other bases by
prefixing the number with the desired base and the character 'r'. For example,
16 can be written as
0x prefix can be used for hexadecimal as it is so
common. The radix must be themselves written in base 10, and can be any integer
from 2 to 36. For any radix above 10, use the letters as digits (not case
Numbers can also be in scientific notation such as
3e10. A custom radix
can be used for scientific notation numbers (the exponent will share the radix).
For numbers in scientific notation with a radix other than 10, use the
symbol to indicate the exponent rather than
Some example numbers:
0 +0.0 -10_000 16r1234abcd 0x23.23 1e10 1.6e-4 7r343_111_266.6&+10 # a base 7 number in scientific notation. # evaluates to 1.72625e+13 in base 10
Janet will allow some pretty wacky formats for numbers. However, no matter what format you write your number in, the resulting value will always be a double precision floating point number.
Besides the 5 main arithmetic functions, Janet also supports a number of math
functions taken from the C library
<math.h>, as well as bit-wise
operators that behave like they do in C or Java. Functions like
math/exp will behave as expected to
a C programmer. They all take either 1 or 2 numeric arguments and return a real
number (never an integer!). Bit-wise functions are all prefixed with
brushift. Bit-wise functions only work on integers.
See the Math API for information on functions in the