-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPython Numbers 7
More file actions
125 lines (65 loc) · 1.73 KB
/
Python Numbers 7
File metadata and controls
125 lines (65 loc) · 1.73 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
Python Numbers
Number Types
int: integral number
float: floating point number, usually IEEE 754 (64 bits, base 2)
complex: complex number, implemented as two floats
decimal.Decimal: floating point number stored in base 10, arbitrary precision
fractions.Fraction: rational, numerator/denominator; automatically compute the greatest common divisor (GCD) to simplify the fraction
Number Tower
The numbers module is specified by the PEP 3141 – A Type Hierarchy for Numbers.
numbers.Number: base class
numbers.Complex: Add real, imag, conjugate()
numbers.Real: subclass of Complex; add many float operations.
numbers.Rational: subclass of Real; add numerator and denominator attributes
numbers.Integral: subclass of Rational
Subclasses:
numbers.Number: int, float, complex, decimal.Decimal, fractions.Fraction
numbers.Complex: int, float, complex, fractions.Fraction
numbers.Real: int, float, fractions.Fraction
numbers.Rational: int, fractions.Fraction
numbers.Integral: int
int, float, complex methods and attributes:
conjugate()
imag
real
int and Fraction attributes:
denominator
numerator
Conversions in Python
int(obj) and float(obj) accept:
int
float
decimal.Decimal
fractions.Fraction
bytes
str
int(obj) rounds towards zero (ROUND_DOWN, ex: int(0.9) == 0 and int(-0.9) == 0).
But int(obj) and float(obj) reject:
complex
complex(obj) accepts:
int
float
complex
decimal.Decimal
fractions.Fraction
str
But complex(obj) rejects:
bytes
decimal.Decimal(obj) accepts:
int
float
decimal.Decimal
str
But decimal.Decimal(obj) rejects:
complex
fractions.Fraction
bytes
fractions.Fraction(obj) accepts:
int
float
decimal.Decimal
fractions.Fraction
str (ex: "1" or "1/2")
But fractions.Fraction(obj) rejects:
complex
bytes