2.6 KiB
2.6 KiB
Steel Decimal
A Rust library that provides high-precision decimal arithmetic support for the Steel programming language. This crate transforms Steel scripts to use decimal operations and provides function registration utilities for Steel VMs.
Features
- High-precision decimal arithmetic using
rust_decimalcrate - Automatic script transformation from standard math to decimal operations
- Comprehensive mathematical functions (basic arithmetic, trigonometry, logarithms)
- Financial calculations with proper decimal precision
- Configurable precision control
- Selective function registration via builder pattern
- Variable support with validation
- Thread-safe precision contexts
Quick Start
Add this to your Cargo.toml:
[dependencies]
steel_decimal = "1.0.0"
Supported Operations
Basic Arithmetic
- Addition:
(+ a b)→(decimal-add "a" "b") - Subtraction:
(- a b)→(decimal-sub "a" "b") - Multiplication:
(* a b)→(decimal-mul "a" "b") - Division:
(/ a b)→(decimal-div "a" "b")
Advanced Mathematics
- Power:
(^ base exp)→(decimal-pow "base" "exp") - Square root:
(sqrt x)→(decimal-sqrt "x") - Natural log:
(ln x)→(decimal-ln "x") - Base 10 log:
(log10 x)→(decimal-log10 "x") - Exponential:
(exp x)→(decimal-exp "x")
Trigonometric Functions
(sin x),(cos x),(tan x)
Comparison Operations
(> a b),(< a b),(= a b),(>= a b),(<= a b)
Financial Functions
- Percentage:
(decimal-percentage amount rate) - Compound interest:
(decimal-compound principal rate time)
Precision Control
- Set precision:
(set-precision 4) - Get current precision:
(get-precision) - Clear precision:
(clear-precision)
Examples
The crate includes several examples:
# Basic usage
cargo run --example basic_usage
# Selective function registration
cargo run --example selective_registration
# Working with variables
cargo run --example with_variables
Scientific Notation Support
Steel Decimal supports scientific notation in decimal parsing:
let script = "(+ 1.5e2 2.3e-1)"; // 150 + 0.23 = 150.23
let transformed = steel_decimal.transform(script);
Thread Safety
Precision settings are thread-local, allowing different threads to use different precision contexts safely.
License
This project is licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT License (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.