forked from MeRajat/CPython-Internals
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfloat.md
More file actions
93 lines (59 loc) · 2.28 KB
/
Copy pathfloat.md
File metadata and controls
93 lines (59 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# float
# contents
* [related file](#related-file)
* [memory layout](#memory-layout)
* [example](#example)
* [0](#0)
* [1](#1)
* [0.1](#0.1)
* [1.1](#1.1)
* [-0.1](#-0.1)
* [free_list](#free_list)
# 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](https://en.wikipedia.org/wiki/IEEE_754-1985)/[IEEE-754标准与浮点数运算](https://blog.csdn.net/m0_37972557/article/details/84594879) for more detail

# example
## 0
the binary representation of 0.0 in **IEEE 754** format is 64 zero bits
f = 0.0

## 1.0
f = 1.0

## 0.1
f = 0.1

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

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

# 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**

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

**f3** comes from the head of the **free_list**
>>> f3 = 3.0
>>> id(f3)
4551393616
