Skip to content

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.

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 8100000000

Int 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.

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 4000000000
Int16 range: -32768 to 32767
UInt64 max: 18446744073709551615

Signed 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.

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:

255
15
10
1000000
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.

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+23

2.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.0
123456790.0

Float32 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.)

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.30000000000000004
False

Because 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:

True

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 9
8.0 7.0 8.0
19.99
4.0
1024
  • abs, min, max, round and pow are built in (no import).
  • round(x, 2) rounds to 2 decimal places. On a float, round, floor and ceil give back a float (8.0), not an Int.
  • sqrt, floor, ceil, isclose and many others come from std.math.
  • ** raises to a power: 2 ** 10 is 1024.
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

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 0
max: 255

UInt8 is a good fit here: one byte per value, and the range 0 to 255 matches exactly.

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.63

All the values are Float64 here, so the math just works. round(area, 2) keeps two decimal places.

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 65535

The 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 to Float64.
  • Sized types (Int8 to Int256, UInt8 to UInt256) have a fixed number of bits and a range you can read with .MIN and .MAX.
  • Write numbers with _ separators and 0x, 0o, 0b prefixes, like in Python.
  • Float32 keeps about 7 digits, Float64 about 15 to 16.
  • Decimal math has tiny rounding errors: compare with isclose, not ==.
  • abs, min, max, round are built in; sqrt, floor, ceil come from std.math.

Next up: strings, how Mojo stores text and why len() doesn’t work on it.

Quick check

1. What type does var n = 42 have?
2. Which type can hold the value -200?
3. With var a = 0.1 and var b = 0.2, what does print(a + b == 0.3) print?
4. What does round(2.5) give in Mojo?