Skip to main content

How to Find the Smallest and Largest Value a TensorFlow Data Type Can Hold

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Every TensorFlow data type has limits: uint8 stops at 255, float16 at 65,504. A value outside those limits doesn't fit, and maths that goes past them gives wrong answers. Each data type can tell you its own limits and how many bytes it uses. This guide prints them for five common types in TensorFlow 2.22.0.

Syntax​

Syntax
tf.<data type>.min
tf.<data type>.max
tf.<data type>.size
PartWhat it means
tf.<data type>A data type, such as tf.uint8 or tf.float32, or a tensor's .dtype
.minThe smallest value the type can hold
.maxThe largest value the type can hold
.sizeHow many bytes one value of the type takes

The DType API page describes the first two: max "Returns the maximum representable value in this data type", and min "Returns the minimum representable value in this data type."

The Example​

A loop over five types: uint8, the type of photo pixels; int8 and int32, whole numbers; float16 and float32, decimal numbers. It prints each type's limits and size on one line.

Prerequisites​

  • A uv project with TensorFlow. This guide uses TensorFlow 2.22.0 on Python 3.14.10, with uv 0.13.10.

Step 1: Write the Script​

ranges.py
import tensorflow as tf

for dtype in [tf.uint8, tf.int8, tf.int32, tf.float16, tf.float32]:
print(f"{dtype.name:<8} min {dtype.min}, max {dtype.max}, bytes per value {dtype.size}")

The ranges.py script in a macOS-style code window: a loop over tf.uint8, tf.int8, tf.int32, tf.float16 and tf.float32 that prints each type's name, min, max and size in bytes.

dtype.name:<8 pads each name to 8 characters, so the columns line up.

Step 2: Run It​

Terminal
uv run python ranges.py
Output
uint8 min 0, max 255, bytes per value 1
int8 min -128, max 127, bytes per value 1
int32 min -2147483648, max 2147483647, bytes per value 4
float16 min -65504.0, max 65504.0, bytes per value 2
float32 min -3.4028234663852886e+38, max 3.4028234663852886e+38, bytes per value 4

The terminal after uv run python ranges.py: uint8 from 0 to 255 in 1 byte, int8 from -128 to 127 in 1 byte, int32 from -2147483648 to 2147483647 in 4 bytes, float16 from -65504.0 to 65504.0 in 2 bytes, and float32 from about -3.4e+38 to 3.4e+38 in 4 bytes.

TypeRangeBytesTypical use
uint80 to 2551Photo pixels: one byte per colour
int8-128 to 1271Small whole numbers
int32about ±2.1 billion4Whole numbers, TensorFlow's default for them
float16±65,5042Decimals, when memory matters more than precision
float32about ±3.4 × 10³⁸4Decimals, TensorFlow's default for them

Fewer bytes per value means less memory, and a smaller range. A photo stored as uint8 takes a quarter of the memory it takes as float32. The command works unchanged in PowerShell.

The first run in a new environment is slow, while TensorFlow loads for the first time.

Common Mistakes​

MistakeWhat happensDo this instead
Asking a non-number type for its range: tf.string.maxTypeError: Cannot find maximum value of <dtype: 'string'> with type <dtype: 'string'>. Text has no smallest or largest valueAsk only number types, such as tf.int32 or tf.float32
Doing maths in uint8 near its limit255 + 50 doesn't fit in uint8, and came out as 49Cast to a wider type first, as Fixed: 255 + 50 Gives 49 for a uint8 Tensor in TensorFlow shows

FAQ​

Can I read the range from a tensor?​

Yes, through its data type: photo.dtype.max gives the largest value the tensor's type can hold, not the largest value in the tensor. For that, use tf.reduce_max(photo).

Why are float16 and float32 ranges so different?​

Because of their size: float16 packs a decimal number into 2 bytes, and float32 into 4. More bits allow bigger numbers and more digits, so float32 reaches about 3.4 × 10³⁸ where float16 stops at 65,504.

Conclusion​

.min, .max and .size gave each type's limits and bytes: uint8 holds 0 to 255 in 1 byte, int32 about ±2.1 billion in 4, float16 ±65,504 in 2 and float32 about ±3.4 × 10³⁸ in 4. Text types have no range, and asking for one fails.

Additional Resources​