@@ -103,67 +103,6 @@ if x < y + 0.001: # O(1)
103103 pass
104104```
105105
106- ## Precision and Limitations
107-
108- ### Fixed 64-bit Precision
109-
110- ``` python
111- # IEEE 754 double precision (64 bits)
112- # - 1 sign bit
113- # - 11 exponent bits (range ~10^-308 to 10^308)
114- # - 52 mantissa bits (~15-17 decimal digits precision)
115-
116- x = 0.1 + 0.2
117- print (x == 0.3 ) # False! Precision loss
118-
119- # Representation
120- print (f " { x:.20f } " ) # 0.30000000000000004443
121- ```
122-
123- ### Avoiding Precision Issues
124-
125- ``` python
126- from decimal import Decimal
127-
128- # Use Decimal for financial calculations
129- price = Decimal(' 0.10' )
130- tax = Decimal(' 0.20' )
131- total = price + tax # Exact: 0.30
132-
133- # Use math.isclose for float comparisons
134- import math
135- x = 0.1 + 0.2
136- if math.isclose(x, 0.3 ): # Accounts for precision
137- print (" Equal within tolerance" )
138- ```
139-
140- ## Special Float Values
141-
142- | Value | Representation | Notes |
143- | -------| -----------------| -------|
144- | Positive infinity | ` float('inf') ` | Larger than any number |
145- | Negative infinity | ` float('-inf') ` | Smaller than any number |
146- | Not a Number | ` float('nan') ` | Unordered, ` nan != nan ` |
147- | Zero | ` 0.0 ` or ` -0.0 ` | Two representations |
148-
149- ``` python
150- import math
151-
152- x = float (' inf' )
153- y = float (' nan' )
154-
155- # Infinity operations
156- print (x + 100 ) # inf
157- print (x - x) # nan (indeterminate)
158- print (x / x) # nan
159-
160- # NaN behavior - unordered
161- print (math.isnan(y)) # True
162- print (y == y) # False (NaN != NaN always)
163- print (y < 5 ) # False (unordered)
164- print (y > 5 ) # False (unordered)
165- ```
166-
167106## Performance Characteristics
168107
169108### Fixed-size Operations
@@ -212,20 +151,6 @@ for i in range(1000000):
212151- ** [ Decimal] ( ../stdlib/decimal.md ) ** - Arbitrary precision decimal
213152- ** [ Fraction] ( ../stdlib/fractions.md ) ** - Rational numbers
214153
215- ## Best Practices
216-
217- ✅ ** Do** :
218-
219- - Use ` float ` for general numerical computation
220- - Use ` math.isclose() ` for float comparisons
221- - Use ` round() ` for display, not comparison
222-
223- ❌ ** Avoid** :
224-
225- - Comparing floats with ` == ` directly
226- - Assuming decimal representation is exact
227- - Using floats for financial calculations (use ` Decimal ` )
228-
229154## Further Reading
230155
231156- [ CPython Internals: float] ( https://zpoint.github.io/CPython-Internals/BasicObject/float/float.html ) -
0 commit comments