| Crates.io | calculator-danielgorgonha |
| lib.rs | calculator-danielgorgonha |
| version | 0.2.0 |
| created_at | 2025-07-25 17:46:21.515424+00 |
| updated_at | 2025-07-25 17:49:50.917304+00 |
| description | Simple library for u32 operations including power and logarithm |
| homepage | https://github.com/danielgorgonha/calculator |
| repository | https://github.com/danielgorgonha/calculator |
| max_upload_size | |
| id | 1767874 |
| size | 11,896 |
A simple Rust library for basic mathematical operations with non-negative integers (u32).
checked_* methodsAdd to your Cargo.toml:
[dependencies]
calculator-danielgorgonha = "0.2.0"
use calculator_danielgorgonha::calc1::{add, sub};
use calculator_danielgorgonha::calc2::{multiply, rate};
use calculator_danielgorgonha::calc3::{power, logarithm};
fn main() {
// Basic operations
println!("10 + 20 = {}", add(10, 20)); // 30
println!("20 - 10 = {}", sub(20, 10)); // 10
println!("10 * 20 = {}", multiply(10, 20)); // 200
println!("20 / 10 = {}", rate(20, 10)); // 2
// Advanced operations
println!("2^3 = {}", power(2, 3)); // 8
println!("log₂(8) = {}", logarithm(8, 2)); // 3
// Special cases
println!("10 - 20 = {}", sub(10, 20)); // 0 (underflow protection)
println!("10 / 0 = {}", rate(10, 0)); // 0 (division by zero)
println!("MAX + 1 = {}", add(u32::MAX, 1)); // MAX (overflow)
}
calc1add(a: u32, b: u32) -> u32Adds two non-negative numbers. If overflow occurs, returns u32::MAX.
sub(a: u32, b: u32) -> u32Subtracts two non-negative numbers. If a < b, returns 0 to prevent underflow.
calc2multiply(a: u32, b: u32) -> u32Multiplies two non-negative numbers. If overflow occurs, returns u32::MAX.
rate(a: u32, b: u32) -> u32Divides two non-negative numbers. If b == 0, returns 0 to prevent division by zero.
calc3power(base: u32, exponent: u32) -> u32Raises a number to the power of another. If overflow occurs, returns u32::MAX.
logarithm(number: u32, base: u32) -> u32Calculates the logarithm of a number with a given base. Returns 0 for edge cases.
This library uses u32 (unsigned 32-bit integers) because:
u32 can represent values from 0 to 4,294,967,295 (vs -2,147,483,648 to 2,147,483,647 for i32)Run tests with:
cargo test
Generate documentation with:
cargo doc --open
Contributions are welcome! Please open an issue or pull request.
This project is under the MIT license. See the LICENSE file for more details.
Made with 💜 by Daniel R Gorgonha :wave: