forked from zpoint/CPython-Internals
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfloat_cn.md
More file actions
111 lines (71 loc) · 2.29 KB
/
Copy pathfloat_cn.md
File metadata and controls
111 lines (71 loc) · 2.29 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# float
# 目录
* [相关位置文件](#相关位置文件)
* [内存构造](#内存构造)
* [示例](#示例)
* [0](#0)
* [1](#1)
* [0.1](#0.1)
* [1.1](#1.1)
* [-0.1](#-0.1)
* [free_list](#free_list)
# 相关位置文件
* cpython/Objects/floatobject.c
* cpython/Include/floatobject.h
* cpython/Objects/clinic/floatobject.c.h
# 内存构造
**PyFloatObject** 仅仅是一层对 c 语言中双精度浮点数的包装(**double**), 一个双精度浮点数使用8个字节去表示一个浮点数
详细的内容可以参考 [IEEE 754](https://en.wikipedia.org/wiki/IEEE_754-1985)/[IEEE-754标准与浮点数运算](https://blog.csdn.net/m0_37972557/article/details/84594879)

# 示例
## 0
0.0 使用 **IEEE 754** 标准的表示方式为 64 个为 0 的 bit
```python3
f = 0.0
```

## 1.0
```python3
f = 1.0
```

## 0.1
```python3
f = 0.1
```

## 1.1
1.1 和 0.1 的区别是指数位最后的几个位不相同

## -0.1
-0.1 和 0.1 的区别是第一个符号位不相同

# free_list
```c
#ifndef PyFloat_MAXFREELIST
#define PyFloat_MAXFREELIST 100
#endif
static int numfree = 0;
static PyFloatObject *free_list = NULL;
```
free_list 是一个单链表, 最多存储 **PyFloat_MAXFREELIST** 个 **PyFloatObject**

单链表通过 **ob_type** 字段串联起来
```python3
>>> f = 0.0
>>> id(f)
4551393664
>>> f2 = 1.0
>>> id(f2)
4551393616
del f
del f2
```

**f3** 取自 **free_list** 的表头
```python3
>>> f3 = 3.0
>>> id(f3)
4551393616
```
