Skip to content

Commit 42f9483

Browse files
committed
float done
1 parent 4a85575 commit 42f9483

7 files changed

Lines changed: 128 additions & 6 deletions

File tree

BasicObject/float/float.md

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* [0.1](#0.1)
1111
* [1.1](#1.1)
1212
* [-0.1](#-0.1)
13-
* [summary](#summary)
13+
* [free_list](#free_list)
1414

1515
#### related file
1616
* cpython/Objects/floatobject.c
@@ -29,7 +29,7 @@ you can refer to [IEEE 754](https://en.wikipedia.org/wiki/IEEE_754-1985)/[IEEE-7
2929

3030
##### 0
3131

32-
the binary representation of 0.0 in **IEEE 754** format is all bit in zero
32+
the binary representation of 0.0 in **IEEE 754** format is 64 zero bits
3333

3434
f = 0.0
3535

@@ -59,6 +59,35 @@ the difference between -0.1 and 0.1 is the first sign bit
5959

6060
![-0.1](https://github.com/zpoint/Cpython-Internals/blob/master/BasicObject/float/-0.1.png)
6161

62-
##### nonascii characters
62+
##### free_list
6363

64-
s = "我是帅哥".encode("utf8")
64+
#ifndef PyFloat_MAXFREELIST
65+
#define PyFloat_MAXFREELIST 100
66+
#endif
67+
static int numfree = 0;
68+
static PyFloatObject *free_list = NULL;
69+
70+
free_list is a single linked list, store at most PyFloat_MAXFREELIST **PyFloatObject**
71+
72+
![free_list](https://github.com/zpoint/Cpython-Internals/blob/master/BasicObject/float/free_list.png)
73+
74+
The linked list in linked via the field **ob_type**
75+
76+
>>> f = 0.0
77+
>>> id(f)
78+
4551393664
79+
>>> f2 = 1.0
80+
>>> id(f2)
81+
4551393616
82+
del f
83+
del f2
84+
85+
![free_list2](https://github.com/zpoint/Cpython-Internals/blob/master/BasicObject/float/free_list2.png)
86+
87+
**f3** comes from the head of the **free_list**
88+
89+
>>> f3 = 3.0
90+
>>> id(f3)
91+
4551393616
92+
93+
![free_list3](https://github.com/zpoint/Cpython-Internals/blob/master/BasicObject/float/free_list3.png)

BasicObject/float/float_cn.md

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# float
2+
3+
### 目录
4+
5+
* [相关位置文件](#相关位置文件)
6+
* [内存构造](#内存构造)
7+
* [示例](#示例)
8+
* [0](#0)
9+
* [1](#1)
10+
* [0.1](#0.1)
11+
* [1.1](#1.1)
12+
* [-0.1](#-0.1)
13+
* [free_list](#free_list)
14+
15+
#### 相关位置文件
16+
* cpython/Objects/floatobject.c
17+
* cpython/Include/floatobject.h
18+
* cpython/Objects/clinic/floatobject.c.h
19+
20+
#### 内存构造
21+
22+
**PyFloatObject** 仅仅是一层对 c 语言中双精度浮点数的包装(**double**), 一个双精度浮点数使用8个字节去表示一个浮点数
23+
24+
详细的内容可以参考 [IEEE 754](https://en.wikipedia.org/wiki/IEEE_754-1985)/[IEEE-754标准与浮点数运算](https://blog.csdn.net/m0_37972557/article/details/84594879)
25+
26+
![layout](https://github.com/zpoint/Cpython-Internals/blob/master/BasicObject/float/layout.png)
27+
28+
#### 示例
29+
30+
##### 0
31+
32+
0.0 使用 **IEEE 754** 标准的表示方式为 64 个为 0 的 bit
33+
34+
f = 0.0
35+
36+
![0](https://github.com/zpoint/Cpython-Internals/blob/master/BasicObject/float/0.png)
37+
38+
##### 1.0
39+
40+
f = 1.0
41+
42+
![1](https://github.com/zpoint/Cpython-Internals/blob/master/BasicObject/float/1.png)
43+
44+
##### 0.1
45+
46+
f = 0.1
47+
48+
![0.1](https://github.com/zpoint/Cpython-Internals/blob/master/BasicObject/float/0.1.png)
49+
50+
##### 1.1
51+
52+
1.1 和 0.1 的区别是指数位最后的几个位不相同
53+
54+
![1.1](https://github.com/zpoint/Cpython-Internals/blob/master/BasicObject/float/1.1.png)
55+
56+
##### -0.1
57+
58+
-0.1 和 0.1 的区别是第一个符号位不相同
59+
60+
![-0.1](https://github.com/zpoint/Cpython-Internals/blob/master/BasicObject/float/-0.1.png)
61+
62+
##### free_list
63+
64+
#ifndef PyFloat_MAXFREELIST
65+
#define PyFloat_MAXFREELIST 100
66+
#endif
67+
static int numfree = 0;
68+
static PyFloatObject *free_list = NULL;
69+
70+
free_list 是一个单链表, 最多存储 **PyFloat_MAXFREELIST****PyFloatObject**
71+
72+
![free_list](https://github.com/zpoint/Cpython-Internals/blob/master/BasicObject/float/free_list.png)
73+
74+
单链表通过 **ob_type** 字段串联起来
75+
76+
>>> f = 0.0
77+
>>> id(f)
78+
4551393664
79+
>>> f2 = 1.0
80+
>>> id(f2)
81+
4551393616
82+
del f
83+
del f2
84+
85+
![free_list2](https://github.com/zpoint/Cpython-Internals/blob/master/BasicObject/float/free_list2.png)
86+
87+
**f3** 取自 **free_list** 的表头
88+
89+
>>> f3 = 3.0
90+
>>> id(f3)
91+
4551393616
92+
93+
![free_list3](https://github.com/zpoint/Cpython-Internals/blob/master/BasicObject/float/free_list3.png)

BasicObject/float/free_list.png

22.4 KB
Loading

BasicObject/float/free_list2.png

177 KB
Loading

BasicObject/float/free_list3.png

197 KB
Loading

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Trying to illustrate every detail of cpython implementation
2323
- [x] [long/int](https://github.com/zpoint/Cpython-Internals/blob/master/BasicObject/long/long.md)
2424
- [x] [bytes](https://github.com/zpoint/Cpython-Internals/blob/master/BasicObject/bytes/bytes.md)
2525
- [x] [bytearray](https://github.com/zpoint/Cpython-Internals/blob/master/BasicObject/bytearray/bytearray.md)
26-
- [ ] [float](https://github.com/zpoint/Cpython-Internals/blob/master/BasicObject/float/float.md)
26+
- [x] [float](https://github.com/zpoint/Cpython-Internals/blob/master/BasicObject/float/float.md)
2727
- [ ] func
2828
- [ ] method
2929
- [ ] iter

README_CN.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
- [x] [long/int](https://github.com/zpoint/Cpython-Internals/blob/master/BasicObject/long/long_cn.md)
2525
- [x] [bytes](https://github.com/zpoint/Cpython-Internals/blob/master/BasicObject/bytes/bytes_cn.md)
2626
- [x] [bytearray](https://github.com/zpoint/Cpython-Internals/blob/master/BasicObject/bytearray/bytearray_cn.md)
27-
- [ ] [float](https://github.com/zpoint/Cpython-Internals/blob/master/BasicObject/float/float_cn.md)
27+
- [x] [float](https://github.com/zpoint/Cpython-Internals/blob/master/BasicObject/float/float_cn.md)
2828
- [ ] func
2929
- [ ] method
3030
- [ ] iter

0 commit comments

Comments
 (0)