Correct way to emulate single precision floating point in python?

Viewed 35775

What's the best way to emulate single-precision floating point in python? (Or other floating point formats for that matter?) Just use ctypes?

6 Answers

If numpy (the excellent suggestion of other answers) is inapplicable for you (e.g. because you're in an environment that doesn't allow arbitrary third-party extensions), the array module in Python standard library is fine too -- type code 'f' gives you 32-bit floats. Besides those and the (usual) double precision floats, there isn't much for "other floating point formats" -- what did you have in mind? (e.g. gmpy offers GMP's modest support for floats with much longer, arbitrary bit sizes -- but it's modest indeed, e.g., no trig functions).

how about ctypes.c_float from standard library?

If your application suits arrays/matrices, you can use numpy with float32

To expand a little on the ctypes option [1]:

>>> import ctypes
>>> ctypes.sizeof(ctypes.c_int)                                                                                                                                                        
4
>>> ctypes.sizeof(ctypes.c_long)                                                                                                                                                       
8

>>> ctypes.sizeof(ctypes.c_float)                                                                                                                                                      
4
>>> ctypes.sizeof(ctypes.c_double)                                                                                                                                                     
8

With numpy [2], e.g.:

>>> import numpy as np
>>> np.zeros((1,1), dtype='uint8').nbytes                                                                                                                                              
1
>>> np.zeros((1,1), dtype='uint16').nbytes                                                                                                                                             
2
>>> np.zeros((1,1), dtype='uint64').nbytes                                                                                                                                             
8
>>> np.zeros((1,1), dtype='float').nbytes  # watch out for this one
8
>>> np.zeros((1,1), dtype='float32').nbytes                                                                                                                                            
4
>>> np.zeros((1,1), dtype='float64').nbytes                                                                                                                                            
8
>>> np.zeros((1,1), dtype='single').nbytes                                                                                                                                             
4
>>> np.zeros((1,1), dtype='double').nbytes                                                                                                                                             
8

numpy.astype does conversions, e.g.

>>> np.zeros((1,1), dtype='double').astype('single').nbytes                                                                                                                            
4

[1] https://docs.python.org/3/library/ctypes.html#fundamental-data-types

[2] https://docs.scipy.org/doc/numpy-1.15.1/reference/arrays.dtypes.html

Related