To the first question: there's no hardware support for float16 on a typical processor (at least outside the GPU). NumPy does exactly what you suggest: convert the float16 operands to float32, perform the scalar operation on the float32 values, then round the float32 result back to float16. It can be proved that the results are still correctly-rounded: the precision of float32 is large enough (relative to that of float16) that double rounding isn't an issue here, at least for the four basic arithmetic operations and square root.

In the current NumPy source, this is what the definition of the four basic arithmetic operations looks like for float16 scalar operations.

#define half_ctype_add(a, b, outp) *(outp) = \
        npy_float_to_half(npy_half_to_float(a) + npy_half_to_float(b))
#define half_ctype_subtract(a, b, outp) *(outp) = \
        npy_float_to_half(npy_half_to_float(a) - npy_half_to_float(b))
#define half_ctype_multiply(a, b, outp) *(outp) = \
        npy_float_to_half(npy_half_to_float(a) * npy_half_to_float(b))
#define half_ctype_divide(a, b, outp) *(outp) = \
        npy_float_to_half(npy_half_to_float(a) / npy_half_to_float(b))

The code above is taken from scalarmath.c.src in the NumPy source. You can also take a look at loops.c.src for the corresponding code for array ufuncs. The supporting npy_half_to_float and npy_float_to_half functions are defined in halffloat.c, along with various other support functions for the float16 type.

For the second question: no, there's no float8 type in NumPy. float16 is a standardized type (described in the IEEE 754 standard), that's already in wide use in some contexts (notably GPUs). There's no IEEE 754 float8 type, and there doesn't appear to be an obvious candidate for a "standard" float8 type. I'd also guess that there just hasn't been that much demand for float8 support in NumPy.

Answer from Mark Dickinson on Stack Overflow
Top answer
1 of 2
40

To the first question: there's no hardware support for float16 on a typical processor (at least outside the GPU). NumPy does exactly what you suggest: convert the float16 operands to float32, perform the scalar operation on the float32 values, then round the float32 result back to float16. It can be proved that the results are still correctly-rounded: the precision of float32 is large enough (relative to that of float16) that double rounding isn't an issue here, at least for the four basic arithmetic operations and square root.

In the current NumPy source, this is what the definition of the four basic arithmetic operations looks like for float16 scalar operations.

#define half_ctype_add(a, b, outp) *(outp) = \
        npy_float_to_half(npy_half_to_float(a) + npy_half_to_float(b))
#define half_ctype_subtract(a, b, outp) *(outp) = \
        npy_float_to_half(npy_half_to_float(a) - npy_half_to_float(b))
#define half_ctype_multiply(a, b, outp) *(outp) = \
        npy_float_to_half(npy_half_to_float(a) * npy_half_to_float(b))
#define half_ctype_divide(a, b, outp) *(outp) = \
        npy_float_to_half(npy_half_to_float(a) / npy_half_to_float(b))

The code above is taken from scalarmath.c.src in the NumPy source. You can also take a look at loops.c.src for the corresponding code for array ufuncs. The supporting npy_half_to_float and npy_float_to_half functions are defined in halffloat.c, along with various other support functions for the float16 type.

For the second question: no, there's no float8 type in NumPy. float16 is a standardized type (described in the IEEE 754 standard), that's already in wide use in some contexts (notably GPUs). There's no IEEE 754 float8 type, and there doesn't appear to be an obvious candidate for a "standard" float8 type. I'd also guess that there just hasn't been that much demand for float8 support in NumPy.

2 of 2
32

This answer builds on the float8 aspect of the question. The accepted answer covers the rest pretty well. One of the main reasons there isn't a widely accepted float8 type, other than a lack of standard is that its not very useful practically.

Primer on Floating Point

In standard notation, a float[n] data type is stored using n bits in memory. That means that at most only 2^n unique values can be represented. In IEEE 754, a handful of these possible values, like nan, aren't even numbers as such. That means all floating point representations (even if you go float256) have gaps in the set of rational numbers that they are able to represent and they round to the nearest value if you try to get a representation for a number in this gap. Generally the higher the n, the smaller these gaps are.

You can see the gap in action if you use the struct package to get the binary representation of some float32 numbers. It's a bit startling to run into at first but there's a gap of 32 just in the integer space:

import struct

billion_as_float32 = struct.pack('f', 1000000000)
for i in range(32):
    billion_as_float32 == struct.pack('f', 1000000001 + i) // True

Generally, floating point is best at tracking only the most significant bits so that if your numbers have the same scale, the important differences are preserved. Floating point standards generally differ only in the way they distribute the available bits between a base and an exponent. For instance, IEEE 754 float32 uses 24 bits for the base and 8 bits for the exponent.

Back to float8

By the above logic, a float8 value can only ever take on 256 distinct values, no matter how clever you are in splitting the bits between base and exponent. Unless you're keen on it rounding numbers to one of 256 arbitrary numbers clustered near zero it's probably more efficient to just track the 256 possibilities in a int8.

For instance, if you wanted to track a very small range with coarse precision you could divide the range you wanted into 256 points and then store which of the 256 points your number was closest to. If you wanted to get really fancy you could have a non-linear distribution of values either clustered at the centre or at the edges depending on what mattered most to you.

The likelihood of anyone else (or even yourself later on) needing this exact scheme is extremely small and most of the time the extra byte or 3 you pay as a penalty for using float16 or float32 instead is too small to make a meaningful difference. Hence...almost no one bothers to write up a float8 implementation.

Discussions

Define a custom float8 in python-numpy and convert from/to float16? - Stack Overflow
I am trying to define a custom 8 bit floating point format as follows: 1 sign bit 2 bits for mantissa 5 bits for exponent Is it possible to define this as a numpy datatype? If not, what is the e... More on stackoverflow.com
🌐 stackoverflow.com
numpy.float() returns non-numpy python builtin float
This is different to behavior of dtype options in array initializa... More on github.com
🌐 github.com
5
November 1, 2013
ENH: <support datatype FP8>
Proposed new feature or change: Hi : Do you have any plan to support FP8 which proposed in https://arxiv.org/pdf/2209.05433 ? More on github.com
🌐 github.com
2
May 9, 2024
[Week 1] np.float vs float
regarding Exercise 2 for this function : conv_single_step() I converted the last value Z to float the value is correct but the datatype is worng which cause the test to fail. what do I miss? More on community.deeplearning.ai
🌐 community.deeplearning.ai
1
0
August 26, 2022
🌐
NumPy
numpy.org › doc › 1.22 › user › basics.types.html
Data types — NumPy v1.22 Manual
NumPy supports a much greater variety of numerical types than Python does. This section shows which are available, and how to modify an array’s data-type · The primitive types supported are tied closely to those in C:
🌐
Crumb
crumb.sh › 3ERVuUd3iaD
NumPy float types: a demonstration of precision - Python 3 code example - crumb.sh
The different NumPy float types allow us to store floats in different precision, dependent on the number of bits we allow the float to use. The larger the number of allowed bits, the more precision our array’s elements will have.
Top answer
1 of 1
5

I'm by no means an expert in numpy, but I like to think about FP representation problems. The size of your array is not huge, so any reasonably efficient method should be fine. It doesn't look like there's an 8 bit FP representation, I guess because the precision isn't so good.

To convert to an array of bytes, each containing a single 8 bit FP value, for a single dimensional array, all you need is

float16 = np.array([6.3, 2.557])           # Here's some data in an array
float8s = array.tobytes()[1::2]
print(float8s)
>>> b'FAAF'

This just takes the high-order bytes from the 16 bit float by lopping off the low order part, giving a 1 bit sign, 5 bit exponent and 2 bit significand. The high order byte is always the second byte of each pair on a little-endian machine. I've tried it on a 2D array and it works the same. This truncates. Rounding in decimal would be a whole other can of worms.

Getting back to 16 bits would be just inserting zeros. I found this method by experiment and there is undoubtedly a better way, but this reads the byte array as 8 bit integers and writes a new one as 16 bit integers and then converts it back to an array of floats. Note the big-endian representation converting back to bytes as we want the 8 bit values to be the high order bytes of the integers.

float16 = np.frombuffer(np.array(np.frombuffer(float8s, dtype='u1'), dtype='>u2').tobytes(), dtype='f2')
print(float16)
>>> array([6. , 2.5, 2.5, 6. ], dtype=float16)

You can definitely see the loss of precision! I hope this helps. If this is sufficient, let me know. If not, I'd be up for looking deeper into it.

🌐
NumPy
numpy.org › doc › 2.1 › reference › arrays.scalars.html
Scalars — NumPy v2.1 Manual
Python defines only one type of a particular data class (there is only one integer type, one floating-point type, etc.). This can be convenient in applications that don’t need to be concerned with all the ways data can be represented in a computer. For scientific computing, however, more ...
🌐
Omz Software
omz-software.com › pythonista › numpy › user › basics.types.html
Data types — NumPy v1.8 Manual
Numpy supports a much greater variety of numerical types than Python does. This section shows which are available, and how to modify an array’s data-type · Additionally to intc the platform dependent C integer types short, long, longlong and their unsigned versions are defined
🌐
GitHub
github.com › numpy › numpy › issues › 3998
numpy.float() returns non-numpy python builtin float · Issue #3998 · numpy/numpy
November 1, 2013 - >>> print type(np.arange(2, dtype=np.float)[0]) <type 'numpy.float64'> >>> print type(np.arange(2, dtype=np.int)[0]) <type 'numpy.int64'>
Author   megies
Find elsewhere
🌐
GitHub
github.com › numpy › numpy › issues › 26405
ENH: <support datatype FP8> · Issue #26405 · numpy/numpy
May 9, 2024 - Proposed new feature or change: Hi : Do you have any plan to support FP8 which proposed in https://arxiv.org/pdf/2209.05433 ?
Author   xiguadong
🌐
w3resource
w3resource.com › numpy › data-types.php
NumPy: Data types - w3resource
>> import numpy as np >>> c = np.arange(5, dtype=np.uint8) >>> (c.astype(float)) array([ 0., 1., 2., 3., 4.]) >>> (np.int8(c)) array([0, 1, 2, 3, 4], dtype=int8)
🌐
NumPy
numpy.org › doc › stable › reference › arrays.scalars.html
Scalars — NumPy v2.4 Manual
Python defines only one type of a particular data class (there is only one integer type, one floating-point type, etc.). This can be convenient in applications that don’t need to be concerned with all the ways data can be represented in a computer. For scientific computing, however, more ...
🌐
NumPy
numpy.org › doc › 2.2 › reference › arrays.scalars.html
Scalars — NumPy v2.2 Manual
Python defines only one type of a particular data class (there is only one integer type, one floating-point type, etc.). This can be convenient in applications that don’t need to be concerned with all the ways data can be represented in a computer. For scientific computing, however, more ...
🌐
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.finfo.html
numpy.finfo — NumPy v2.1 Manual
Machine limits for floating point types · Kind of floating point or complex floating point data-type about which to get information
🌐
GeeksforGeeks
geeksforgeeks.org › python › using-numpy-to-convert-array-elements-to-float-type
Using NumPy to Convert Array Elements to Float Type - GeeksforGeeks
July 15, 2025 - This approach reuses the astype() method like before, but here you overwrite the original array with the converted one. It’s a form of in-place reassignment, not true in-place conversion which NumPy doesn’t support due to fixed data types .
🌐
Google Groups
groups.google.com › g › cython-users › c › yubkr1nySA8
Question about numpy.float16 support in cython
July 24, 2021 - It looks like https://github.com/numpy/numpy/blob/623bc1fae1d47df24e7f1e29321d0c0ba2771ce0/numpy/core/include/numpy/halffloat.h defines a few operations on the type, and for everything else you convert it to/from float32 when you need to use it. The advantage is then in space rather than processing time. See https://stackoverflow.com/questions/38975770/python-numpy-float16-datatype-operations-and-float8/40507235
🌐
Medium
medium.com › @amit25173 › different-ways-to-convert-numpy-float-to-int-f47f3be42453
Different Ways to Convert NumPy Float to Int | by Amit Yadav | Medium
April 12, 2025 - The easiest and most common way to convert floats to integers in NumPy is by using .astype(int).
🌐
SciPy
docs.scipy.org › doc › numpy-1.6.0 › user › basics.types.html
Data types — NumPy v1.6 Manual (DRAFT)
Numpy supports a much greater variety of numerical types than Python does. This section shows which are available, and how to modify an array’s data-type · Numpy numerical types are instances of dtype (data-type) objects, each having unique characteristics. Once you have imported NumPy using
🌐
DeepLearning.AI
community.deeplearning.ai › course q&a › deep learning specialization › convolutional neural networks
[Week 1] np.float vs float - Convolutional Neural Networks - DeepLearning.AI
August 26, 2022 - regarding Exercise 2 for this function : conv_single_step() I converted the last value Z to float the value is correct but the datatype is worng which cause the test to fail. what do I miss?
🌐
GitHub
github.com › numpy › numpy › issues › 25836
BUG: Weird conversion behavior from np.float32 to Python float · Issue #25836 · numpy/numpy
February 16, 2024 - Describe the issue: I found out that converting np.float32 to a Python float via .item() gives a weird result. While I understand NumPy retains the float32 internal representation of the value, I feel that for such a simple value the fol...
Author   fandreuz