calculator-danielgorgonha

Crates.iocalculator-danielgorgonha
lib.rscalculator-danielgorgonha
version0.2.0
created_at2025-07-25 17:46:21.515424+00
updated_at2025-07-25 17:49:50.917304+00
descriptionSimple library for u32 operations including power and logarithm
homepagehttps://github.com/danielgorgonha/calculator
repositoryhttps://github.com/danielgorgonha/calculator
max_upload_size
id1767874
size11,896
Daniel R Gorgonha (danielgorgonha)

documentation

https://docs.rs/calculator-danielgorgonha

README

Calculator Library

A simple Rust library for basic mathematical operations with non-negative integers (u32).

Features

  • Basic operations: Addition, subtraction, multiplication, and division
  • Advanced operations: Power and logarithm functions
  • Overflow protection: All operations use Rust's checked_* methods
  • Special case handling: Division by zero and overflow are handled gracefully
  • Complete documentation: All functions are documented with examples
  • Comprehensive tests: Coverage of normal cases and edge cases

Installation

Add to your Cargo.toml:

[dependencies]
calculator-danielgorgonha = "0.2.0"

Usage

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)
}

API

Module calc1

add(a: u32, b: u32) -> u32

Adds two non-negative numbers. If overflow occurs, returns u32::MAX.

sub(a: u32, b: u32) -> u32

Subtracts two non-negative numbers. If a < b, returns 0 to prevent underflow.

Module calc2

multiply(a: u32, b: u32) -> u32

Multiplies two non-negative numbers. If overflow occurs, returns u32::MAX.

rate(a: u32, b: u32) -> u32

Divides two non-negative numbers. If b == 0, returns 0 to prevent division by zero.

Module calc3

power(base: u32, exponent: u32) -> u32

Raises a number to the power of another. If overflow occurs, returns u32::MAX.

logarithm(number: u32, base: u32) -> u32

Calculates the logarithm of a number with a given base. Returns 0 for edge cases.

Why u32?

This library uses u32 (unsigned 32-bit integers) because:

  • Simplicity: For basic calculations, negative numbers are rarely needed
  • Larger range: u32 can represent values from 0 to 4,294,967,295 (vs -2,147,483,648 to 2,147,483,647 for i32)
  • No ambiguity: No confusion about signs
  • More efficient: Operations with non-negative numbers are slightly faster

Testing

Run tests with:

cargo test

Documentation

Generate documentation with:

cargo doc --open

Contributing

Contributions are welcome! Please open an issue or pull request.


:memo: License

This project is under the MIT license. See the LICENSE file for more details.


Made with 💜 by Daniel R Gorgonha :wave:

Commit count: 0

cargo fmt