prelude: numbers
Arithmetic on int and float, powers, bitwise functions on ints, and reading numbers from text
The number contexts, Add(a) and Num(a), the arithmetic attached to int and float that the operators read through them, powers, bitwise functions on ints, and reading numbers from text. They are in the prelude, so no import is needed. nanyx/math has more functions on numbers.
Converting between numbers
Arithmetic doesn’t mix int and float: n * 1.5 is a type error when n is an int. int.toFloat turns an int into a float, and never fails:
def n = 3
def x = int.toFloat(n) * 1.5 -- 4.5
def half = (7 \toFloat) / 2.0 -- 3.5
To turn a float back into an int, choose how to drop the fraction with floor, ceil, round or truncate from nanyx/math, or with float.toInt. Conversions to and from the sized number types are on the conversions page.
Bits
Nanyx has no operators for bits, so they are functions attached to int: int.bitAnd, int.bitOr, int.bitXor and int.bitNot, and the shifts int.shiftLeft, int.shiftRight, which keeps the sign, and int.shiftRightUnsigned, which fills with zeros:
def flags = int.bitOr(1, 4) -- 5
def hasFour = int.bitAnd(flags, 4) != 0 -- true
def kilo = 1 \int.shiftLeft(10) -- 1024
Reference
int.fromString
def int.fromString: string -> #some(int) | #invalidInt
The integer s writes in decimal, with an optional leading - or +, or #invalidInt for
anything else (an empty string, a sign alone, spaces, other characters, or a number past the range
of int).
int.toFloat
def int.toFloat: int -> float
n as a float: int.toFloat(3) is 3.0. Every int has an exact float. To go back, see
floor, ceil, round and truncate in nanyx/math.
Add
context Add(a) = (
zero: a
`+`: a, a -> a
)
The values of type a that add. x + y is read through it, and so is 0 beside a value of
type a in a function whose type requires it: def total: [Add(a)] list(a) -> a. A type gets
one by attaching the two members, def Money.zero = ... and def Money.`+` = ....
Sub
context Sub(a) = (
`-`: a, a -> a
negate: a -> a
)
The values of type a that subtract. x - y and -x are read through it. A type gets one by
attaching the two members, def Money.`-` = ... and def Money.negate = ....
Num
context Num(a) = (
zero: a
`+`: a, a -> a
`-`: a, a -> a
negate: a -> a
`*`: a, a -> a
`/`: a, a -> a
`%`: a, a -> a
fromInt: int -> a
isInteger: bool
)
The numbers of type a: Add’s and Sub’s members, and x * y, x / y and x % y. In a
function whose type requires it, any other integer literal beside a value of type a is fromInt
of it, so def square: [Num(a)] a -> a = { x -> x * x } and { x -> x * 2 } work at int and
float. A function that requires Num(a) can call one that requires Add(a) or Sub(a): a
Num has every member they have. On an integer type / truncates and % takes the sign of x;
on float they are IEEE 754’s. isInteger says which a is: true for an integer type, so
x ** -1 stops the program on one.
int.zero
def int.zero: int
The int that adds nothing.
int.`+`
def int.`+`: int, int -> int
x plus y.
int.`-`
def int.`-`: int, int -> int
x minus y.
int.negate
def int.negate: int -> int
n with its sign changed.
int.`*`
def int.`*`: int, int -> int
x times y.
int.`/`
def int.`/`: int, int -> int
x divided by y, truncated towards zero: 7 / 2 is 3 and -7 / 2 is -3.
int.`%`
def int.`%`: int, int -> int
The remainder of x divided by y, with the sign of x: -7 % 2 is -1.
int.fromInt
def int.fromInt: int -> int
n itself.
int.isInteger
def int.isInteger: bool
true: an int is an integer type.
int.bitAnd
def int.bitAnd: int, int -> int
The bits set in both x and y: int.bitAnd(12, 10) is 8.
int.bitOr
def int.bitOr: int, int -> int
The bits set in x, in y or in both: int.bitOr(12, 10) is 14.
int.bitXor
def int.bitXor: int, int -> int
The bits set in one of x and y but not both: int.bitXor(12, 10) is 6.
int.bitNot
def int.bitNot: int -> int
n with each of its bits flipped, which is -n - 1: int.bitNot(0) is -1.
int.shiftLeft
def int.shiftLeft: int, int -> int
x with its bits moved count places towards the high end, zeros coming in at the low end: 1 \int.shiftLeft(4)
is 16. Only the low six bits of count are read, so a count is taken modulo 64.
int.shiftRight
def int.shiftRight: int, int -> int
x with its bits moved count places towards the low end, copies of the sign bit coming in at the high end, so
that the sign is kept: int.shiftRight(-8, 1) is -4. Only the low six bits of count are read, so a count is
taken modulo 64.
int.shiftRightUnsigned
def int.shiftRightUnsigned: int, int -> int
x with its bits moved count places towards the low end, zeros coming in at the high end, as if x had no
sign: int.shiftRightUnsigned(-1, 60) is 15. Only the low six bits of count are read, so a count is taken
modulo 64.
float.zero
def float.zero: float
The float that adds nothing.
float.`+`
def float.`+`: float, float -> float
x plus y.
float.`-`
def float.`-`: float, float -> float
x minus y.
float.negate
def float.negate: float -> float
x with its sign changed: float.negate(0.0) is negative zero.
float.`*`
def float.`*`: float, float -> float
x times y.
float.`/`
def float.`/`: float, float -> float
x divided by y.
float.`%`
def float.`%`: float, float -> float
The remainder of x divided by y, truncated as C’s fmod is: 7.5 % 2.0 is 1.5.
float.fromInt
def float.fromInt: int -> float
n as a float, as int.toFloat gives it.
float.isInteger
def float.isInteger: bool
false: a float is not an integer type.
pow
def pow: [Num(a)] a, int -> a
base ** exponent, as a function that can be passed around.
float.pow
def float.pow: float, float -> float
x to the power y, which need not be a whole number: float.pow(2.0, 0.5) is the square root
of 2. A whole y is x ** y, exactly; any other is e ** (y * ln(x)), so a negative x gives
NaN.
float.fromString
def float.fromString: string -> #some(float) | #invalidFloat
The float s writes in decimal, or #invalidFloat for anything else (an empty string, a sign
alone, spaces, other characters, "1.2.3"). It reads an optional leading - or +, digits with
an optional . among them ("3", "0.25", ".5" and "5." are all read), and an optional
exponent, e or E then an integer: "1.0e-7". NaN, Infinity and -Infinity are read as
float.toString writes them. A number too large for a float is infinity.
The result is the nearest float to the decimal when it has at most 15 significant digits and an exponent, once the point is moved to the end, between -22 and 22, which covers most text written by hand. Otherwise it is not correctly rounded and may be a unit in the last place out.