45

I want to check whether a float is actually 32 or 64bits (and the number of bits of a numpy float array). There should be a built-in, but just didn't find out...

Sven Marnach
  • 574,206
  • 118
  • 941
  • 841
Hailiang Zhang
  • 17,604
  • 23
  • 71
  • 117

5 Answers5

34

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.

James Mchugh
  • 994
  • 8
  • 26
Sven Marnach
  • 574,206
  • 118
  • 941
  • 841
  • 40
    sys.float_info contains many interesting properties but size of a float is not one of them – panda-34 Feb 28 '16 at 11:48
  • @panda-34 It depends what exactly you mean by "size". In any case, that's the best info you can get. (You can easily recover the size in bits from the information in `sys.float_info`, but I wonder how that would ever be useful.) – Sven Marnach Mar 01 '16 at 15:23
  • 7
    OP plainly asked about the size in bits, 32 or 64 and you cannot recover the size in bits from float_info unless you want to do some wild guessing based on exponent range and mantissa length – panda-34 Mar 01 '16 at 19:43
  • 16
    "*The size of a Python float can be requested via `sys.float_info.`*" vs. " *The size in bits simply isn't exposed directly by Python*". It's not your fault that Python doesn't expose the size in bits, but it's your fault that you're pretending to give a factual answer when really you're just waving your hand. – Andras Deak -- Слава Україні May 19 '17 at 17:21
  • 2
    Considering that an arbitrary float format might have parity bits, padding, or other weirdness not visible from `sys.float_info`, you can't determine the size of a float from there. It's straightforward enough if you're assuming the only options are IEEE754 binary32 and binary64, but if you're assuming that much, you might as well assume binary64 and not check at all. Of course, the in-memory size of the underlying C doubles doesn't mean that much when Python floats have so much object overhead attached, and NumPy dtypes take exactly the space they say they do (when packed in an array). – user2357112 May 19 '17 at 17:29
17

numpy.finfo lists sizes and other attributes of float32 ..., including
nexp : number of bits in the exponent including its sign and bias.
nmant : number of bits in the mantissa.
On a machine with IEEE-754 standard floating point,

import numpy as np
for f in (np.float32, np.float64, float):
    finfo = np.finfo(f)
    print finfo.dtype, finfo.nexp, finfo.nmant

will print e.g.

float32 8 23
float64 11 52
float64 11 52

(Try float16 and float128 too.)

denis
  • 21,378
  • 10
  • 65
  • 88
  • plus one bit for sign – denfromufa Aug 31 '15 at 14:31
  • float16 Half precision float: sign bit, 5 bits exponent, 10 bits mantissa; float32 Single precision float: sign bit, 8 bits exponent, 23 bits mantissa; float64 Double precision float: sign bit, 11 bits exponent, 52 bits mantissa – denfromufa Aug 31 '15 at 14:36
9
print numpy.finfo(numpy.float)
Machine parameters for float64
---------------------------------------------------------------------
precision= 15   resolution= 1.0000000000000001e-15
machep=   -52   eps=        2.2204460492503131e-16
negep =   -53   epsneg=     1.1102230246251565e-16   
minexp= -1022   tiny=       2.2250738585072014e-308
maxexp=  1024   max=        1.7976931348623157e+308
nexp  =    11   min=        -max
---------------------------------------------------------------------
gggg
  • 2,284
  • 1
  • 17
  • 19
8

The range of floating point values is available in the sys.float_info object.

As Sven says, for CPython float is always 64-bit. But Python's language reference says

You are at the mercy of the underlying machine architecture (and C or Java implementation) for the accepted range ...".

So this is not necessarily the case for other Python implementations.

3

python -c "import sys,math; print(sys.float_info.mant_dig + math.ceil(math.log2(sys.float_info.max_10_exp - sys.float_info.min_10_exp)) + 1)"

"+1" because one digit in the mantissa is stored implicitly.

Konstantin Burlachenko
  • 5,233
  • 2
  • 41
  • 40