Skip to main content

Numbers

An unusual fact about Raven's numbers: they're defined in the standard library, in Raven itself, right down to the bit widths. The tower goes:

  • Integer – fixed-width integers: Int64 (the default, written 1500), Int32, and unsigned UInt64/UInt32/UInt16/UInt8. Underneath these is Bits[N], raw bit vectors of any width – Char is a UInt[21], for example. Bool is here too.
  • Real – the above plus floats: Float64 (the default, written 3.141) and Float32. Inf, NaN and pi are available.
  • Number – the above plus Complex and BigInt.

Integer, Real and Number are traits, so your own numeric types can join the tower.

Literals

x = 1500 # Int64
y = 3.141 # Float64
big = 1_000_000 # underscores as separators
mask = 0xCAFE # hexadecimal

Conversions and promotion

Type names double as conversion functions:

Float64(1) # 1.0
Int32(7)
UInt8(255)

Mixed-type arithmetic promotes to the wider type – 1 + 2.5 is 3.5. The promote and coerce functions expose this machinery for your own types.

Big integers

big converts to an arbitrary-precision BigInt (backed by JS bigints):

> factorial(big(50))
big(30414093201713378043612608166064768844377641568960512000000000000)

Complex numbers

> Complex(1, 2) / 2
0.5 + 1.0im

real and imag extract the parts. The complex number implementation is a nice compact example of Raven style – bundles, dispatch and traits in about a hundred lines.

Functions

The usual suspects are available: abs, abs2, min, max, div, rem, round, zero, one, factorial, plus a full set of scientific functions – sqrt, cbrt, hypot, exp, log, log2, log10, pow, and the trigonometric and hyperbolic families (sin, cos, tan, asinh, ...).

For integer types, typemin, typemax and bitsize describe the representation, and bits gets at the raw bit pattern.

Predicates end in ?: zero?(x), integer?(x).