Skip to the page
The language / Values and numbers

Start here01

The language02

Programs and the world03

Kiln04

02 The language / 02

Values and numbers

The kinds of value there are, and the arithmetic, rounding and conversions on numbers.

Every value in Hematite has a type, and the type decides what you can do with it. This page starts with the kinds of value there are, then stays with numbers: how to write them, what arithmetic gives, and how to round and convert them. Numbers follow a few firm rules, and each one is there so that a result never surprises you: the type of a sum or a division is known from the code alone, and a mistake stops the program instead of giving a wrong number.

Values

These are all the kinds of value. Most have a page of their own, linked from the table.

TypeExamplesNotes
Int42, -764-bit signed integer; see numbers
Float1.5, -0.2564-bit floating point
Booleantrue, falsethe result of comparisons
String"hello", "Hi, #{name}"text; see strings
Array[1, 2, 3], ["a"]a list of one type, like [Int]; see arrays
Hash{"Ada": 36}, {1: "one"}keys to values, like {String: Int}; see hashes
Objectnew Account(owner: "Ada")an instance of a class; see classes
Enum valueStatus.draftone of an enum's named values; see enums
nilnil"no value"; returned by print. Only Type? variables hold it; see nil and optional types

Numbers

There are two kinds of number. An Int is a whole number, and a Float is a number with a fractional part, written with a dot.

Int
0, 42, 1_000_000. From -9223372036854775807 to 9223372036854775807.
Float
1.5, 0.25, 1.234_5. A 64-bit floating point number.

In a long number, group the digits with _ so it can be read at a glance:

numbers.hem
const count = 42
const big = 9_223_372_036_854_775_807
const ratio = 0.25

An underscore goes between two digits, one at a time. One at the end, two in a row or one before the dot is an error. So is an Int beyond the range above:

invalid-numbers.hem
const x = 1_         # error: trailing '_' in number
const x = 1__000     # error: trailing '_' in number
const x = 1_.5       # error: trailing '_' in number
const x = 9223372036854775808   # error: integer 9223372036854775808 is too large

A Float needs digits on both sides of the dot. A dot straight after a number calls a method, as in 1.abs(), so 1. on its own can't also mean a Float.

Not this

.5 and 1., which are not numbers

Write this

0.5 and 1.0

Arithmetic

The operators are +, -, *, / and % (the remainder). Which type comes out depends only on the operator and the types that go in, never on the values, so you can always tell the type of a result by reading the code.

Division is the one to remember: / always gives a Float, even for two Ints. 7 / 2 is 3.5, and 6 / 2 is 3.0, not 3. When you want a whole number from two Ints, use quotient (below); otherwise, round the result.

The other four give an Int when both sides are Ints. If either side is a Float, the result is a Float: 2 * 1.5 is 3.0.

The remainder, %, takes the sign of the right side: -7 % 3 is 2, and 7 % -3 is -2. With a positive right side the result is never negative, which suits counting that wraps around, such as hours on a clock.

Its other half is quotient, the whole-number division: how many whole times one Int goes into another, rounded down. 7.quotient(2) is 3, and (-7).quotient(2) is -4. The two always fit back together: a.quotient(b) * b + a % b is a. Use it rather than rounding a / b, which goes through a Float and can be wrong for very large numbers. The smallest Int divided by -1 would be too big for an Int, so that's an error.

arithmetic.hem
print(7 / 2)                   # prints 3.5
print(6 / 2)                   # prints 3.0
print(-7 / 2)                  # prints -3.5
print(7 % 3)                   # prints 1
print(-7 % 3)                  # prints 2
print(7.quotient(2))           # prints 3
print((-7).quotient(2))        # prints -4
print(10 / 0)                  # error: division by zero
print(9223372036854775807 + 1) # error: integer overflow

Three things are errors that stop the program, rather than answers that are quietly wrong:

  • dividing by zero, with / or %, for Ints and Floats alike;
  • an Int result outside the 64-bit range;
  • arithmetic with nil.

One number is out of reach of a literal. The smallest Int, -9223372036854775808, can't be written directly, since its digits alone are too large for an Int. Compute it instead: -9223372036854775807 - 1.

Rounding

Since / always gives a Float, getting a whole number back is a step you write out, so the reader sees which way the number goes. A Float has three methods for it:

rounding.hem
print((7 / 2).floor()) # prints 3
print((7 / 2).ceil())  # prints 4
print((7 / 2).round()) # prints 4

const pages = (23 / 10).ceil()

print(pages) # prints 3
MethodRoundsExample
x.floor()down, toward negative infinity(-3.5).floor() is -4
x.ceil()up, toward positive infinity(-3.5).ceil() is -3
x.round()to the nearest; halves go away from zero(2.5).round() is 3, (-2.5).round() is -3

Without an argument, each gives an Int, so the result must fit in one. Give a number of decimals, from 0 to 15, and you get a Float back, rounded to that many places:

rounding-to-decimals.hem
print((3.14159).round(2)) # prints 3.14
print((1.05).floor(1))    # prints 1.0
print((1.15).round(1))    # prints 1.2

These methods exist on Floats only. An Int is already whole, so n.round() on an Int is an error.

Conversions

To convert a value from one type to another, call the name of the type you want with the value:

conversions.hem
print(Float(3))         # prints 3.0
print(Int(3.0))         # prints 3
print(try Int("42"))    # prints 42
print(try Float("1.5")) # prints 1.5
print(String(42) + "!") # prints 42!
WriteAcceptsNotes
Float(x)an Int, or a String like "1.5", "-2"
Int(x)a Float without a fraction, or a String like "42", "-7"Int(2.5) is an error: round first
String(x)anything except a Stringthe text print would print
Bytes(x)a String, as its UTF-8; or an [Int], each 0 to 255Bytes([1, 300]) is an error; Ints not written out make a Bytes! (see Bytes)

Converting a value to the type it already has, such as Float(1.5), is an error.

Text might not be a number at all, and only running the program can tell. So Int(text) and Float(text) can fail: they give an Int! and a Float!, and each call either passes the failure on with try or handles it with rescue (failures explains both). The text must be written like a Hematite number: no spaces, no _, no exponent.

A Float with a fraction is another matter: Int(2.5) is a mistake in the program, not a failure to handle. Round it first, and say which way:

conversion-errors.hem
const x = Float("abc") rescue error
  return 0.0
end

print(x) # prints 0.0

const n = Int("7") # error: Int("7") can fail; write 'try Int("7")' to pass the failure on, or 'Int("7") rescue ...' to handle it

print(Int(2.5)) # error: 2.5 has a fraction; round it first, like (2.5).floor() or (2.5).round()