Mojo Numbers
Mojo has two families of numbers: integers (whole numbers like 42 or -7) and floating-point numbers (decimals like 3.14). Each family comes in several sizes. This page shows which one to use, how to write numbers in your code, and a few surprises with decimals that catch everyone at least once.
Int: the default whole number
Section titled “Int: the default whole number”When you write a whole number without a type, you get an Int:
def main(): var players = 4 var temperature = -12 var population = 8_100_000_000 print(players, temperature, population)Output:
4 -12 8100000000Int is the type you’ll use for counting, looping and indexing, and it’s what the standard library expects in most places. It uses your machine’s native size, which is 64 bits on today’s 64-bit computers. So it holds numbers from about minus 9.2 quintillion to plus 9.2 quintillion.
The underscores in 8_100_000_000 are just for your eyes: Mojo ignores them, like Python does. Use them to group digits in big numbers.
Sized and unsigned integers
Section titled “Sized and unsigned integers”Sometimes you need an exact size, for example to match a file format, store pixels, or save memory in a big list. Mojo has integer types with the size in the name:
| Signed (can be negative) | Unsigned (0 and up) | Bits |
|---|---|---|
Int8 |
UInt8 (also called Byte) |
8 |
Int16 |
UInt16 |
16 |
Int32 |
UInt32 |
32 |
Int64 |
UInt64 |
64 |
Int128 |
UInt128 |
128 |
Int256 |
UInt256 |
256 |
Int |
UInt |
Machine size (64 on most computers) |
You pick one with a type annotation, and every type tells you its range with .MIN and .MAX:
def main(): var red: UInt8 = 255 var offset: Int16 = -300 var file_size: UInt64 = 4_000_000_000 print(red, offset, file_size) print("Int16 range:", Int16.MIN, "to", Int16.MAX) print("UInt64 max:", UInt64.MAX)Output:
255 -300 4000000000Int16 range: -32768 to 32767UInt64 max: 18446744073709551615Signed types use one bit for the sign, so they cover negatives and positives. Unsigned types (the U ones) can’t be negative, and use that bit to reach twice as high. Int8 goes from -128 to 127; UInt8 goes from 0 to 255.
Writing integer literals
Section titled “Writing integer literals”Besides plain decimal numbers, you can write integers in hexadecimal, octal and binary, the same way as in Python:
def main(): print(0xFF) print(0o17) print(0b1010) print(1_000_000)Output:
25515101000000| Prefix | Base | Example | Value |
|---|---|---|---|
0x |
16 (hex) | 0xFF |
255 |
0o |
8 (octal) | 0o17 |
15 |
0b |
2 (binary) | 0b1010 |
10 |
Hex shows up a lot with colors (0xFF8800) and bit flags. One thing that doesn’t work: leading zeros on a normal number. var code = 007 is a syntax error, just like in Python 3.
Float64 and Float32: decimal numbers
Section titled “Float64 and Float32: decimal numbers”A number with a decimal point is a floating-point number. Without a type, it’s a Float64:
def main(): var price = 19.99 var tiny = 2.5e-4 var huge = 6.02e23 print(price, tiny, huge)Output:
19.99 0.00025 6.02e+232.5e-4 is scientific notation: 2.5 times 10 to the power of -4. Mojo has no plain Float type. The two you’ll see most are:
| Type | Bits | Precision |
|---|---|---|
Float64 |
64 | About 15 to 16 significant digits. The default. |
Float32 |
32 | About 7 significant digits. Half the memory, common on GPUs. |
The difference shows up when a number has many digits:
def main(): var exact: Float64 = 123456789.0 var rough: Float32 = 123456789.0 print(exact) print(rough)Output:
123456789.0123456790.0Float32 can’t store all nine digits, so it keeps the closest value it can. For everyday math, stick with Float64. (There’s also Float16, BFloat16 and some 8-bit formats, but those are for machine learning and need special hardware for good speed.)
Decimals are close, not exact
Section titled “Decimals are close, not exact”Floating-point numbers are stored in binary, and most decimals (like 0.1) don’t have an exact binary form. So tiny rounding errors creep in. This is true in Python, JavaScript, C and every other language that uses standard floats:
def main(): var a = 0.1 var b = 0.2 var total = a + b print(total) print(total == 0.3)Output:
0.30000000000000004FalseBecause of this, don’t compare decimals with ==. Check that they’re close enough with isclose from the std.math module:
from std.math import isclose
def main(): var a = 0.1 var b = 0.2 var total = a + b print(isclose(total, 0.3))Output:
TrueHandy number functions
Section titled “Handy number functions”A few useful functions are always available, and more live in std.math:
from std.math import sqrt, floor, ceil
def main(): var x = 7.6 print(abs(-12), min(4, 9), max(4, 9)) print(round(x), floor(x), ceil(x)) print(round(19.987, 2)) print(sqrt(16.0)) print(2 ** 10)Output:
12 4 98.0 7.0 8.019.994.01024abs,min,max,roundandpoware built in (no import).round(x, 2)rounds to 2 decimal places. On a float,round,floorandceilgive back a float (8.0), not anInt.sqrt,floor,ceil,iscloseand many others come fromstd.math.**raises to a power:2 ** 10is1024.
Coming from Python?
Section titled “Coming from Python?”| Python | Mojo | |
|---|---|---|
| Default whole number | int, unlimited size |
Int, 64-bit on most machines |
| Default decimal | float (64-bit) |
Float64 |
| Fixed-size types | Only via NumPy (np.int8…) |
Built in: Int8, UInt8, Float32… |
0x, 0o, 0b, _ in literals |
Yes | Yes, same syntax |
0.1 + 0.2 on variables |
0.30000000000000004 |
Same |
Practice
Section titled “Practice”1. Pixel values
Section titled “1. Pixel values”A pixel has a red, green and blue part, each from 0 to 255. Store 255, 136 and 0 in three UInt8 variables and print them. Then print the biggest value a UInt8 can hold.
Hint: use var red: UInt8 = 255 and UInt8.MAX.
Show solution
def main(): var red: UInt8 = 255 var green: UInt8 = 136 var blue: UInt8 = 0 print(red, green, blue) print("max:", UInt8.MAX)Output:
255 136 0max: 255UInt8 is a good fit here: one byte per value, and the range 0 to 255 matches exactly.
2. Circle area
Section titled “2. Circle area”Compute the area of a circle with radius 2.5 (area is 3.14159 * radius * radius) and print it rounded to 2 decimal places.
Hint: round(value, 2).
Show solution
def main(): var radius = 2.5 var area = 3.14159 * radius * radius print(round(area, 2))Output:
19.63All the values are Float64 here, so the math just works. round(area, 2) keeps two decimal places.
3. Hex to decimal
Section titled “3. Hex to decimal”Print the decimal values of the hex numbers 0x10, 0x7F and 0xFFFF.
Hint: just print the literals; Mojo shows integers in decimal.
Show solution
def main(): print(0x10, 0x7F, 0xFFFF)Output:
16 127 65535The prefix only changes how you write the number. Once it’s in your program, it’s an ordinary Int.
- Whole numbers default to
Int(machine size, 64-bit on most computers); decimals default toFloat64. - Sized types (
Int8toInt256,UInt8toUInt256) have a fixed number of bits and a range you can read with.MINand.MAX. - Write numbers with
_separators and0x,0o,0bprefixes, like in Python. Float32keeps about 7 digits,Float64about 15 to 16.- Decimal math has tiny rounding errors: compare with
isclose, not==. abs,min,max,roundare built in;sqrt,floor,ceilcome fromstd.math.
Next up: strings, how Mojo stores text and why len() doesn’t work on it.
Quick check
Section titled “Quick check”Quick check
Read more
Section titled “Read more”- Numeric types reference: every integer and float type,
SIMD, literals and the sharp edges, from the official docs. - Mojo manual: Types: the numeric types section explains integers, floats and literals step by step.
- Literals reference: the exact syntax for integer and float literals.
std.mathmodule:sqrt,floor,ceil,iscloseand the rest of the math functions.