flexfloat 0.1.2__tar.gz → 0.1.3__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {flexfloat-0.1.2 → flexfloat-0.1.3}/.bumpversion.cfg +1 -1
- {flexfloat-0.1.2 → flexfloat-0.1.3}/.github/workflows/pr-labeler.yml +1 -0
- {flexfloat-0.1.2 → flexfloat-0.1.3}/.github/workflows/release.yml +0 -16
- flexfloat-0.1.3/PKG-INFO +340 -0
- flexfloat-0.1.3/README.md +306 -0
- flexfloat-0.1.3/flexfloat/__init__.py +32 -0
- flexfloat-0.1.3/flexfloat/bitarray/__init__.py +90 -0
- flexfloat-0.1.3/flexfloat/bitarray/bitarray.py +198 -0
- flexfloat-0.1.3/flexfloat/bitarray/bitarray_int64.py +308 -0
- flexfloat-0.1.2/flexfloat/bitarray.py → flexfloat-0.1.3/flexfloat/bitarray/bitarray_list.py +27 -97
- flexfloat-0.1.3/flexfloat/bitarray/bitarray_mixins.py +93 -0
- {flexfloat-0.1.2 → flexfloat-0.1.3}/flexfloat/core.py +306 -20
- flexfloat-0.1.3/flexfloat.egg-info/PKG-INFO +340 -0
- {flexfloat-0.1.2 → flexfloat-0.1.3}/flexfloat.egg-info/SOURCES.txt +7 -1
- {flexfloat-0.1.2 → flexfloat-0.1.3}/pyproject.toml +1 -1
- flexfloat-0.1.3/tests/__init__.py +27 -0
- {flexfloat-0.1.2 → flexfloat-0.1.3}/tests/test_addition.py +18 -20
- {flexfloat-0.1.2 → flexfloat-0.1.3}/tests/test_bigfloat.py +6 -7
- {flexfloat-0.1.2 → flexfloat-0.1.3}/tests/test_bitarray.py +58 -52
- flexfloat-0.1.3/tests/test_bitarray_implementations.py +359 -0
- {flexfloat-0.1.2 → flexfloat-0.1.3}/tests/test_conversions.py +17 -17
- {flexfloat-0.1.2 → flexfloat-0.1.3}/tests/test_division.py +15 -19
- {flexfloat-0.1.2 → flexfloat-0.1.3}/tests/test_multiplication.py +20 -23
- flexfloat-0.1.3/tests/test_power.py +488 -0
- {flexfloat-0.1.2 → flexfloat-0.1.3}/tests/test_str_representation.py +6 -6
- {flexfloat-0.1.2 → flexfloat-0.1.3}/tests/test_subtraction.py +18 -19
- flexfloat-0.1.2/PKG-INFO +0 -147
- flexfloat-0.1.2/README.md +0 -113
- flexfloat-0.1.2/flexfloat/__init__.py +0 -13
- flexfloat-0.1.2/flexfloat.egg-info/PKG-INFO +0 -147
- flexfloat-0.1.2/tests/__init__.py +0 -7
- {flexfloat-0.1.2 → flexfloat-0.1.3}/.github/workflows/build-check.yml +0 -0
- {flexfloat-0.1.2 → flexfloat-0.1.3}/.github/workflows/manual-release.yml +0 -0
- {flexfloat-0.1.2 → flexfloat-0.1.3}/.github/workflows/test.yml +0 -0
- {flexfloat-0.1.2 → flexfloat-0.1.3}/.github/workflows/version-check.yml +0 -0
- {flexfloat-0.1.2 → flexfloat-0.1.3}/.gitignore +0 -0
- {flexfloat-0.1.2 → flexfloat-0.1.3}/LICENSE +0 -0
- {flexfloat-0.1.2 → flexfloat-0.1.3}/MANIFEST.in +0 -0
- {flexfloat-0.1.2 → flexfloat-0.1.3}/flexfloat/py.typed +0 -0
- {flexfloat-0.1.2 → flexfloat-0.1.3}/flexfloat/types.py +0 -0
- {flexfloat-0.1.2 → flexfloat-0.1.3}/flexfloat.egg-info/dependency_links.txt +0 -0
- {flexfloat-0.1.2 → flexfloat-0.1.3}/flexfloat.egg-info/requires.txt +0 -0
- {flexfloat-0.1.2 → flexfloat-0.1.3}/flexfloat.egg-info/top_level.txt +0 -0
- {flexfloat-0.1.2 → flexfloat-0.1.3}/scripts/validate_cicd.py +0 -0
- {flexfloat-0.1.2 → flexfloat-0.1.3}/scripts/version_manager.py +0 -0
- {flexfloat-0.1.2 → flexfloat-0.1.3}/setup.cfg +0 -0
- {flexfloat-0.1.2 → flexfloat-0.1.3}/setup.py +0 -0
@@ -69,22 +69,6 @@ jobs:
|
|
69
69
|
# Install bump2version if not already installed
|
70
70
|
pip install bump2version
|
71
71
|
|
72
|
-
# Create .bumpversion.cfg
|
73
|
-
cat > .bumpversion.cfg << EOF
|
74
|
-
[bumpversion]
|
75
|
-
current_version = ${{ steps.current_version.outputs.version }}
|
76
|
-
commit = False
|
77
|
-
tag = False
|
78
|
-
|
79
|
-
[bumpversion:file:pyproject.toml]
|
80
|
-
search = version = "{current_version}"
|
81
|
-
replace = version = "{new_version}"
|
82
|
-
|
83
|
-
[bumpversion:file:flexfloat/__init__.py]
|
84
|
-
search = __version__ = "{current_version}"
|
85
|
-
replace = __version__ = "{new_version}"
|
86
|
-
EOF
|
87
|
-
|
88
72
|
# Bump version
|
89
73
|
bump2version ${{ steps.bump_type.outputs.bump_type }}
|
90
74
|
|
flexfloat-0.1.3/PKG-INFO
ADDED
@@ -0,0 +1,340 @@
|
|
1
|
+
Metadata-Version: 2.4
|
2
|
+
Name: flexfloat
|
3
|
+
Version: 0.1.3
|
4
|
+
Summary: A library for arbitrary precision floating point arithmetic
|
5
|
+
Author: Ferran Sanchez Llado
|
6
|
+
License: MIT
|
7
|
+
Classifier: Development Status :: 3 - Alpha
|
8
|
+
Classifier: Intended Audience :: Developers
|
9
|
+
Classifier: Intended Audience :: Science/Research
|
10
|
+
Classifier: License :: OSI Approved :: MIT License
|
11
|
+
Classifier: Operating System :: OS Independent
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
13
|
+
Classifier: Programming Language :: Python :: 3.11
|
14
|
+
Classifier: Programming Language :: Python :: 3.12
|
15
|
+
Classifier: Topic :: Scientific/Engineering :: Mathematics
|
16
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
17
|
+
Requires-Python: >=3.11
|
18
|
+
Description-Content-Type: text/markdown
|
19
|
+
License-File: LICENSE
|
20
|
+
Provides-Extra: dev
|
21
|
+
Requires-Dist: pytest>=7.0; extra == "dev"
|
22
|
+
Requires-Dist: pytest-cov>=4.0; extra == "dev"
|
23
|
+
Requires-Dist: black>=23.0; extra == "dev"
|
24
|
+
Requires-Dist: isort>=5.0; extra == "dev"
|
25
|
+
Requires-Dist: mypy>=1.0; extra == "dev"
|
26
|
+
Requires-Dist: pylint>=3.0; extra == "dev"
|
27
|
+
Requires-Dist: flake8>=6.0; extra == "dev"
|
28
|
+
Requires-Dist: bump2version>=1.0; extra == "dev"
|
29
|
+
Requires-Dist: build>=0.10; extra == "dev"
|
30
|
+
Requires-Dist: twine>=4.0; extra == "dev"
|
31
|
+
Requires-Dist: check-manifest>=0.49; extra == "dev"
|
32
|
+
Requires-Dist: PyYAML>=6.0; extra == "dev"
|
33
|
+
Dynamic: license-file
|
34
|
+
|
35
|
+
# FlexFloat
|
36
|
+
|
37
|
+
[](https://www.python.org/downloads/)
|
38
|
+
[](https://opensource.org/licenses/MIT)
|
39
|
+
[](https://badge.fury.io/py/flexfloat)
|
40
|
+
|
41
|
+
A high-precision Python library for arbitrary precision floating-point arithmetic with **growable exponents** and **fixed-size fractions**. FlexFloat extends IEEE 754 double-precision format to handle numbers beyond the standard range while maintaining computational efficiency and precision consistency.
|
42
|
+
|
43
|
+
## ✨ Key Features
|
44
|
+
|
45
|
+
- **🔢 Growable Exponents**: Dynamically expand exponent size to handle extremely large (>10^308) or small (<10^-308) numbers
|
46
|
+
- **🎯 Fixed-Size Fractions**: Maintain IEEE 754-compatible 52-bit fraction precision for consistent accuracy
|
47
|
+
- **⚡ Full Arithmetic Support**: Addition, subtraction, multiplication, division, and power operations
|
48
|
+
- **🔧 Multiple BitArray Backends**: Choose between list-based and int64-based implementations for optimal performance
|
49
|
+
- **🌟 Special Value Handling**: Complete support for NaN, ±infinity, and zero values
|
50
|
+
- **🛡️ Overflow Protection**: Automatic exponent growth prevents overflow/underflow errors
|
51
|
+
- **📊 IEEE 754 Baseline**: Fully compatible with standard double-precision format as the starting point
|
52
|
+
|
53
|
+
## 🚀 Quick Start
|
54
|
+
|
55
|
+
### Installation
|
56
|
+
|
57
|
+
```bash
|
58
|
+
pip install flexfloat
|
59
|
+
```
|
60
|
+
|
61
|
+
### Basic Usage
|
62
|
+
|
63
|
+
```python
|
64
|
+
from flexfloat import FlexFloat
|
65
|
+
|
66
|
+
# Create FlexFloat instances
|
67
|
+
a = FlexFloat.from_float(1.5)
|
68
|
+
b = FlexFloat.from_float(2.5)
|
69
|
+
|
70
|
+
# Perform arithmetic operations
|
71
|
+
result = a + b
|
72
|
+
print(result.to_float()) # 4.0
|
73
|
+
|
74
|
+
# Handle very large numbers that would overflow standard floats
|
75
|
+
large_a = FlexFloat.from_float(1e308)
|
76
|
+
large_b = FlexFloat.from_float(1e308)
|
77
|
+
large_result = large_a + large_b
|
78
|
+
|
79
|
+
# Result automatically grows exponent to handle the overflow
|
80
|
+
print(f"Exponent bits: {len(large_result.exponent)}") # > 11 (grown beyond IEEE 754)
|
81
|
+
print(f"Can represent: {large_result}") # No overflow!
|
82
|
+
```
|
83
|
+
|
84
|
+
### Advanced Examples
|
85
|
+
|
86
|
+
```python
|
87
|
+
from flexfloat import FlexFloat
|
88
|
+
|
89
|
+
# Mathematical operations
|
90
|
+
x = FlexFloat.from_float(2.0)
|
91
|
+
y = FlexFloat.from_float(3.0)
|
92
|
+
|
93
|
+
# Power operations
|
94
|
+
power_result = x ** y # 2^3 = 8
|
95
|
+
print(power_result.to_float()) # 8.0
|
96
|
+
|
97
|
+
# Exponential using Euler's number
|
98
|
+
e_result = FlexFloat.e ** x # e^2
|
99
|
+
print(f"e^2 ≈ {e_result.to_float()}")
|
100
|
+
|
101
|
+
# Working with extreme values
|
102
|
+
tiny = FlexFloat.from_float(1e-300)
|
103
|
+
huge = FlexFloat.from_float(1e300)
|
104
|
+
extreme_product = tiny * huge
|
105
|
+
print(f"Product: {extreme_product.to_float()}") # Still computable!
|
106
|
+
|
107
|
+
# Precision demonstration
|
108
|
+
precise_calc = FlexFloat.from_float(1.0) / FlexFloat.from_float(3.0)
|
109
|
+
print(f"1/3 with 52-bit precision: {precise_calc}")
|
110
|
+
```
|
111
|
+
|
112
|
+
## 🔧 BitArray Backends
|
113
|
+
|
114
|
+
FlexFloat supports multiple BitArray implementations for different performance characteristics:
|
115
|
+
|
116
|
+
```python
|
117
|
+
from flexfloat import (
|
118
|
+
FlexFloat,
|
119
|
+
set_default_implementation,
|
120
|
+
get_available_implementations
|
121
|
+
)
|
122
|
+
|
123
|
+
# View available implementations
|
124
|
+
print(get_available_implementations()) # ['list', 'int64']
|
125
|
+
|
126
|
+
# Use list-based implementation (default, more flexible)
|
127
|
+
set_default_implementation('list')
|
128
|
+
flex_list = FlexFloat.from_float(42.0)
|
129
|
+
|
130
|
+
# Use int64-based implementation (faster for small bit arrays)
|
131
|
+
set_default_implementation('int64')
|
132
|
+
flex_int64 = FlexFloat.from_float(42.0)
|
133
|
+
|
134
|
+
# Both produce the same results with different performance characteristics
|
135
|
+
```
|
136
|
+
|
137
|
+
### Implementation Comparison
|
138
|
+
|
139
|
+
| Implementation | Best For | Pros | Cons |
|
140
|
+
|---------------|----------|------|------|
|
141
|
+
| `list[bool]` | Smaller exponents and testing | Flexible, easy to understand | Slower for large numbers |
|
142
|
+
| `list[int64]` | Standard operations | Fast for bigger numbers, efficient memory | Overhead for small numbers |
|
143
|
+
|
144
|
+
## 📚 API Reference
|
145
|
+
|
146
|
+
### Core Operations
|
147
|
+
|
148
|
+
```python
|
149
|
+
# Construction
|
150
|
+
FlexFloat.from_float(value: float) -> FlexFloat
|
151
|
+
FlexFloat(sign: bool, exponent: BitArray, fraction: BitArray)
|
152
|
+
|
153
|
+
# Conversion
|
154
|
+
flexfloat.to_float() -> float
|
155
|
+
|
156
|
+
# Arithmetic
|
157
|
+
a + b, a - b, a * b, a / b, a ** b
|
158
|
+
abs(a), -a
|
159
|
+
|
160
|
+
# Mathematical functions
|
161
|
+
FlexFloat.e ** x # Exponential function
|
162
|
+
```
|
163
|
+
|
164
|
+
### Special Values
|
165
|
+
|
166
|
+
```python
|
167
|
+
from flexfloat import FlexFloat
|
168
|
+
|
169
|
+
# Create special values
|
170
|
+
nan_val = FlexFloat.nan()
|
171
|
+
inf_val = FlexFloat.infinity()
|
172
|
+
neg_inf = FlexFloat.negative_infinity()
|
173
|
+
zero_val = FlexFloat.zero()
|
174
|
+
|
175
|
+
# Check for special values
|
176
|
+
if result.is_nan():
|
177
|
+
print("Result is Not a Number")
|
178
|
+
if result.is_infinite():
|
179
|
+
print("Result is infinite")
|
180
|
+
```
|
181
|
+
|
182
|
+
## 🧪 Development & Testing
|
183
|
+
|
184
|
+
### Development Installation
|
185
|
+
|
186
|
+
```bash
|
187
|
+
git clone https://github.com/ferranSanchezLlado/flexfloat-py.git
|
188
|
+
cd flexfloat-py
|
189
|
+
pip install -e ".[dev]"
|
190
|
+
```
|
191
|
+
|
192
|
+
### Running Tests
|
193
|
+
|
194
|
+
```bash
|
195
|
+
# Run all tests
|
196
|
+
python -m pytest tests/
|
197
|
+
|
198
|
+
# Run with coverage
|
199
|
+
python -m pytest tests/ --cov=flexfloat --cov-report=html
|
200
|
+
|
201
|
+
# Run specific test categories
|
202
|
+
python -m pytest tests/test_arithmetic.py # Arithmetic operations
|
203
|
+
python -m pytest tests/test_conversions.py # Number conversions
|
204
|
+
python -m pytest tests/test_bitarray.py # BitArray implementations
|
205
|
+
```
|
206
|
+
|
207
|
+
### Code Quality
|
208
|
+
|
209
|
+
```bash
|
210
|
+
# Format code
|
211
|
+
black flexfloat/ tests/
|
212
|
+
|
213
|
+
# Sort imports
|
214
|
+
isort flexfloat/ tests/
|
215
|
+
|
216
|
+
# Type checking
|
217
|
+
mypy flexfloat/
|
218
|
+
|
219
|
+
# Linting
|
220
|
+
pylint flexfloat/
|
221
|
+
flake8 flexfloat/
|
222
|
+
```
|
223
|
+
|
224
|
+
## 🎯 Use Cases
|
225
|
+
|
226
|
+
### Scientific Computing
|
227
|
+
```python
|
228
|
+
# Handle calculations that would overflow standard floats
|
229
|
+
from flexfloat import FlexFloat
|
230
|
+
|
231
|
+
# Factorial of large numbers
|
232
|
+
def flex_factorial(n):
|
233
|
+
result = FlexFloat.from_float(1.0)
|
234
|
+
for i in range(1, n + 1):
|
235
|
+
result = result * FlexFloat.from_float(i)
|
236
|
+
return result
|
237
|
+
|
238
|
+
large_factorial = flex_factorial(1000) # No overflow!
|
239
|
+
```
|
240
|
+
|
241
|
+
### Financial Calculations
|
242
|
+
```python
|
243
|
+
# High-precision compound interest calculations
|
244
|
+
principal = FlexFloat.from_float(1000000.0)
|
245
|
+
rate = FlexFloat.from_float(1.05) # 5% annual return
|
246
|
+
years = FlexFloat.from_float(100)
|
247
|
+
|
248
|
+
# Calculate compound interest over very long periods
|
249
|
+
final_amount = principal * (rate ** years)
|
250
|
+
```
|
251
|
+
|
252
|
+
### Physics Simulations
|
253
|
+
```python
|
254
|
+
# Handle extreme values in physics calculations
|
255
|
+
c = FlexFloat.from_float(299792458) # Speed of light
|
256
|
+
mass = FlexFloat.from_float(1e-30) # Atomic mass
|
257
|
+
|
258
|
+
# E = mc² with extreme precision
|
259
|
+
energy = mass * c * c
|
260
|
+
```
|
261
|
+
|
262
|
+
## 🏗️ Architecture
|
263
|
+
|
264
|
+
FlexFloat is built with a modular architecture:
|
265
|
+
|
266
|
+
```
|
267
|
+
flexfloat/
|
268
|
+
├── core.py # Main FlexFloat class
|
269
|
+
├── types.py # Type definitions
|
270
|
+
├── bitarray/ # BitArray implementations
|
271
|
+
│ ├── bitarray.py # Abstract base class
|
272
|
+
│ ├── bitarray_list.py # List-based implementation
|
273
|
+
│ ├── bitarray_int64.py # Int64-based implementation
|
274
|
+
│ └── bitarray_mixins.py # Common functionality
|
275
|
+
└── __init__.py # Public API exports
|
276
|
+
```
|
277
|
+
|
278
|
+
### Design Principles
|
279
|
+
|
280
|
+
1. **IEEE 754 Compatibility**: Start with standard double-precision format
|
281
|
+
2. **Graceful Scaling**: Automatically expand exponent when needed
|
282
|
+
3. **Precision Preservation**: Keep fraction size fixed for consistent accuracy
|
283
|
+
4. **Performance Options**: Multiple backends for different use cases
|
284
|
+
5. **Pythonic Interface**: Natural syntax for mathematical operations
|
285
|
+
|
286
|
+
## 📊 Performance Considerations
|
287
|
+
|
288
|
+
### When to Use FlexFloat
|
289
|
+
|
290
|
+
✅ **Good for:**
|
291
|
+
- Calculations requiring numbers > 10^308 or < 10^-308
|
292
|
+
- Scientific computing with extreme values
|
293
|
+
- Financial calculations requiring high precision
|
294
|
+
- Preventing overflow/underflow in long calculations
|
295
|
+
|
296
|
+
❌ **Consider alternatives for:**
|
297
|
+
- Simple arithmetic with standard-range numbers
|
298
|
+
- Performance-critical tight loops
|
299
|
+
- Applications where standard `float` precision is sufficient
|
300
|
+
|
301
|
+
### Optimization Tips
|
302
|
+
|
303
|
+
```python
|
304
|
+
# Prefer int64 implementation for standard operations
|
305
|
+
set_default_implementation('int64')
|
306
|
+
|
307
|
+
# Batch operations when possible
|
308
|
+
values = [FlexFloat.from_float(x) for x in range(1000)]
|
309
|
+
sum_result = sum(values, FlexFloat.zero())
|
310
|
+
|
311
|
+
# Use appropriate precision for your use case
|
312
|
+
if value_in_standard_range:
|
313
|
+
result = float(flexfloat_result.to_float()) # Convert back if needed
|
314
|
+
```
|
315
|
+
|
316
|
+
## 📋 Roadmap
|
317
|
+
|
318
|
+
- [ ] Additional mathematical functions (sin, cos, tan, log, sqrt)
|
319
|
+
- [ ] Serialization support (JSON, pickle)
|
320
|
+
- [ ] Performance optimizations for large arrays
|
321
|
+
- [ ] Complex number support
|
322
|
+
- [ ] Decimal mode for exact decimal representation
|
323
|
+
|
324
|
+
|
325
|
+
## 📄 License
|
326
|
+
|
327
|
+
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
|
328
|
+
|
329
|
+
## 🙏 Acknowledgments
|
330
|
+
|
331
|
+
- IEEE 754 standard for floating-point arithmetic foundation
|
332
|
+
- Python community for inspiration and best practices
|
333
|
+
- Contributors and users who help improve the library
|
334
|
+
|
335
|
+
## 📞 Support
|
336
|
+
|
337
|
+
- 📚 **Documentation**: Full API documentation available in docstrings
|
338
|
+
- 🐛 **Issues**: Report bugs on [GitHub Issues](https://github.com/ferranSanchezLlado/flexfloat-py/issues)
|
339
|
+
- 💬 **Discussions**: Join conversations on [GitHub Discussions](https://github.com/ferranSanchezLlado/flexfloat-py/discussions)
|
340
|
+
- 📧 **Contact**: Reach out to the maintainer for questions
|
@@ -0,0 +1,306 @@
|
|
1
|
+
# FlexFloat
|
2
|
+
|
3
|
+
[](https://www.python.org/downloads/)
|
4
|
+
[](https://opensource.org/licenses/MIT)
|
5
|
+
[](https://badge.fury.io/py/flexfloat)
|
6
|
+
|
7
|
+
A high-precision Python library for arbitrary precision floating-point arithmetic with **growable exponents** and **fixed-size fractions**. FlexFloat extends IEEE 754 double-precision format to handle numbers beyond the standard range while maintaining computational efficiency and precision consistency.
|
8
|
+
|
9
|
+
## ✨ Key Features
|
10
|
+
|
11
|
+
- **🔢 Growable Exponents**: Dynamically expand exponent size to handle extremely large (>10^308) or small (<10^-308) numbers
|
12
|
+
- **🎯 Fixed-Size Fractions**: Maintain IEEE 754-compatible 52-bit fraction precision for consistent accuracy
|
13
|
+
- **⚡ Full Arithmetic Support**: Addition, subtraction, multiplication, division, and power operations
|
14
|
+
- **🔧 Multiple BitArray Backends**: Choose between list-based and int64-based implementations for optimal performance
|
15
|
+
- **🌟 Special Value Handling**: Complete support for NaN, ±infinity, and zero values
|
16
|
+
- **🛡️ Overflow Protection**: Automatic exponent growth prevents overflow/underflow errors
|
17
|
+
- **📊 IEEE 754 Baseline**: Fully compatible with standard double-precision format as the starting point
|
18
|
+
|
19
|
+
## 🚀 Quick Start
|
20
|
+
|
21
|
+
### Installation
|
22
|
+
|
23
|
+
```bash
|
24
|
+
pip install flexfloat
|
25
|
+
```
|
26
|
+
|
27
|
+
### Basic Usage
|
28
|
+
|
29
|
+
```python
|
30
|
+
from flexfloat import FlexFloat
|
31
|
+
|
32
|
+
# Create FlexFloat instances
|
33
|
+
a = FlexFloat.from_float(1.5)
|
34
|
+
b = FlexFloat.from_float(2.5)
|
35
|
+
|
36
|
+
# Perform arithmetic operations
|
37
|
+
result = a + b
|
38
|
+
print(result.to_float()) # 4.0
|
39
|
+
|
40
|
+
# Handle very large numbers that would overflow standard floats
|
41
|
+
large_a = FlexFloat.from_float(1e308)
|
42
|
+
large_b = FlexFloat.from_float(1e308)
|
43
|
+
large_result = large_a + large_b
|
44
|
+
|
45
|
+
# Result automatically grows exponent to handle the overflow
|
46
|
+
print(f"Exponent bits: {len(large_result.exponent)}") # > 11 (grown beyond IEEE 754)
|
47
|
+
print(f"Can represent: {large_result}") # No overflow!
|
48
|
+
```
|
49
|
+
|
50
|
+
### Advanced Examples
|
51
|
+
|
52
|
+
```python
|
53
|
+
from flexfloat import FlexFloat
|
54
|
+
|
55
|
+
# Mathematical operations
|
56
|
+
x = FlexFloat.from_float(2.0)
|
57
|
+
y = FlexFloat.from_float(3.0)
|
58
|
+
|
59
|
+
# Power operations
|
60
|
+
power_result = x ** y # 2^3 = 8
|
61
|
+
print(power_result.to_float()) # 8.0
|
62
|
+
|
63
|
+
# Exponential using Euler's number
|
64
|
+
e_result = FlexFloat.e ** x # e^2
|
65
|
+
print(f"e^2 ≈ {e_result.to_float()}")
|
66
|
+
|
67
|
+
# Working with extreme values
|
68
|
+
tiny = FlexFloat.from_float(1e-300)
|
69
|
+
huge = FlexFloat.from_float(1e300)
|
70
|
+
extreme_product = tiny * huge
|
71
|
+
print(f"Product: {extreme_product.to_float()}") # Still computable!
|
72
|
+
|
73
|
+
# Precision demonstration
|
74
|
+
precise_calc = FlexFloat.from_float(1.0) / FlexFloat.from_float(3.0)
|
75
|
+
print(f"1/3 with 52-bit precision: {precise_calc}")
|
76
|
+
```
|
77
|
+
|
78
|
+
## 🔧 BitArray Backends
|
79
|
+
|
80
|
+
FlexFloat supports multiple BitArray implementations for different performance characteristics:
|
81
|
+
|
82
|
+
```python
|
83
|
+
from flexfloat import (
|
84
|
+
FlexFloat,
|
85
|
+
set_default_implementation,
|
86
|
+
get_available_implementations
|
87
|
+
)
|
88
|
+
|
89
|
+
# View available implementations
|
90
|
+
print(get_available_implementations()) # ['list', 'int64']
|
91
|
+
|
92
|
+
# Use list-based implementation (default, more flexible)
|
93
|
+
set_default_implementation('list')
|
94
|
+
flex_list = FlexFloat.from_float(42.0)
|
95
|
+
|
96
|
+
# Use int64-based implementation (faster for small bit arrays)
|
97
|
+
set_default_implementation('int64')
|
98
|
+
flex_int64 = FlexFloat.from_float(42.0)
|
99
|
+
|
100
|
+
# Both produce the same results with different performance characteristics
|
101
|
+
```
|
102
|
+
|
103
|
+
### Implementation Comparison
|
104
|
+
|
105
|
+
| Implementation | Best For | Pros | Cons |
|
106
|
+
|---------------|----------|------|------|
|
107
|
+
| `list[bool]` | Smaller exponents and testing | Flexible, easy to understand | Slower for large numbers |
|
108
|
+
| `list[int64]` | Standard operations | Fast for bigger numbers, efficient memory | Overhead for small numbers |
|
109
|
+
|
110
|
+
## 📚 API Reference
|
111
|
+
|
112
|
+
### Core Operations
|
113
|
+
|
114
|
+
```python
|
115
|
+
# Construction
|
116
|
+
FlexFloat.from_float(value: float) -> FlexFloat
|
117
|
+
FlexFloat(sign: bool, exponent: BitArray, fraction: BitArray)
|
118
|
+
|
119
|
+
# Conversion
|
120
|
+
flexfloat.to_float() -> float
|
121
|
+
|
122
|
+
# Arithmetic
|
123
|
+
a + b, a - b, a * b, a / b, a ** b
|
124
|
+
abs(a), -a
|
125
|
+
|
126
|
+
# Mathematical functions
|
127
|
+
FlexFloat.e ** x # Exponential function
|
128
|
+
```
|
129
|
+
|
130
|
+
### Special Values
|
131
|
+
|
132
|
+
```python
|
133
|
+
from flexfloat import FlexFloat
|
134
|
+
|
135
|
+
# Create special values
|
136
|
+
nan_val = FlexFloat.nan()
|
137
|
+
inf_val = FlexFloat.infinity()
|
138
|
+
neg_inf = FlexFloat.negative_infinity()
|
139
|
+
zero_val = FlexFloat.zero()
|
140
|
+
|
141
|
+
# Check for special values
|
142
|
+
if result.is_nan():
|
143
|
+
print("Result is Not a Number")
|
144
|
+
if result.is_infinite():
|
145
|
+
print("Result is infinite")
|
146
|
+
```
|
147
|
+
|
148
|
+
## 🧪 Development & Testing
|
149
|
+
|
150
|
+
### Development Installation
|
151
|
+
|
152
|
+
```bash
|
153
|
+
git clone https://github.com/ferranSanchezLlado/flexfloat-py.git
|
154
|
+
cd flexfloat-py
|
155
|
+
pip install -e ".[dev]"
|
156
|
+
```
|
157
|
+
|
158
|
+
### Running Tests
|
159
|
+
|
160
|
+
```bash
|
161
|
+
# Run all tests
|
162
|
+
python -m pytest tests/
|
163
|
+
|
164
|
+
# Run with coverage
|
165
|
+
python -m pytest tests/ --cov=flexfloat --cov-report=html
|
166
|
+
|
167
|
+
# Run specific test categories
|
168
|
+
python -m pytest tests/test_arithmetic.py # Arithmetic operations
|
169
|
+
python -m pytest tests/test_conversions.py # Number conversions
|
170
|
+
python -m pytest tests/test_bitarray.py # BitArray implementations
|
171
|
+
```
|
172
|
+
|
173
|
+
### Code Quality
|
174
|
+
|
175
|
+
```bash
|
176
|
+
# Format code
|
177
|
+
black flexfloat/ tests/
|
178
|
+
|
179
|
+
# Sort imports
|
180
|
+
isort flexfloat/ tests/
|
181
|
+
|
182
|
+
# Type checking
|
183
|
+
mypy flexfloat/
|
184
|
+
|
185
|
+
# Linting
|
186
|
+
pylint flexfloat/
|
187
|
+
flake8 flexfloat/
|
188
|
+
```
|
189
|
+
|
190
|
+
## 🎯 Use Cases
|
191
|
+
|
192
|
+
### Scientific Computing
|
193
|
+
```python
|
194
|
+
# Handle calculations that would overflow standard floats
|
195
|
+
from flexfloat import FlexFloat
|
196
|
+
|
197
|
+
# Factorial of large numbers
|
198
|
+
def flex_factorial(n):
|
199
|
+
result = FlexFloat.from_float(1.0)
|
200
|
+
for i in range(1, n + 1):
|
201
|
+
result = result * FlexFloat.from_float(i)
|
202
|
+
return result
|
203
|
+
|
204
|
+
large_factorial = flex_factorial(1000) # No overflow!
|
205
|
+
```
|
206
|
+
|
207
|
+
### Financial Calculations
|
208
|
+
```python
|
209
|
+
# High-precision compound interest calculations
|
210
|
+
principal = FlexFloat.from_float(1000000.0)
|
211
|
+
rate = FlexFloat.from_float(1.05) # 5% annual return
|
212
|
+
years = FlexFloat.from_float(100)
|
213
|
+
|
214
|
+
# Calculate compound interest over very long periods
|
215
|
+
final_amount = principal * (rate ** years)
|
216
|
+
```
|
217
|
+
|
218
|
+
### Physics Simulations
|
219
|
+
```python
|
220
|
+
# Handle extreme values in physics calculations
|
221
|
+
c = FlexFloat.from_float(299792458) # Speed of light
|
222
|
+
mass = FlexFloat.from_float(1e-30) # Atomic mass
|
223
|
+
|
224
|
+
# E = mc² with extreme precision
|
225
|
+
energy = mass * c * c
|
226
|
+
```
|
227
|
+
|
228
|
+
## 🏗️ Architecture
|
229
|
+
|
230
|
+
FlexFloat is built with a modular architecture:
|
231
|
+
|
232
|
+
```
|
233
|
+
flexfloat/
|
234
|
+
├── core.py # Main FlexFloat class
|
235
|
+
├── types.py # Type definitions
|
236
|
+
├── bitarray/ # BitArray implementations
|
237
|
+
│ ├── bitarray.py # Abstract base class
|
238
|
+
│ ├── bitarray_list.py # List-based implementation
|
239
|
+
│ ├── bitarray_int64.py # Int64-based implementation
|
240
|
+
│ └── bitarray_mixins.py # Common functionality
|
241
|
+
└── __init__.py # Public API exports
|
242
|
+
```
|
243
|
+
|
244
|
+
### Design Principles
|
245
|
+
|
246
|
+
1. **IEEE 754 Compatibility**: Start with standard double-precision format
|
247
|
+
2. **Graceful Scaling**: Automatically expand exponent when needed
|
248
|
+
3. **Precision Preservation**: Keep fraction size fixed for consistent accuracy
|
249
|
+
4. **Performance Options**: Multiple backends for different use cases
|
250
|
+
5. **Pythonic Interface**: Natural syntax for mathematical operations
|
251
|
+
|
252
|
+
## 📊 Performance Considerations
|
253
|
+
|
254
|
+
### When to Use FlexFloat
|
255
|
+
|
256
|
+
✅ **Good for:**
|
257
|
+
- Calculations requiring numbers > 10^308 or < 10^-308
|
258
|
+
- Scientific computing with extreme values
|
259
|
+
- Financial calculations requiring high precision
|
260
|
+
- Preventing overflow/underflow in long calculations
|
261
|
+
|
262
|
+
❌ **Consider alternatives for:**
|
263
|
+
- Simple arithmetic with standard-range numbers
|
264
|
+
- Performance-critical tight loops
|
265
|
+
- Applications where standard `float` precision is sufficient
|
266
|
+
|
267
|
+
### Optimization Tips
|
268
|
+
|
269
|
+
```python
|
270
|
+
# Prefer int64 implementation for standard operations
|
271
|
+
set_default_implementation('int64')
|
272
|
+
|
273
|
+
# Batch operations when possible
|
274
|
+
values = [FlexFloat.from_float(x) for x in range(1000)]
|
275
|
+
sum_result = sum(values, FlexFloat.zero())
|
276
|
+
|
277
|
+
# Use appropriate precision for your use case
|
278
|
+
if value_in_standard_range:
|
279
|
+
result = float(flexfloat_result.to_float()) # Convert back if needed
|
280
|
+
```
|
281
|
+
|
282
|
+
## 📋 Roadmap
|
283
|
+
|
284
|
+
- [ ] Additional mathematical functions (sin, cos, tan, log, sqrt)
|
285
|
+
- [ ] Serialization support (JSON, pickle)
|
286
|
+
- [ ] Performance optimizations for large arrays
|
287
|
+
- [ ] Complex number support
|
288
|
+
- [ ] Decimal mode for exact decimal representation
|
289
|
+
|
290
|
+
|
291
|
+
## 📄 License
|
292
|
+
|
293
|
+
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
|
294
|
+
|
295
|
+
## 🙏 Acknowledgments
|
296
|
+
|
297
|
+
- IEEE 754 standard for floating-point arithmetic foundation
|
298
|
+
- Python community for inspiration and best practices
|
299
|
+
- Contributors and users who help improve the library
|
300
|
+
|
301
|
+
## 📞 Support
|
302
|
+
|
303
|
+
- 📚 **Documentation**: Full API documentation available in docstrings
|
304
|
+
- 🐛 **Issues**: Report bugs on [GitHub Issues](https://github.com/ferranSanchezLlado/flexfloat-py/issues)
|
305
|
+
- 💬 **Discussions**: Join conversations on [GitHub Discussions](https://github.com/ferranSanchezLlado/flexfloat-py/discussions)
|
306
|
+
- 📧 **Contact**: Reach out to the maintainer for questions
|