Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Decimal floating point arithmetic for micropython
This Python module for [*micropython*](https://micropython.org/) provides support for decimal floating point arithmetic. It tries to overcome the limitations of single precision float numbers (32-bit) and provides a solution when double precision float numbers (64 bit) are not enough.
This Python module for [*micropython*](https://micropython.org/) and [*CircuitPython*](https://circuitpython.org/) provides support for decimal floating point arithmetic. It tries to overcome the limitations of single precision float numbers (32-bit) and provides a solution when double precision float numbers (64 bit) are not enough.

The Python Standard Library contains the wonderful module [*decimal*](https://docs.python.org/3/library/decimal.html), but it has not been ported to *micropython*. This module provides a small, but valuable, part of the functionality of *decimal*.

Expand Down
7 changes: 5 additions & 2 deletions mpy_decimal/mpy_decimal.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
from typing import Tuple
if sys.implementation.name == "micropython": # Just in case...
pass
if sys.implementation.name == "circuitpython": # Also just in case...
pass


class DecimalNumber:
"""DecimalNumber is a class for decimal floating point arithmetic with arbitrary precision."""
VERSION = (1, 0, 0)
VERSION_NAME = "v1.0.0 - August 2021"
VERSION = (1, 0, 1)
VERSION_NAME = "v1.0.1 - September 2025"
DEFAULT_SCALE: int = 16
DECIMAL_SEP: str = "."
THOUSANDS_SEP: str = ","
Expand Down Expand Up @@ -1101,3 +1103,4 @@ def __str__(self) -> str:

if __name__ == "__main__":
print("DecimalNumber module -", DecimalNumber.VERSION)

10 changes: 10 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"urls": [
[
"decimal.py",
"github:scruss/micropython-decimal/mpy_decimal/decimal.py"
]
],
"deps": [],
"version": "1.0.1"
}
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

setup (
name = 'Micropython DecimalNumber',
version = '1.0',
version = '1.0.1',
packages = find_packages()
)
16 changes: 14 additions & 2 deletions tests/perf_decimal_number.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@
pi_decimals: int = 1000
if sys.implementation.name == "micropython":
import machine
import utime
import time
iteration_limit: int = 1000
iteration_limit2: int = 400
pi_decimals: int = 300
if sys.implementation.name == "circuitpython":
import microcontroller
import supervisor
iteration_limit: int = 1000
iteration_limit2: int = 400
pi_decimals: int = 300
Expand All @@ -32,14 +38,19 @@ def system_machine_info() -> None:
if sys.implementation.name == "micropython":
print(format_str.format("CPU frequency:"),
machine.freq() // 1000000, "Mhz")
if sys.implementation.name == "circuitpython":
print(format_str.format("CPU frequency:"),
microcontroller.cpu.frequency // 1000000, "Mhz")

def get_time_ms() -> int:
"""It gets the time in miliseconds.
The way to get it depends on the implementation."""
if sys.implementation.name == "cpython":
return round(time.time() * 1000)
if sys.implementation.name == "micropython":
return utime.ticks_ms()
return time.ticks_ms()
if sys.implementation.name == "circuitpython":
return supervisor.ticks_ms()

def gen_random_number() -> DecimalNumber:
"""Generates a random number with a number of decimals equal to scale.
Expand Down Expand Up @@ -249,3 +260,4 @@ def print_title(title: str) -> None:

print_title("CALCULATING PI")
perf_decimal_number_pi()

3 changes: 3 additions & 0 deletions tests/test_decimal_number.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import traceback
if sys.implementation.name == "micropython":
pass
if sys.implementation.name == "circuitpython":
pass


class TestDecimalNumber():
Expand Down Expand Up @@ -1407,3 +1409,4 @@ def test_atan2(self) -> bool:
print("Result: 1 test failed")
else:
print("Result: {0} tests failed".format(failed_counter))