Properties of a Python float can be requested via sys.float_info. It returns information such as max/min value, max/min exp value, etc. These properties can potentially be used to calculate the byte size of a float. I never encountered anything else than 64 bit, though, on many different architectures.

The items of a NumPy array might have different size, but you can check their size in bytes by a.itemsize, where a is a NumPy array.

Answer from Sven Marnach on Stack Overflow
🌐
Reddit
reddit.com › r/learnpython › confusion about the size of floats in python
r/learnpython on Reddit: Confusion about the size of floats in python
June 8, 2015 -

Hi, not sure if this belongs here or in the main python sub, but decided to try here first. Anyways, I'm working on a project where I have a large text file which essentially contains a roughly 22,000x6,000 matrix of decimal values. This file is >2GB in size, however it's all encoded as text so the size of the data should become considerably smaller once I read them in and represent them as floats (they are mostly a single-digit integer part with 14 digit fractional part, along with the decimal point itself, each character of which UTF-8 can represent as a single byte since it's in the ascii range so each value should use 16 bytes if my calculations are correct). I figure that python represents floats as 8-byte double precision values, as the documentation says CPython will use a C double for your platform to represent them. My googling seems to support this assumption. That would mean that my original matrix in the file should be about 16220006000 ~= 2 GB (not including the whitespace separators), and the matrix in memory as floats should be 8220006000 ~= 1 GB, which while not small, should fit comfortably in memory on my laptop. However, this is not the case and I'm getting an out of memory error as a result.

As a result, I did some investigation and did >>> x = 3.14 >>> sys.getsizeof(x) 24 What the hell, how can this be??? What could CPython possibly be doing with 24 bytes? All I can figure is that there's an 8-byte pointer to a 16-byte double (huge!) and getsizeof() is counting both? Note that this is 64-bit CPython running on linux. I tried again on a 32-bit CPython running on windows and the answer I got was 16 bytes, so I figure this means a 4-byte pointer to an 8-byte double? Somewhat frustrated with this result, I tried again, this time using numpy.float16 because these are relatively small values and I don't need all that space anyways. For the linux 64-bit CPython I got 24 bytes again, and on the Windows 32-bit CPython I got 12 bytes. No idea at all what's happening here because if my previous guesses were correct I should have 8 and 4 byte pointers to 2-byte float16s, giving 10 and 6 bytes, respectively. Clearly I'm missing something here. Can anyone explain what's happening to me, and why so much memory is being used to store these values?

Also, I'm aware that my use case is memory intensive and I'm working on refactoring so that I don't need to have everything in memory at once. However, I'm still extremely curious about this behavior as it seems strange to me. Thanks!

Top answer
1 of 3
5
In python, everything is an object. That means that getsizeof doesn't just include the raw numerical data, or even a pointer to the raw numerical data, it includes the size of the entire object, including the numerical data, methods, etc. It is what is called an "opaque data type", which means that the exact internal representation is not visible to the user. I am not sure what you mean when you say you are using a "matrix". Python has no built-in multidimensional data structures. Do you mean you are using a numpy matrix or array? If so, the floats in a numpy array are not the same as python floats. In the case of a numpy array, they are true IEEE floats. The array as a whole is an object, but it stores the float data as raw floats in consecutive (or rather strided) regions of memory. So the overhead will only be present once, for the entire array, rather than once for each element of the array (as is the case for Python lists). This means that looking at what happens with an individual python float is not generalizable to a numpy float array. Anyway, the proper data structure for what you are doing is a Pandas DataFrame. You can store this as an HDF5 file so that you don't have to read the whole thing into memory at once. You can also use Dask or Blaze to do true, transparent out-of-core processing.
2 of 3
3
In CPython, every object begins with a reference count (of type ssize_t, i.e. a machine word) and a pointer to the type object for that object, which is necessary to be able to do any operations (e.g. invoke a method) on the object. That's two machine words right there off the bat, before you even start to talk about the data contained in the object. After that header comes a variable amount of data; in the case of a float object, it's just a C double value directly, so another eight bytes. For a 32 bit CPython, that's 4 + 4 + 8 = 16 bytes, for a 64 bit CPython, that's 8 + 8 + 8 = 24 bytes. Note that CPython can also be configured for debug builds, which adds two more pointers to that common header shared by all objects, which are used to form a doubly linked list of all objects on the heap, which can be used to track down reference counting bugs. Modules like Numpy can store raw values without these object headers, but that representation is completely hidden from you. Anything that you can actually access has to be an object. So while the values inside that Numpy array may only occupy 2 bytes each, the moment that you access any such value, it's converted (boxed) into a Python object that has the same header as any other object in Python. It's impossible not to see the contribution of that header using sys.getsizeof(), because you can't not pass a Python object to that function. Moreover, these structures are subject to alignment requirements. At the very least, each member of the structure must have its natural alignment (e.g. a member that's 4 bytes must be aligned to the nearest 4 byte boundary, a member that's 8 bytes must be aligned to the nearest 8 byte boundary, and so on.) That also means that the overall size of the structure must be padded to accommodate the member with the highest alignment, so for example if a struct contains an 8 byte pointer, then the size of the struct must be a multiple of 8; extra padding will be inserted at the end to make that happen. If that wasn't the case, then you couldn't pack structures back to back and still satisfy the alignment requirements of each individual member. Since the Python object header contains two machine words, that means its size must be a multiple of the native word size. In your example of a numpy.float16 object, on a 64 bit system, the layout would be 8 + 8 + 2 + 6 bytes padding = 24. And on a 32 bit system, 4 + 4 + 2 + 2 bytes padding = 12 bytes. But again, this only applies when you actually access these values as Python objects, but the whole point of Numpy is to avoid doing that. When you declare an ndarray in Numpy, all the values are packed without Python object headers, and are accessed by C in their raw form. If you multiply a Numpy array by a scalar for instance, that's all done in C without any boxing or unboxing. But if you write e.g. my_numpy_float12_array[42] then Numpy has to extract that value from its compact 2 byte representation and box in a Python object, resulting in a 24 byte Python object on a 64 bit system. It's impossible to access that 2 byte value directly, because you can only access Python objects. But as long as you use Numpy properly, that's not a problem, because you're rarely accessing individual elements like that. Again, the whole point of Numpy is to avoid doing as much as possible in Python and do everything in C, i.e. you should never manually loop over values, you should use whatever vector construct represents the operation you're trying to perform.
Discussions

getting size of primitive data types in python - Stack Overflow
If you just want to know the size of a single float, the easiest way is to simply instantiate some arbitrary float. For example: ... This returns 24 bytes in your case (and probably for most other people as well). In the case of CPython (the most common Python implementation), every float object ... More on stackoverflow.com
🌐 stackoverflow.com
March 16, 2018
How does Python manage size of integers and floats? - Stack Overflow
Why does 1.5 which is a float takes smaller size than an integer 1. ... Save this answer. ... Show activity on this post. I'll talk just about ints for now, and look at floats at the end. In Python, unlike C and many other languages, int is not just a datatype storing a number. More on stackoverflow.com
🌐 stackoverflow.com
Python get the size of float number - Stack Overflow
I need to detect the actual size of int and float numbers, to verify, if it would fit into a char/short/int/long/longlong or float/double/... types of other languages like c, Java, etc. For int ty... More on stackoverflow.com
🌐 stackoverflow.com
To find size of int, float, double and char of my System in python - Stack Overflow
I am new to python and i am trying to find size of int, float, double and char of my System. Do we have any syntax to find them? More on stackoverflow.com
🌐 stackoverflow.com
🌐
EyeHunts
tutorial.eyehunts.com › home › python float size
Python float size - Tutorial - By EyeHunts
July 6, 2023 - However, in most Python implementations, including CPython (the reference implementation), the size of a float object is typically 24 bytes.
🌐
IncludeHelp
includehelp.com › python › how-to-check-the-size-of-a-float.aspx
Python - How to check the size of a float?
All the properties of a Python float can be requested via sys.float_info(). It returns information such as max/min value, max/min exp value, etc. However, a much easier approach to find the information about the float size is to use numpy.finfo() which is powerful enough to tell us about the properties of float or any other data type including the number of bits in the exponents, the number of bits in the mantissa, etc.
Top answer
1 of 5
15

Running

sys.getsizeof(float)

does not return the size of any individual float, it returns the size of the float class. That class contains a lot more data than just any single float, so the returned size will also be much bigger.

If you just want to know the size of a single float, the easiest way is to simply instantiate some arbitrary float. For example:

sys.getsizeof(float())

Note that

float()

simply returns 0.0, so this is actually equivalent to:

sys.getsizeof(0.0)

This returns 24 bytes in your case (and probably for most other people as well). In the case of CPython (the most common Python implementation), every float object will contain a reference counter and a pointer to the type (a pointer to the float class), which will each be 8 bytes for 64bit CPython or 4 bytes each for 32bit CPython. The remaining bytes (24 - 8 - 8 = 8 in your case which is very likely to be 64bit CPython) will be the bytes used for the actual float value itself.

This is not guaranteed to work out the same way for other Python implementations though. The language reference says:

These represent machine-level double precision floating point numbers. You are at the mercy of the underlying machine architecture (and C or Java implementation) for the accepted range and handling of overflow. Python does not support single-precision floating point numbers; the savings in processor and memory usage that are usually the reason for using these are dwarfed by the overhead of using objects in Python, so there is no reason to complicate the language with two kinds of floating point numbers.

and I'm not aware of any runtime methods to accurately tell you the number of bytes used. However, note that the quote above from the language reference does say that Python only supports double precision floats, so in most cases (depending on how critical it is for you to always be 100% right) it should be comparable to double precision in C.

2 of 5
8
import ctypes
ctypes.sizeof(ctypes.c_double)
🌐
GeeksforGeeks
geeksforgeeks.org › python-float-type-and-its-methods
Float type and its methods in python - GeeksforGeeks
February 21, 2025 - Floats support decimal and exponential notation. They occupy 8 bytes (64 bits) in memory.
🌐
Note.nkmk.me
note.nkmk.me › home › python
Maximum and Minimum float Values in Python | note.nkmk.me
August 11, 2023 - In Python, the float type is a 64-bit double-precision floating-point number, equivalent to double in languages like C. This article explains how to get and check the range (maximum and minimum values ...
Find elsewhere
🌐
Sololearn
sololearn.com › en › Discuss › 2671032 › maximum-binary-size-of-float-in-python
Maximum "binary" size of float in python
January 20, 2021 - Sololearn is the world's largest community of people learning to code. With over 25 programming courses, choose from thousands of topics to learn how to code, brush up your programming knowledge, upskill your technical ability, or stay informed about the latest trends.
🌐
Execute Program
executeprogram.com › courses › python-for-programmers › lessons › size-and-precision-of-numbers
Python for Programmers: Size and Precision of Numbers
That costs us some performance, but it frees us from having to ask "will this integer get too big?" In Python, no integer is too big! However, Python's floating point numbers do have an upper limit. (Like most programming languages, Python uses IEEE 754 double-precision floating point numbers.
🌐
Python Tutorial
pythontutorial.net › home › advanced python › python float
An Essential Guide to Python float Type By Examples
September 10, 2022 - To see how Python represents the 0.1 internally, you can use the format() function. The following shows how Python represents the number 0.1 using 20 digits: >>> format(0.1, '.20f') '0.10000000000000000555'Copy · As you can see, 0.1 is not exactly 0.1 but 0.10000000000000000555...
🌐
sebhastian
sebhastian.com › python-length-of-integer-float
How to get the length of integers or floats in Python | sebhastian
March 30, 2023 - To count the length of float numbers in Python, you can use the str() and len() functions as shown below:
🌐
Readthedocs
bitstring.readthedocs.io › en › stable › exotic_floats.html
Exotic Floating Point Formats — bitstring 4.3 documentation
Python floats are typically 64 bits long, but 32 and 16 bit sizes are also supported through the struct module. These are the well-known IEEE formats.
Top answer
1 of 1
5

I'll talk just about ints for now, and look at floats at the end.

In Python, unlike C and many other languages, int is not just a datatype storing a number. It is a full object, with extra information about the object (more detail here).

This extra information takes up lots of space, which is why the objects can seem unusually large. Specifically, it takes up 3 lots of 8 bytes (3*8=24 bytes). These are a reference count, a type and a size.

Python stores integers using a specific number of bytes depending on how large they are. Specifically:

0 <= n < 2**0:
    requires 24 bytes
2**0 <= n < 2**30:
    requires 28 bytes
2**30 <= n < 2**60:
    requires 32 bytes

In general, for every increase of 30 powers of 2 (for want of better terminology!), 4 extra bytes are required.

This pattern also follows for the negative numbers, just going the opposite direction.

These specific values are the values on my computer, but will vary depending on the environment you're running Python on. However, the general patterns are likely to be the same.

I believe (as explained a little here) that the reason zero alone uses less space is that the only int which can be represented using just 24 bytes is zero, so there is no need to additionally store the actual value, reducing the size. It's possible I've misunderstood this specific case so please correct me below if so!

However, floats are stored differently. Their values are simply stored using 64 bits (i.e. a double), which means there are 8 bytes representing the value. Since this never varies, there is no need to store the size as we do with integers, meaning there are only two 8 byte values to store alongside the specific float value. This means the total size is two lots of 8 bytes for the object data and one lot of 8 bytes for the actual value, or 24 bytes in total.

It is this property of not needing to store the value's size that frees up 8 bytes, which means 1.5 requires less space than 1.

🌐
W3Resource
w3resource.com › python-interview › what-is-the-maximum-and-minimum-value-for-an-int-float-and-string-data-type-in-python.php
Exploring data type limits in Python
Python's float data type has 15-17 decimal digits of precision. ... In Python, a string's length is limited by the system's memory. Since strings are immutable, the maximum size of a string is determined by the total memory available and the ...
🌐
Stack Overflow
stackoverflow.com › questions › 74203847 › python-get-the-size-of-float-number
Python get the size of float number - Stack Overflow
I need to detect the actual size of int and float numbers, to verify, if it would fit into a char/short/int/long/longlong or float/double/... types of other languages like c, Java, etc. For int ty...
🌐
Simplilearn
simplilearn.com › home › resources › software development › python float() function: key concepts [with examples]
Python float() Function: Key Concepts [With Examples]
November 27, 2024 - Learn how the float() function in Python converts a specified value into a floating point number. Syntax: float(value). A key function for numeric conversions.
Address: 5851 Legacy Circle, 6th Floor, Plano, TX 75024 United States
🌐
DEV Community
dev.to › mike-vincent › quarks-outlines-python-floating-point-numbers-3jpb
Quark’s Outlines: Python Floating Point Numbers - DEV Community
August 9, 2025 - These examples show how Python helps you solve problems with floating point values. You are writing a program that tracks height in meters. You want to store 1.75, not 1 or 2.
🌐
NumPy
numpy.org › doc › 1.22 › user › basics.types.html
Data types — NumPy v1.22 Manual
This should be taken into account when interfacing with low-level code (such as C or Fortran) where the raw memory is addressed. Data-types can be used as functions to convert python numbers to array scalars (see the array scalar section for an explanation), python sequences of numbers to arrays of that type, or as arguments to the dtype keyword that many numpy functions or methods accept. Some examples: >>> import numpy as np >>> x = np.float32(1.0) >>> x 1.0 >>> y = np.int_([1,2,4]) >>> y array([1, 2, 4]) >>> z = np.arange(3, dtype=np.uint8) >>> z array([0, 1, 2], dtype=uint8)
🌐
Real Python
realpython.com › ref › builtin-types › float
float | Python’s Built-in Data Types – Real Python
The built-in float data type represents floating-point numbers, which are numbers with a decimal point.