Skip to content

Commit 97df994

Browse files
authored
Update decimojo to v0.3.0 (#105)
1 parent a420ebb commit 97df994

File tree

2 files changed

+40
-50
lines changed

2 files changed

+40
-50
lines changed

recipes/decimojo/README.md

Lines changed: 37 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,45 @@
1-
# DeciMojo
1+
# DeciMojo <!-- omit in toc -->
22

33
![icon](icon_256x256.png)
44

5-
A comprehensive decimal and integer mathematics library for [Mojo](https://www.modular.com/mojo).
5+
An arbitrary-precision decimal and integer mathematics library for [Mojo](https://www.modular.com/mojo).
66

7-
**[中文·漢字»](https://zhuyuhao.com/decimojo/docs/readme_zht.html)** | **[Repository on GitHub»](https://github.com/forfudan/decimojo)**
7+
**[中文·漢字»](https://zhuyuhao.com/decimojo/docs/readme_zht.html)** | **[Repository on GitHub»](https://github.com/forfudan/decimojo)** | **[Changelog](https://zhuyuhao.com/decimojo/docs/changelog.html)**
88

99
## Overview
1010

11-
DeciMojo provides a comprehensive decimal and integer mathematics library for Mojo, delivering exact calculations for financial modeling, scientific computing, and applications where floating-point approximation errors are unacceptable. Beyond basic arithmetic, the library includes advanced mathematical functions with guaranteed precision.
11+
DeciMojo provides an arbitrary-precision decimal and integer mathematics library for Mojo, delivering exact calculations for financial modeling, scientific computing, and applications where floating-point approximation errors are unacceptable. Beyond basic arithmetic, the library includes advanced mathematical functions with guaranteed precision.
1212

1313
The core types are:
1414

15-
- A 128-bit fixed-point decimal implementation (`Decimal`) supporting up to 29 significant digits with a maximum of 28 decimal places[^fixed]. It features a complete set of mathematical functions including logarithms, exponentiation, roots, and trigonometric operations.
15+
- A 128-bit fixed-point decimal implementation (`Decimal`) supporting up to 29 significant digits with a maximum of 28 decimal places[^fixed]. It features a complete set of mathematical functions including logarithms, exponentiation, roots, etc.
16+
- An arbitrary-precision decimal implementation `BigDecimal` allowing for calculations with unlimited digits and decimal places[^arbitrary].
1617
- A base-10 arbitrary-precision signed integer type (`BigInt`) and a base-10 arbitrary-precision unsigned integer type (`BigUInt`) supporting unlimited digits[^integer]. It features comprehensive arithmetic operations, comparison functions, and supports extremely large integer calculations efficiently.
1718

18-
The library is expanding to include `BigDecimal` types that support arbitrary precision[^arbitrary], allowing for calculations with unlimited digits and decimal places. These extensions are currently under active development.
19+
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+
21+
| type | alias | information | internal representation |
22+
| ------------ | ------- | ------------------------------------ | ----------------------------------- |
23+
| `BigUInt` | `BUInt` | arbitrary-precision unsigned integer | `List[UInt32]` |
24+
| `BigInt` | `BInt` | arbitrary-precision integer | `BigUInt`, `Bool` |
25+
| `Decimal` | `Dec` | 128-bit fixed-precision decimal | `UInt32`,`UInt32`,`UInt32`,`UInt32` |
26+
| `BigDecimal` | `BDec` | arbitrary-precision decimal | `BigUInt`, `Int`, `Bool` |
1927

2028
## Installation
2129

2230
DeciMojo is available in the [modular-community](https://repo.prefix.dev/modular-community) package repository. You can install it using any of these methods:
2331

2432
From the `magic` CLI, simply run ```magic add decimojo```. This fetches the latest version and makes it immediately available for import.
2533

26-
For projects with a `mojoproject.toml`file, add the dependency ```decimojo = ">=0.2.0"```. Then run `magic install` to download and install the package.
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.
2735

2836
For the latest development version, clone the [GitHub repository](https://github.com/forfudan/decimojo) and build the package locally.
2937

3038
| `decimojo` | `mojo` |
3139
| ---------- | ------ |
3240
| v0.1.0 | >=25.1 |
3341
| v0.2.0 | >=25.2 |
42+
| v0.3.0 | >=25.2 |
3443

3544
## Quick start
3645

@@ -99,6 +108,24 @@ fn main() raises:
99108

100109
[Click here for 8 key examples](https://zhuyuhao.com/decimojo/docs/examples) highlighting the most important features of the `Decimal` type.
101110

111+
Here are some examples showcasing the arbitrary-precision feature of the `BigDecimal` type.
112+
113+
```mojo
114+
from decimojo import BDec, RM
115+
116+
117+
fn main() raises:
118+
var PRECISION = 100
119+
var a = BDec("123456789.123456789")
120+
var b = BDec("1234.56789")
121+
print(a.sqrt(precision=PRECISION))
122+
# 11111.11106611111096943055498174930232833813065468909453818857935956641682120364106016272519460988485
123+
print(a.power(b, precision=PRECISION))
124+
# 3.346361102419080234023813540078946868219632448203078657310495672766009862564151996325555496759911131748170844123475135377098326591508239654961E+9989
125+
print(a.log(b, precision=PRECISION))
126+
# 2.617330026656548299907884356415293977170848626010103229392408225981962436022623783231699264341492663671325580092077394824180414301026578169909
127+
```
128+
102129
Here is a comprehensive quick-start guide showcasing each major function of the `BigInt` type.
103130

104131
```mojo
@@ -160,47 +187,10 @@ The name ultimately emphasizes our mission: bringing precise, reliable decimal c
160187

161188
## Status
162189

163-
Rome wasn't built in a day. DeciMojo is currently under active development. For the 128-bit `Decimal` type, it has successfully progressed through the **"make it work"** phase and is now well into the **"make it right"** phase with many optimizations already in place. Bug reports and feature requests are welcome! If you encounter issues, please [file them here](https://github.com/forfudan/decimojo/issues).
164-
165-
### Make it Work ✅ (COMPLETED)
166-
167-
- Core decimal implementation with a robust 128-bit representation (96-bit coefficient + 32-bit flags)
168-
- Comprehensive arithmetic operations (+, -, *, /, %, **) with proper overflow handling
169-
- Type conversions to/from various formats (String, Int, Float64, etc.)
170-
- Proper representation of special values (NaN, Infinity)
171-
- Full suite of comparison operators with correct decimal semantics
172-
173-
### Make it Right 🔄 (MOSTLY COMPLETED)
174-
175-
- Reorganized codebase with modular structure (decimal, arithmetics, comparison, exponential).
176-
- Edge case handling for all operations (division by zero, zero to negative power).
177-
- Scale and precision management with sophisticated rounding strategies.
178-
- Financial calculations with banker's rounding (ROUND_HALF_EVEN).
179-
- High-precision advanced mathematical functions (sqrt, root, ln, exp, log10, power).
180-
- Proper implementation of traits (Absable, Comparable, Floatable, Roundable, etc).
181-
- **BigInt and BigUInt** implementations with complete arithmetic operations, proper division semantics (floor and truncate), and support for arbitrary-precision calculations.
182-
183-
### Make it Fast ⚡ (SIGNIFICANT PROGRESS)
184-
185-
DeciMojo delivers exceptional performance compared to Python's `decimal` module while maintaining precise calculations. This performance difference stems from fundamental design choices:
186-
187-
- **DeciMojo**: Uses a fixed 128-bit representation (96-bit coefficient + 32-bit flags) with a maximum of 28 decimal places, optimized for modern hardware and Mojo's performance capabilities.
188-
- **Python decimal**: Implements arbitrary precision that can represent numbers with unlimited significant digits but requires dynamic memory allocation and more complex algorithms.
189-
190-
This architectural difference explains our benchmarking results:
191-
192-
- Core arithmetic operations (+, -, *, /) achieve 100x-3500x speedup over Python's decimal module.
193-
- Special case handling (powers of 0, 1, etc.) shows up to 3500x performance improvement.
194-
- Advanced mathematical functions (sqrt, ln, exp) demonstrate 5x-600x better performance.
195-
- Only specific edge cases (like computing 10^(1/100)) occasionally perform better in Python due to its arbitrary precision algorithms.
190+
Rome wasn't built in a day. DeciMojo is currently under active development. It has successfully progressed through the **"make it work"** phase and is now well into the **"make it right"** phase with many optimizations already in place. Bug reports and feature requests are welcome! If you encounter issues, please [file them here](https://github.com/forfudan/decimojo/issues).
196191

197192
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.
198193

199-
### Future Extensions 🚀 (PLANNED)
200-
201-
- **BigDecimal**: 🔄 **IN PROGRESS** - Arbitrary-precision decimal type with configurable precision[^arbitrary].
202-
- **BigComplex**: 📝 **PLANNED** - Arbitrary-precision complex number type built on BigDecimal.
203-
204194
## Tests and benches
205195

206196
After cloning the repo onto your local disk, you can:
@@ -216,9 +206,9 @@ If you find DeciMojo useful for your research, consider listing it in your citat
216206
@software{Zhu.2025,
217207
author = {Zhu, Yuhao},
218208
year = {2025},
219-
title = {A comprehensive decimal and integer mathematics library for Mojo},
209+
title = {An arbitrary-precision decimal and integer mathematics library for Mojo},
220210
url = {https://github.com/forfudan/decimojo},
221-
version = {0.2.0},
211+
version = {0.3.0},
222212
note = {Computer Software}
223213
}
224214
```

recipes/decimojo/recipe.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
context:
2-
version: "0.2.0"
2+
version: "0.3.0"
33

44
package:
55
name: "decimojo"
66
version: ${{ version }}
77

88
source:
99
- git: https://github.com/forFudan/decimojo.git
10-
rev: fba2fa810382d34f36907b18312340ff730bcec8
10+
rev: f2930f0832f1096207cb93bb2cc52b028b06b429
1111

1212
build:
1313
number: 0
@@ -35,7 +35,7 @@ about:
3535
homepage: https://github.com/forFudan/decimojo.git
3636
license: Apache-2.0
3737
license_file: LICENSE
38-
summary: A comprehensive decimal and integer mathematics library for Mojo
38+
summary: An arbitrary-precision decimal and integer mathematics library for Mojo
3939
repository: https://github.com/forFudan/decimojo.git
4040

4141
extra:

0 commit comments

Comments
 (0)