Skip to content

Latest commit

 

History

History
111 lines (71 loc) · 2.38 KB

float.md

File metadata and controls

111 lines (71 loc) · 2.38 KB

floatimage title

contents

related file

  • cpython/Objects/floatobject.c
  • cpython/Include/floatobject.h
  • cpython/Objects/clinic/floatobject.c.h

memory layout

PyFloatObject is no more than a wrapper of c type double, which takes 8 bytes to represent a floating point number

you can refer to IEEE 754/IEEE-754标准与浮点数运算 for more detail

layout

example

0

the binary representation of 0.0 in IEEE 754 format is 64 zero bits

f = 0.0

0

1.0

f = 1.0

1

0.1

f = 0.1

0.1

1.1

the difference between 1.1 and 0.1 is the last few exponent bits

1.1

-0.1

the difference between -0.1 and 0.1 is the first sign bit

-0.1

free_list

#ifndef PyFloat_MAXFREELIST
#define PyFloat_MAXFREELIST    100
#endif
static int numfree = 0;
static PyFloatObject *free_list = NULL;

free_list is a single linked list, store at most PyFloat_MAXFREELIST PyFloatObject

free_list

The linked list is linked via the field ob_type

>>> f = 0.0
>>> id(f)
4551393664
>>> f2 = 1.0
>>> id(f2)
4551393616
del f
del f2

free_list2

f3 comes from the head of the free_list

>>> f3 = 3.0
>>> id(f3)
4551393616

free_list3