Janet 1.38.0-73334f3 Documentation
(Other Versions:
1.37.1
1.36.0
1.35.0
1.34.0
1.31.0
1.29.1
1.28.0
1.27.0
1.26.0
1.25.1
1.24.0
1.23.0
1.22.0
1.21.0
1.20.0
1.19.0
1.18.1
1.17.1
1.16.1
1.15.0
1.13.1
1.12.2
1.11.1
1.10.1
1.9.1
1.8.1
1.7.0
1.6.0
1.5.1
1.5.0
1.4.0
1.3.1
)
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 print
function, all arithmetic operators are entered in
prefix notation. Janet also supports the remainder operator, or %
, which
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
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 16
, 1_6
, 16r10
, 4r100
, or
0x10
. The 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
sensitive).
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 e
.
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.
Arithmetic functions
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/sin
,
math/cos
, math/log
, and 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 b
.
They are bnot
, bor
, bxor
, band
, blshift
,
brshift
, and brushift
. Bit-wise functions only work on integers.
See the Math API for information on functions in the
math/
namespace.