Skip to content

api calculator lib

github-actions[bot] edited this page Sep 8, 2026 · 2 revisions

Crate calculator

crate calculator

Calculator Library

A simple calculator library demonstrating Rust documentation with sphinx-rust.

This crate provides basic arithmetic operations and a calculator struct that maintains a history of operations.

Example

use calculator::{Calculator, Operation};

let mut calc = Calculator::new();
let result = calc.calculate(5.0, 3.0, Operation::Add);
assert_eq!(result, 8.0);

Enums

enum CalculatorError

Errors that can occur during calculator operations.

DivisionByZero

Attempted to divide by zero.

Overflow

The result overflowed.

Traits implemented

impl std::fmt::Display for CalculatorError

impl std::error::Error for CalculatorError

enum Operation

Enumeration of supported arithmetic operations.

Add

Addition operation (+)

Subtract

Subtraction operation (-)

Multiply

Multiplication operation (*)

Divide

Division operation (/)

Traits implemented

impl std::fmt::Display for Operation

Structs and Unions

struct Calculator

A calculator that performs basic arithmetic operations.

The Calculator struct maintains a history of all operations performed, allowing users to review previous calculations.

Examples

use calculator::Calculator;

let mut calc = Calculator::new();
let sum = calc.add(10.0, 5.0);
assert_eq!(sum, 15.0);

Implementations

Functions

fn add(&mut self, a: f64, b: f64) -> f64

Adds two numbers together.

Arguments

  • a - The first operand
  • b - The second operand

Returns

The sum of a and b.

Examples

use calculator::Calculator;

let mut calc = Calculator::new();
assert_eq!(calc.add(2.0, 3.0), 5.0);

fn calculate(&mut self, a: f64, b: f64, op: Operation) -> f64

Performs a calculation using the specified operation.

Arguments

  • a - The first operand
  • b - The second operand
  • op - The operation to perform

Returns

The result of the operation.

Panics

Panics if dividing by zero. Use Calculator::divide for safe division.

fn clear_history(&mut self)

Clears the operation history.

Examples

use calculator::Calculator;

let mut calc = Calculator::new();
calc.add(1.0, 2.0);
assert!(!calc.history().is_empty());
calc.clear_history();
assert!(calc.history().is_empty());

fn divide(&mut self, a: f64, b: f64) -> Result<f64, CalculatorError>

Divides the first number by the second.

Arguments

  • a - The dividend
  • b - The divisor

Returns

The quotient a / b, or an error if b is zero.

Errors

Returns a DivisionByZero error if b is zero.

Examples

use calculator::Calculator;

let mut calc = Calculator::new();
assert_eq!(calc.divide(10.0, 2.0).unwrap(), 5.0);
assert!(calc.divide(10.0, 0.0).is_err());

fn history(&self) -> &[OperationResult]

Returns a reference to the operation history.

Returns

A slice containing all operations performed by this calculator.

fn multiply(&mut self, a: f64, b: f64) -> f64

Multiplies two numbers together.

Arguments

  • a - The first factor
  • b - The second factor

Returns

The product of a and b.

Examples

use calculator::Calculator;

let mut calc = Calculator::new();
assert_eq!(calc.multiply(3.0, 4.0), 12.0);

fn new() -> Self

Creates a new Calculator instance with an empty history.

Returns

A new Calculator with no operation history.

Examples

use calculator::Calculator;

let calc = Calculator::new();
assert!(calc.history().is_empty());

fn subtract(&mut self, a: f64, b: f64) -> f64

Subtracts the second number from the first.

Arguments

  • a - The minuend
  • b - The subtrahend

Returns

The difference a - b.

Examples

use calculator::Calculator;

let mut calc = Calculator::new();
assert_eq!(calc.subtract(10.0, 4.0), 6.0);

struct OperationResult

The result of a calculation, including operands and the operation performed.

operand_a: f64

The first operand

operand_b: f64

The second operand

operation: Operation

The operation that was performed

result: f64

The calculated result

Traits implemented

impl std::fmt::Display for OperationResult

struct ScientificCalculator

A scientific calculator with additional mathematical functions.

Extends the basic Calculator with trigonometric, logarithmic, and other advanced mathematical operations.

Implementations

Functions

fn basic(&self) -> &Calculator

Returns a reference to the underlying basic calculator.

fn basic_mut(&mut self) -> &mut Calculator

Returns a mutable reference to the underlying basic calculator.

fn cos(&self, angle: f64) -> f64

Calculates the cosine of an angle.

Arguments

  • angle - The angle (in degrees or radians based on settings)

Returns

The cosine of the angle.

fn ln(&self, x: f64) -> Result<f64, CalculatorError>

Calculates the natural logarithm of a number.

Arguments

  • x - The number (must be positive)

Returns

The natural logarithm of x, or an error if x <= 0.

fn new() -> Self

Creates a new ScientificCalculator.

By default, trigonometric functions use radians.

fn pow(&self, base: f64, exponent: f64) -> f64

Raises a number to a power.

Arguments

  • base - The base number
  • exponent - The exponent

Returns

base raised to the power of exponent.

fn set_use_degrees(&mut self, use_degrees: bool)

Sets whether to use degrees for trigonometric functions.

Arguments

  • use_degrees - If true, trig functions expect degrees; if false, radians.

fn sin(&self, angle: f64) -> f64

Calculates the sine of an angle.

Arguments

  • angle - The angle (in degrees or radians based on settings)

Returns

The sine of the angle.

fn sqrt(&self, x: f64) -> f64

Calculates the square root of a number.

Arguments

  • x - The number (must be non-negative)

Returns

The square root of x, or NaN if x is negative.

Clone this wiki locally