Skip to content

Commit 4a85575

Browse files
committed
float v0.2
1 parent 9a27403 commit 4a85575

7 files changed

Lines changed: 30 additions & 43 deletions

File tree

BasicObject/float/-0.1.png

135 KB
Loading

BasicObject/float/0.1.png

137 KB
Loading

BasicObject/float/0.png

140 KB
Loading

BasicObject/float/1.1.png

130 KB
Loading

BasicObject/float/1.png

138 KB
Loading

BasicObject/float/float.md

Lines changed: 30 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -5,73 +5,60 @@
55
* [related file](#related-file)
66
* [memory layout](#memory-layout)
77
* [example](#example)
8-
* [empty bytes](#empty-bytes)
9-
* [ascii characters](#ascii-characters)
10-
* [nonascii characters](#nonascii-characters)
8+
* [0](#0)
9+
* [1](#1)
10+
* [0.1](#0.1)
11+
* [1.1](#1.1)
12+
* [-0.1](#-0.1)
1113
* [summary](#summary)
12-
* [ob_shash](#ob_shash)
13-
* [ob_size](#ob_size)
14-
* [summary](#summary)
1514

1615
#### related file
17-
* cpython/Objects/bytesobject.c
18-
* cpython/Include/bytesobject.h
19-
* cpython/Objects/clinic/bytesobject.c.h
16+
* cpython/Objects/floatobject.c
17+
* cpython/Include/floatobject.h
18+
* cpython/Objects/clinic/floatobject.c.h
2019

2120
#### memory layout
2221

23-
![memory layout](https://img-blog.csdnimg.cn/20190318160629447.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzMxNzIwMzI5,size_16,color_FFFFFF,t_70)
22+
**PyFloatObject** is no more than a wrapper of c type **double**, which takes 8 bytes to represent a floating point number
2423

25-
The memory layout of **PyBytesObject** looks like [memory layout of tuple object](https://github.com/zpoint/Cpython-Internals/blob/master/BasicObject/tuple/tuple.md#memory-layout) and [memory layout of int object](https://github.com/zpoint/Cpython-Internals/blob/master/BasicObject/long/long.md#memory-layout), but simpler than any of them.
24+
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
2625

27-
#### example
28-
29-
##### empty bytes
30-
31-
**bytes** object is an immutable object, whenever you need to modify a **bytes** object, you need to create a new one, which keeps the implementation simple.
32-
33-
s = b""
34-
35-
![empty](https://github.com/zpoint/Cpython-Internals/blob/master/BasicObject/bytes/empty.png)
36-
37-
##### ascii characters
26+
![layout](https://github.com/zpoint/Cpython-Internals/blob/master/BasicObject/float/layout.png)
3827

39-
let's initialize a byte object with ascii characters
28+
#### example
4029

41-
s = b"abcdefg123"
30+
##### 0
4231

43-
![ascii](https://github.com/zpoint/Cpython-Internals/blob/master/BasicObject/bytes/ascii.png)
32+
the binary representation of 0.0 in **IEEE 754** format is all bit in zero
4433

45-
##### nonascii characters
34+
f = 0.0
4635

47-
s = "我是帅哥".encode("utf8")
36+
![0](https://github.com/zpoint/Cpython-Internals/blob/master/BasicObject/float/0.png)
4837

49-
![nonascii](https://github.com/zpoint/Cpython-Internals/blob/master/BasicObject/bytes/nonascii.png)
38+
##### 1.0
5039

51-
#### summary
40+
f = 1.0
5241

42+
![1](https://github.com/zpoint/Cpython-Internals/blob/master/BasicObject/float/1.png)
5343

54-
##### ob_shash
44+
##### 0.1
5545

46+
f = 0.1
5647

57-
The field **ob_shash** should stores the hash value of the byte object, value **-1** means not computed yet.
48+
![0.1](https://github.com/zpoint/Cpython-Internals/blob/master/BasicObject/float/0.1.png)
5849

59-
The first time the hash value computed, it will be cached to the **ob_shash** field
50+
##### 1.1
6051

61-
the cached hash value can saves recalculation and speeds up dict lookups
52+
the difference between 1.1 and 0.1 is the last few exponent bits
6253

63-
##### ob_size
54+
![1.1](https://github.com/zpoint/Cpython-Internals/blob/master/BasicObject/float/1.1.png)
6455

65-
field **ob_size** is inside every **PyVarObject**, the **PyBytesObject** uses this **field** to store size information to keep O(1) time complexity for **len()** opeeration and tracks the size of non-ascii string(may be null characters inside)
56+
##### -0.1
6657

67-
##### summary
58+
the difference between -0.1 and 0.1 is the first sign bit
6859

69-
The **PyBytesObject** is a python wrapper of c style null terminate string, with **ob_shash** for caching hash value and **ob_size** for storing the size information of **PyBytesObject**
60+
![-0.1](https://github.com/zpoint/Cpython-Internals/blob/master/BasicObject/float/-0.1.png)
7061

71-
The implementation of **PyBytesObject** looks like the **embstr** encoding in redis
62+
##### nonascii characters
7263

73-
redis-cli
74-
127.0.0.1:6379> set a "hello"
75-
OK
76-
127.0.0.1:6379> object encoding a
77-
"embstr"
64+
s = "我是帅哥".encode("utf8")

BasicObject/float/layout.png

34.7 KB
Loading

0 commit comments

Comments
 (0)