Numbers

Sometimes coercion is transparent. Perl 6 has several numeric types which can intermix freely--such as subtracting a floating point value from an integer, as 123 - 12.1e1.

The most important types are:

Int

Int objects store integer numbers of arbitrary size. If you write a literal that consists only of digits, such as 12, it is an Int.

Num

Num is the floating point type. It stores sign, mantissa, and exponent, each with a fixed width. Calculations involving Num numbers are usually quite fast, though subject to limited precision.

Numbers in scientific notation such as 6.022e23 are of type Num.

Rat

Rat, short for rational, stores fractional numbers without loss of precision. It does so by tracking its numerator and denominator as integers, so mathematical operations on Rats with large components can become quite slow. For this reason, rationals with large denominators automatically degrade to Num.

Writing a fractional value with a dot as the decimal separator, such as 3.14, produces a Rat.

Complex

Complex numbers have two parts: a real part and an imaginary part. If either part is NaN, then the entire number may possibly be NaN.

Numbers in the form a + bi, where bi is the imaginary component, are of type Complex.

The following operators are available for all number types:

Таблица 11.1. Binary numeric operators

OperatorDescription
**Exponentiation: $a**$b is $a to the power of $b
*multiplication
/division
divinteger division
+addition
-subtraction

Таблица 11.2. Unary numeric operators

OperatorDescription
+conversion to number
-negation

Most mathematical functions are available both as methods and functions, so you can write both (-5).abs and abs(-5).

Таблица 11.3. Mathematical functions and methods

MethodDescription
absabsolute value
sqrtsquare root
lognatural logarithm
log10logarithm to base 10
ceilrounding up to an integer
floorrounding down to an integer
roundrounding to next integer
sign-1 for negative, 0 for zero, 1 for positive values

The trigonometric functions sin, cos, tan, asin, acos, atan, sec, cosec, cotan, asec, acosec, acotan, sinh, cosh, tanh, asinh, acosh, atanh, sech, cosech, cotanh, asech, acosech and acotanh work in units of radians by default. You may specify the unit with an argument of Degrees, Gradians or Circles. For example, 180.sin(Degrees) is approximately 0.