Skip to content
Merged
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
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@ This repository includes [TOMLMojo](https://github.com/forfudan/decimojo/tree/ma
| ------------ | ------- | ------------------------------------ | ----------------------------------- |
| `BigUInt` | `BUInt` | arbitrary-precision unsigned integer | `List[UInt32]` |
| `BigInt` | `BInt` | arbitrary-precision integer | `BigUInt`, `Bool` |
| `Decimal` | `Dec` | 128-bit fixed-precision decimal | `UInt32`,`UInt32`,`UInt32`,`UInt32` |
| `BigDecimal` | `BDec` | arbitrary-precision decimal | `BigUInt`, `Int`, `Bool` |
| `Decimal` | `Dec` | 128-bit fixed-precision decimal | `UInt32`,`UInt32`,`UInt32`,`UInt32` |

## Installation

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

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

For projects with a `mojoproject.toml`file, add the dependency ```decimojo = ">=0.3.1"```. Then run `pixi install` to download and install the package.
For projects with a `mojoproject.toml`file, add the dependency ```decimojo = "==0.4.0"```. Then run `pixi install` to download and install the package.

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

Expand All @@ -49,6 +49,7 @@ For the latest development version, clone the [GitHub repository](https://github
| v0.2.0 | ==25.2 | magic |
| v0.3.0 | ==25.2 | magic |
| v0.3.1 | >=25.2, <25.4 | pixi |
| v0.4.0 | ==25.4 | pixi |

## Quick start

Expand Down Expand Up @@ -217,7 +218,7 @@ If you find DeciMojo useful for your research, consider listing it in your citat
year = {2025},
title = {An arbitrary-precision decimal and integer mathematics library for Mojo},
url = {https://github.com/forfudan/decimojo},
version = {0.3.1},
version = {0.4.0},
note = {Computer Software}
}
```
Expand Down
4 changes: 3 additions & 1 deletion benches/bigdecimal/bench_bigdecimal_add.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ fn open_log_file() raises -> PythonObject:

# Generate a timestamp for the filename
var timestamp = String(datetime.datetime.now().isoformat())
var log_filename = log_dir + "/benchmark_bigdecimal_add_" + timestamp + ".log"
var log_filename = (
log_dir + "/benchmark_bigdecimal_add_" + timestamp + ".log"
)

print("Saving benchmark results to:", log_filename)
return python.open(log_filename, "w")
Expand Down
4 changes: 3 additions & 1 deletion benches/bigdecimal/bench_bigdecimal_divide.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ fn open_log_file() raises -> PythonObject:

# Generate a timestamp for the filename
var timestamp = String(datetime.datetime.now().isoformat())
var log_filename = log_dir + "/benchmark_bigdecimal_divide_" + timestamp + ".log"
var log_filename = (
log_dir + "/benchmark_bigdecimal_divide_" + timestamp + ".log"
)

print("Saving benchmark results to:", log_filename)
return python.open(log_filename, "w")
Expand Down
4 changes: 3 additions & 1 deletion benches/bigdecimal/bench_bigdecimal_exp.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ fn open_log_file() raises -> PythonObject:

# Generate a timestamp for the filename
var timestamp = String(datetime.datetime.now().isoformat())
var log_filename = log_dir + "/benchmark_bigdecimal_exp_" + timestamp + ".log"
var log_filename = (
log_dir + "/benchmark_bigdecimal_exp_" + timestamp + ".log"
)

print("Saving benchmark results to:", log_filename)
return python.open(log_filename, "w")
Expand Down
4 changes: 3 additions & 1 deletion benches/bigdecimal/bench_bigdecimal_ln.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ fn open_log_file() raises -> PythonObject:

# Generate a timestamp for the filename
var timestamp = String(datetime.datetime.now().isoformat())
var log_filename = log_dir + "/benchmark_bigdecimal_ln_" + timestamp + ".log"
var log_filename = (
log_dir + "/benchmark_bigdecimal_ln_" + timestamp + ".log"
)

print("Saving benchmark results to:", log_filename)
return python.open(log_filename, "w")
Expand Down
4 changes: 3 additions & 1 deletion benches/bigdecimal/bench_bigdecimal_multiply.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ fn open_log_file() raises -> PythonObject:

# Generate a timestamp for the filename
var timestamp = String(datetime.datetime.now().isoformat())
var log_filename = log_dir + "/benchmark_bigdecimal_multiply_" + timestamp + ".log"
var log_filename = (
log_dir + "/benchmark_bigdecimal_multiply_" + timestamp + ".log"
)

print("Saving benchmark results to:", log_filename)
return python.open(log_filename, "w")
Expand Down
4 changes: 3 additions & 1 deletion benches/bigdecimal/bench_bigdecimal_root.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ fn open_log_file() raises -> PythonObject:

# Generate a timestamp for the filename
var timestamp = String(datetime.datetime.now().isoformat())
var log_filename = log_dir + "/benchmark_bigdecimal_root_" + timestamp + ".log"
var log_filename = (
log_dir + "/benchmark_bigdecimal_root_" + timestamp + ".log"
)

print("Saving benchmark results to:", log_filename)
return python.open(log_filename, "w")
Expand Down
4 changes: 3 additions & 1 deletion benches/bigdecimal/bench_bigdecimal_round.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ fn open_log_file() raises -> PythonObject:

# Generate a timestamp for the filename
var timestamp = String(datetime.datetime.now().isoformat())
var log_filename = log_dir + "/benchmark_bigdecimal_round_" + timestamp + ".log"
var log_filename = (
log_dir + "/benchmark_bigdecimal_round_" + timestamp + ".log"
)

print("Saving benchmark results to:", log_filename)
return python.open(log_filename, "w")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ fn open_log_file() raises -> PythonObject:

# Generate a timestamp for the filename
var timestamp = String(datetime.datetime.now().isoformat())
var log_filename = log_dir + "/benchmark_biguint_scale_up_" + timestamp + ".log"
var log_filename = (
log_dir + "/benchmark_biguint_scale_up_" + timestamp + ".log"
)

print("Saving benchmark results to:", log_filename)
return python.open(log_filename, "w")
Expand Down
4 changes: 3 additions & 1 deletion benches/bigdecimal/bench_bigdecimal_sqrt.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ fn open_log_file() raises -> PythonObject:

# Generate a timestamp for the filename
var timestamp = String(datetime.datetime.now().isoformat())
var log_filename = log_dir + "/benchmark_bigdecimal_sqrt_" + timestamp + ".log"
var log_filename = (
log_dir + "/benchmark_bigdecimal_sqrt_" + timestamp + ".log"
)

print("Saving benchmark results to:", log_filename)
return python.open(log_filename, "w")
Expand Down
4 changes: 3 additions & 1 deletion benches/bigdecimal/bench_bigdecimal_subtract.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ fn open_log_file() raises -> PythonObject:

# Generate a timestamp for the filename
var timestamp = String(datetime.datetime.now().isoformat())
var log_filename = log_dir + "/benchmark_bigdecimal_subtract_" + timestamp + ".log"
var log_filename = (
log_dir + "/benchmark_bigdecimal_subtract_" + timestamp + ".log"
)

print("Saving benchmark results to:", log_filename)
return python.open(log_filename, "w")
Expand Down
4 changes: 3 additions & 1 deletion benches/bigdecimal/bench_scale_up_by_power_of_10.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ fn open_log_file() raises -> PythonObject:

# Generate a timestamp for the filename
var timestamp = String(datetime.datetime.now().isoformat())
var log_filename = log_dir + "/benchmark_biguint_scale_up_" + timestamp + ".log"
var log_filename = (
log_dir + "/benchmark_biguint_scale_up_" + timestamp + ".log"
)

print("Saving benchmark results to:", log_filename)
return python.open(log_filename, "w")
Expand Down
4 changes: 3 additions & 1 deletion benches/bigint/bench_bigint_floor_divide.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ fn open_log_file() raises -> PythonObject:

# Generate a timestamp for the filename
var timestamp = String(datetime.datetime.now().isoformat())
var log_filename = log_dir + "/benchmark_bigint_floor_divide_" + timestamp + ".log"
var log_filename = (
log_dir + "/benchmark_bigint_floor_divide_" + timestamp + ".log"
)

print("Saving benchmark results to:", log_filename)
return python.open(log_filename, "w")
Expand Down
4 changes: 3 additions & 1 deletion benches/bigint/bench_bigint_multiply.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ fn open_log_file() raises -> PythonObject:

# Generate a timestamp for the filename
var timestamp = String(datetime.datetime.now().isoformat())
var log_filename = log_dir + "/benchmark_bigint_multiply_" + timestamp + ".log"
var log_filename = (
log_dir + "/benchmark_bigint_multiply_" + timestamp + ".log"
)

print("Saving benchmark results to:", log_filename)
return python.open(log_filename, "w")
Expand Down
4 changes: 3 additions & 1 deletion benches/bigint/bench_bigint_truncate_divide.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ fn open_log_file() raises -> PythonObject:

# Generate a timestamp for the filename
var timestamp = String(datetime.datetime.now().isoformat())
var log_filename = log_dir + "/benchmark_bigint_truncate_divide_" + timestamp + ".log"
var log_filename = (
log_dir + "/benchmark_bigint_truncate_divide_" + timestamp + ".log"
)

print("Saving benchmark results to:", log_filename)
return python.open(log_filename, "w")
Expand Down
4 changes: 3 additions & 1 deletion benches/biguint/bench_biguint_from_string.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ fn open_log_file() raises -> PythonObject:

# Generate a timestamp for the filename
var timestamp = String(datetime.datetime.now().isoformat())
var log_filename = log_dir + "/benchmark_biguint_from_string_" + timestamp + ".log"
var log_filename = (
log_dir + "/benchmark_biguint_from_string_" + timestamp + ".log"
)

print("Saving benchmark results to:", log_filename)
return python.open(log_filename, "w")
Expand Down
4 changes: 3 additions & 1 deletion benches/biguint/bench_biguint_multiply.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ fn open_log_file() raises -> PythonObject:

# Generate a timestamp for the filename
var timestamp = String(datetime.datetime.now().isoformat())
var log_filename = log_dir + "/benchmark_biguint_multiply_" + timestamp + ".log"
var log_filename = (
log_dir + "/benchmark_biguint_multiply_" + timestamp + ".log"
)

print("Saving benchmark results to:", log_filename)
return python.open(log_filename, "w")
Expand Down
4 changes: 3 additions & 1 deletion benches/biguint/bench_biguint_truncate_divide.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ fn open_log_file() raises -> PythonObject:

# Generate a timestamp for the filename
var timestamp = String(datetime.datetime.now().isoformat())
var log_filename = log_dir + "/benchmark_biguint_truncate_divide_" + timestamp + ".log"
var log_filename = (
log_dir + "/benchmark_biguint_truncate_divide_" + timestamp + ".log"
)

print("Saving benchmark results to:", log_filename)
return python.open(log_filename, "w")
Expand Down
4 changes: 3 additions & 1 deletion benches/decimal/bench_truncate_divide.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ fn open_log_file() raises -> PythonObject:

# Generate a timestamp for the filename
var timestamp = String(datetime.datetime.now().isoformat())
var log_filename = log_dir + "/benchmark_truncate_divide_" + timestamp + ".log"
var log_filename = (
log_dir + "/benchmark_truncate_divide_" + timestamp + ".log"
)

print("Saving benchmark results to:", log_filename)
return python.open(log_filename, "w")
Expand Down
6 changes: 5 additions & 1 deletion docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@

This is a list of RELEASED changes for the DeciMojo Package. For the unreleased changes, please refer to **[changelog_unreleased](https://zhuyuhao.com/decimojo/docs/changelog_unreleased.html)**.

## 25/06/2025 (v0.4.0)

DeciMojo v0.4.0 updates the codebase to Mojo v25.4. This release enables you to use DeciMojo with the latest Mojo features.

## 06/06/2025 (v0.3.1)

DeciMojo v0.3.1 updates the codebase to Mojo 25.3 and replaces the `magic` package manager with `pixi`. This release enables you to use DeciMojo with the latest Mojo features and the new package manager.
DeciMojo v0.3.1 updates the codebase to Mojo v25.3 and replaces the `magic` package manager with `pixi`. This release enables you to use DeciMojo with the latest Mojo features and the new package manager.

## 15/04/2025 (v0.3.0)

Expand Down
5 changes: 3 additions & 2 deletions docs/readme_zht.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ DeciMojo 可在 [modular-community](https://repo.prefix.dev/modular-community)

從 `pixi` CLI,只需運行 ```pixi add decimojo```。這會獲取最新版本並使其立即可用於導入。

對於帶有 `mojoproject.toml` 文件的項目,添加依賴 ```decimojo = ">=0.3.1"```。然後運行 `pixi install` 來下載並安裝包。
對於帶有 `mojoproject.toml` 文件的項目,添加依賴 ```decimojo = "==0.4.0"```。然後運行 `pixi install` 來下載並安裝包。

如需最新的開發版本,請克隆 [GitHub 倉庫](https://github.com/forfudan/decimojo) 並在本地構建包。

Expand All @@ -30,6 +30,7 @@ DeciMojo 可在 [modular-community](https://repo.prefix.dev/modular-community)
| v0.2.0 | >=25.2 | magic |
| v0.3.0 | >=25.2 | magic |
| v0.3.1 | >=25.2, <25.4 | pixi |
| v0.4.0 | ==25.4 | pixi |

## 快速入門

Expand Down Expand Up @@ -217,7 +218,7 @@ DeciMojo 相較於 Python 的 `decimal` 模塊提供了卓越的性能,同時
year = {2025},
title = {DeciMojo: A fixed-point decimal arithmetic library in Mojo},
url = {https://github.com/forfudan/decimojo},
version = {0.3.1},
version = {0.4.0},
note = {Computer Software}
}
```
Expand Down
4 changes: 2 additions & 2 deletions pixi.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ license = "Apache-2.0"
name = "decimojo"
platforms = ["osx-arm64", "linux-64"]
readme = "README.md"
version = "0.3.1"
version = "0.4.0"

[dependencies]
max = ">=25.2, <25.4"
max = "==25.4"

[tasks]
# format the code
Expand Down
12 changes: 8 additions & 4 deletions src/decimojo/bigdecimal/bigdecimal.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ alias BDec = BigDecimal


@value
struct BigDecimal(Absable, Comparable, IntableRaising, Roundable, Writable):
struct BigDecimal(
Absable, Comparable, IntableRaising, Roundable, Stringable, Writable
):
"""Represents a arbitrary-precision decimal.

Notes:
Expand Down Expand Up @@ -257,13 +259,13 @@ struct BigDecimal(Absable, Comparable, IntableRaising, Roundable, Writable):
start = end - 9
var word: UInt32 = 0
for digit in coef[start:end]:
word = word * 10 + UInt32(digit[])
word = word * 10 + UInt32(digit)
coefficient_words.append(word)
end = start
if end > 0:
var word: UInt32 = 0
for digit in coef[0:end]:
word = word * 10 + UInt32(digit[])
word = word * 10 + UInt32(digit)
coefficient_words.append(word)

coefficient = BigUInt(coefficient_words^)
Expand Down Expand Up @@ -871,7 +873,9 @@ struct BigDecimal(Absable, Comparable, IntableRaising, Roundable, Writable):
var number_of_digits_to_remove = self.number_of_trailing_zeros()

var number_of_words_to_remove = number_of_digits_to_remove // 9
var number_of_remaining_digits_to_remove = number_of_digits_to_remove % 9
var number_of_remaining_digits_to_remove = (
number_of_digits_to_remove % 9
)

words = self.coefficient.words[number_of_words_to_remove:]
var coefficient = BigUInt(words^)
Expand Down
22 changes: 13 additions & 9 deletions src/decimojo/bigdecimal/exponential.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -551,15 +551,19 @@ fn sqrt(x: BigDecimal, precision: Int) raises -> BigDecimal:
# TODO: This can be done even earlier in the process
# TODO: Implement a method that remove trailing zeros
if guess.coefficient.ith_digit(0) == 0:
var guess_coefficient_without_trailing_zeros = guess.coefficient.remove_trailing_digits_with_rounding(
guess.coefficient.number_of_trailing_zeros(),
rounding_mode=RoundingMode.ROUND_DOWN,
remove_extra_digit_due_to_rounding=False,
var guess_coefficient_without_trailing_zeros = (
guess.coefficient.remove_trailing_digits_with_rounding(
guess.coefficient.number_of_trailing_zeros(),
rounding_mode=RoundingMode.ROUND_DOWN,
remove_extra_digit_due_to_rounding=False,
)
)
var x_coefficient_without_trailing_zeros = x.coefficient.remove_trailing_digits_with_rounding(
x.coefficient.number_of_trailing_zeros(),
rounding_mode=RoundingMode.ROUND_DOWN,
remove_extra_digit_due_to_rounding=False,
var x_coefficient_without_trailing_zeros = (
x.coefficient.remove_trailing_digits_with_rounding(
x.coefficient.number_of_trailing_zeros(),
rounding_mode=RoundingMode.ROUND_DOWN,
remove_extra_digit_due_to_rounding=False,
)
)
if (
guess_coefficient_without_trailing_zeros
Expand Down Expand Up @@ -1036,7 +1040,7 @@ fn ln_series_expansion(
k += BigUInt.ONE

# Alternate sign: -1^(k+1) = -1 when k is even, 1 when k is odd
var sign = (k % BigUInt(2) == BigUInt.ZERO)
var sign = k % BigUInt(2) == BigUInt.ZERO
var next_term = term.true_divide_inexact(
BigDecimal(k, 0, False), working_precision
)
Expand Down
2 changes: 1 addition & 1 deletion src/decimojo/bigint/bigint.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ alias BInt = BigInt


@value
struct BigInt(Absable, IntableRaising, Writable):
struct BigInt(Absable, IntableRaising, Representable, Stringable, Writable):
"""Represents a base-10 arbitrary-precision signed integer.

Notes:
Expand Down
Loading