You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This repository includes [TOMLMojo](https://github.com/forfudan/decimojo/tree/main/src/tomlmojo), a lightweight TOML parser in pure Mojo. It parses configuration files and test data, supporting basic types, arrays, and nested tables. While created for DeciMojo's testing framework, it offers general-purpose structured data parsing with a clean, simple API.
20
20
21
-
| type | alias | information | internal representation |
DeciMojo is available in the [modular-community](https://repo.prefix.dev/modular-community) package repository. You can install it using any of these methods:
31
31
32
-
From the `magic` CLI, simply run ```magic add decimojo```. This fetches the latest version and makes it immediately available for import.
32
+
1.From the `pixi` CLI, run the command ```pixi add decimojo```. This fetches the latest version and makes it immediately available for import.
33
33
34
-
For projects with a `mojoproject.toml`file, add the dependency ```decimojo = ">=0.3.0"```. Then run `magic install` to download and install the package.
34
+
1. In the `mojoproject.toml`file of your project, add the following dependency:
35
35
36
-
For the latest development version, clone the [GitHub repository](https://github.com/forfudan/decimojo) and build the package locally.
36
+
```toml
37
+
decimojo = "==0.6.0"
38
+
```
37
39
38
-
|`decimojo`|`mojo`|
39
-
| ---------- | ------ |
40
-
| v0.1.0 | >=25.1 |
41
-
| v0.2.0 | >=25.2 |
42
-
| v0.3.0 | >=25.2 |
40
+
Then run `pixi install` to download and install the package.
41
+
42
+
1. For the latest development version in the `main` branch, clone [this GitHub repository](https://github.com/forfudan/decimojo) and build the package locally using the command `pixi run package`.
43
+
44
+
The following table summarizes the package versions and their corresponding Mojo versions:
45
+
46
+
| `decimojo` | `mojo` | package manager |
47
+
| ---------- | ------------- | --------------- |
48
+
| v0.1.0 | ==25.1 | magic |
49
+
| v0.2.0 | ==25.2 | magic |
50
+
| v0.3.0 | ==25.2 | magic |
51
+
| v0.3.1 | >=25.2, <25.4 | pixi |
52
+
| v0.4.x | ==25.4 | pixi |
53
+
| v0.5.0 | ==25.5 | pixi |
54
+
| v0.6.0 | ==0.25.7 | pixi |
43
55
44
56
## Quick start
45
57
46
-
Here is a comprehensive quick-start guide showcasing each major function of the `Decimal` type.
58
+
You can start using DeciMojo by importing the `decimojo` module. An easy way to do this is to import everything from the `prelude` module, which provides the most commonly used types.
47
59
48
60
```mojo
49
-
from decimojo import Decimal, RoundingMode
50
-
51
-
fn main() raises:
52
-
# === Construction ===
53
-
var a = Decimal("123.45") # From string
54
-
var b = Decimal(123) # From integer
55
-
var c = Decimal(123, 2) # Integer with scale (1.23)
56
-
var d = Decimal.from_float(3.14159) # From floating-point
57
-
58
-
# === Basic Arithmetic ===
59
-
print(a + b) # Addition: 246.45
60
-
print(a - b) # Subtraction: 0.45
61
-
print(a * b) # Multiplication: 15184.35
62
-
print(a / b) # Division: 1.0036585365853658536585365854
63
-
64
-
# === Rounding & Precision ===
65
-
print(a.round(1)) # Round to 1 decimal place: 123.5
66
-
print(a.quantize(Decimal("0.01"))) # Format to 2 decimal places: 123.45
67
-
print(a.round(0, RoundingMode.ROUND_DOWN)) # Round down to integer: 123
68
-
69
-
# === Comparison ===
70
-
print(a > b) # Greater than: True
71
-
print(a == Decimal("123.45")) # Equality: True
72
-
print(a.is_zero()) # Check for zero: False
73
-
print(Decimal("0").is_zero()) # Check for zero: True
[Click here for 8 key examples](https://zhuyuhao.com/decimojo/docs/examples) highlighting the most important features of the `Decimal` type.
64
+
This will import the following types or aliases into your namespace:
65
+
66
+
-`dm`: An alias for the `decimojo` module.
67
+
-`BigInt` (alias `BInt`): An arbitrary-precision signed integer type.
68
+
-`BigDecimal` (aliases `BDec` and `Decimal`): An arbitrary-precision decimal type.
69
+
-`Decimal128` (alias `Dec128`): A 128-bit fixed-precision decimal type.
70
+
-`RoundingMode` (alias `RM`): An enumeration for rounding modes.
71
+
-`ROUND_DOWN`, `ROUND_HALF_UP`, `ROUND_HALF_EVEN`, `ROUND_UP`: Constants for common rounding modes.
110
72
111
-
Here are some examples showcasing the arbitrary-precision feature of the `BigDecimal` type.
73
+
---
74
+
75
+
Here are some examples showcasing the arbitrary-precision feature of the `BigDecimal` type (aliases: `BDec` and `Decimal`). For some mathematical operations, the default precision (number of significant digits) is set to `36`. You can change the precision by passing the `precision` argument to the function. This default precision will be configurable globally in future when Mojo supports global variables.
Financial calculations and data analysis require precise decimal arithmetic that floating-point numbers cannot reliably provide. As someone working in finance and credit risk model validation, I needed a dependable correctly-rounded, fixed-precision numeric type when migrating my personal projects from Python to Mojo.
@@ -191,28 +280,6 @@ Rome wasn't built in a day. DeciMojo is currently under active development. It h
191
280
192
281
Regular benchmarks against Python's `decimal` module are available in the `bench/` folder, documenting both the performance advantages and the few specific operations where different approaches are needed.
193
282
194
-
## Tests and benches
195
-
196
-
After cloning the repo onto your local disk, you can:
197
-
198
-
- Use `magic run test` to run tests.
199
-
- Use `magic run bench_decimal` to generate logs for benchmarking tests against `python.decimal` module. The log files are saved in `benches/decimal/logs/`.
200
-
201
-
## Citation
202
-
203
-
If you find DeciMojo useful for your research, consider listing it in your citations.
204
-
205
-
```tex
206
-
@software{Zhu.2025,
207
-
author = {Zhu, Yuhao},
208
-
year = {2025},
209
-
title = {An arbitrary-precision decimal and integer mathematics library for Mojo},
210
-
url = {https://github.com/forfudan/decimojo},
211
-
version = {0.3.0},
212
-
note = {Computer Software}
213
-
}
214
-
```
215
-
216
283
## License
217
284
218
285
This repository and its contributions are licensed under the Apache License v2.0.
0 commit comments