Decimal floating point arithmetic for micropython
Github repository: https://github.com/mpy-dev/micropython-decimal-number
The README.md file contains a description of the funcionality and examples.
This output of the performance test on a Raspberry Pi Pico can give you an idea of what it offers:
Code: Select all
+---------------------------------------------------------------+
| SYSTEM INFORMATION |
+---------------------------------------------------------------+
Implementation name: micropython
Implementation version: 1.17.0
Implementation platform: rp2
CPU frequency: 125 Mhz
+---------------------------------------------------------------+
| PERFORMANCE WITH SCALE = 16 |
+---------------------------------------------------------------+
Scale (max. decimals): 16
Iterations per test: 1000
Number 1: 676800226403580.0763143540562581
Number 2: -588054433.4243183156555517
Addition (n1 + n2): 1.566 ms
Subtraction (n1 - n2): 1.658 ms
Multiplication (n1 * n2): 1.008 ms
Division (n1 / n2): 1.165 ms
Square root abs(n1): 3.894 ms
Power: (pi/2) ** 15 9.742 ms
DecimalNumber from int: 0.373 ms
DecimalNumber from string: 3.771 ms
Iterations per test: 10
Sine: sin(0.54321) 86.1 ms
Cosine: cos(0.54321) 85.4 ms
Tangent: tan(0.54321) 206.2 ms
Arcsine: asin(0.54321) 267.6 ms
Arccosine: acos(0.65432) 466.6 ms
Arctangent: atan(1.2345) 482.9 ms
Exponential: exp(12.345) 150.3 ms
Natural logarithm: ln(12.345) 150.3 ms
+---------------------------------------------------------------+
| PERFORMANCE WITH SCALE = 50 |
+---------------------------------------------------------------+
Scale (max. decimals): 50
Iterations per test: 400
Number 1: -52120532222624450128770268842457267852564.2827656475718343863261636466317618740063535442755
Number 2: 45521155711104405467504347326435032.84784860134353338157872524608016421466524813041101
Addition (n1 + n2): 2.065 ms
Subtraction (n1 - n2): 2.1925 ms
Multiplication (n1 * n2): 1.55 ms
Division (n1 / n2): 1.45 ms
Square root abs(n1): 11.87 ms
Power: (pi/2) ** 15 11.69 ms
DecimalNumber from int: 0.3825 ms
DecimalNumber from string: 10.64 ms
Iterations per test: 4
Sine: sin(0.54321) 183.25 ms
Cosine: cos(0.54321) 185.5 ms
Tangent: tan(0.54321) 397.25 ms
Arcsine: asin(0.54321) 826.4999 ms
Arccosine: acos(0.65432) 1314.5 ms
Arctangent: atan(1.2345) 1368.75 ms
Exponential: exp(12.345) 280.25 ms
Natural logarithm: ln(12.345) 280.0 ms
+---------------------------------------------------------------+
| CALCULATING PI |
+---------------------------------------------------------------+
Pi with 300 decimals: 5.838 s
3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647093844609550582231725359408128481117450284102701938521105559644622948954930381964428810975665933446128475648233786783165271201909145648566923460348610454326648213393607260249141274