Numbers
Emerald has two kinds of number. An Int is a whole number, such as 42 or -7. A Float is a
number that can have a fractional part, such as 3.14 or 0.5. A number with a decimal point is a
Float; one without is an Int.
const players = 4 # an Intconst price = 2.50 # a Floatprint(players, price)4 2.5Use an Int for things you count, and a Float for things you measure.
Arithmetic
Section titled “Arithmetic”The usual operators work on both kinds:
print(7 + 2) # addprint(7 - 2) # subtractprint(7 * 2) # multiplyprint(2 ** 10) # raise to a power95141024Three kinds of division
Section titled “Three kinds of division”Dividing whole numbers raises a question: what is 7 divided by 2? Emerald gives you a way to ask for each answer.
/always gives the exact answer, as aFloat, even when both numbers are whole.//divides and rounds down to a whole number.%gives the remainder left over after//.
print(7 / 2)print(8 / 2)print(7 // 2)print(7 % 2)3.54.0318 / 2 is 4.0, not 4: / always gives a Float, and a whole Float always keeps its .0
so you can tell it from an Int.
% is handier than it looks. n % 2 is 0 exactly when n is even, and minutes % 60 is what
is left after taking out the whole hours.
Mixing the two kinds
Section titled “Mixing the two kinds”When an Int meets a Float, the answer is a Float:
print(2 + 0.5)print(3 * 1.5)2.54.5Wherever a Float is expected, an Int is accepted and becomes one, so var price: Float = 3
holds 3.0. The other way round needs a decision about the fraction, so Emerald makes you say
which you want:
const x = 3.7print(x.round()) # nearest whole numberprint(x.floor()) # round downprint(x.ceil()) # round upprint(x.truncate()) # drop the fraction4343The Float page lists every way to round and convert.
The order of operations
Section titled “The order of operations”Emerald follows the order you learned in math: ** first, then *, /, //, and %, then +
and -. Parentheses go first of all.
print(2 + 3 * 4)print((2 + 3) * 4)print(10 - 4 - 3)14203Operators of the same kind work left to right, so 10 - 4 - 3 is (10 - 4) - 3. The exception is
**, which works right to left, as it does in math: 2 ** 3 ** 2 is 2 ** 9, which is 512.
A minus sign written against a number is part of the number, so -3.abs() is 3. With **, the
power still comes first: -2 ** 2 is -(2 ** 2), which is -4. When in doubt, add parentheses;
they cost nothing and make the meaning plain.
Writing numbers
Section titled “Writing numbers”- A long number can use underscores to group its digits:
1_000_000is one million. - Very large or very small
Floats can be written in scientific notation:6.02e23is 6.02 × 10²³. - Numbers are written in decimal. Emerald doesn’t have hexadecimal (
0xFF), octal, or binary literals, and says so if you try one.
At the edges
Section titled “At the edges”Dividing by zero has no answer, so it is an error rather than a strange result:
print(5 // 0)numbers.em:1:7: floor division by zero print(5 // 0) ^^^^^^Check the divisor before dividing. Division by zero has no result for either numeric type.An Int holds whole numbers up to about nine quintillion (9,223,372,036,854,775,807). An answer
too big to fit is an error, never a wrong number:
print(9223372036854775807 + 1)numbers.em:1:7: addition of 9223372036854775807 and 1 overflows Int print(9223372036854775807 + 1) ^^^^^^^^^^^^^^^^^^^^^^^`Int` holds whole numbers from -9223372036854775808 through 9223372036854775807.A Float is stored approximately, as in every programming language, so some simple-looking sums
come out slightly off:
print(0.1 + 0.2)0.30000000000000004This isn’t a bug in Emerald; it is how fractions are stored in a computer’s binary arithmetic.
Round a Float when you show it, with round_to or format, and don’t compare two calculated
Floats for exact equality. For money, count in cents with an Int.
What you’ve learned
Section titled “What you’ve learned”- An
Intis a whole number; aFloatcan have a fraction. A decimal point makes aFloat. +,-,*, and**do what you’d expect;/always gives aFloat,//rounds down, and%gives the remainder.- An
Intand aFloattogether give aFloat; turning aFloatinto anIntmeans choosing how to round. - Operations follow math’s order, and parentheses make it plain.
- Dividing by zero and overflowing an
Intare errors;Floatarithmetic is approximate.
The built-in Int and Float pages list everything numbers can do. Next, text: strings, and the many things they can do.