-
Notifications
You must be signed in to change notification settings - Fork 1
api calculator lib
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.
use calculator::{Calculator, Operation};
let mut calc = Calculator::new();
let result = calc.calculate(5.0, 3.0, Operation::Add);
assert_eq!(result, 8.0);Errors that can occur during calculator operations.
Attempted to divide by zero.
The result overflowed.
impl std::fmt::Display for CalculatorError
impl std::error::Error for CalculatorError
Enumeration of supported arithmetic operations.
Addition operation (+)
Subtraction operation (-)
Multiplication operation (*)
Division operation (/)
impl std::fmt::Display for Operation
A calculator that performs basic arithmetic operations.
The Calculator struct maintains a history of all operations performed, allowing users to review previous calculations.
use calculator::Calculator;
let mut calc = Calculator::new();
let sum = calc.add(10.0, 5.0);
assert_eq!(sum, 15.0);impl Calculator
Adds two numbers together.
-
a- The first operand -
b- The second operand
The sum of a and b.
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.
-
a- The first operand -
b- The second operand -
op- The operation to perform
The result of the operation.
Panics if dividing by zero. Use Calculator::divide for safe division.
Clears the operation history.
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.
-
a- The dividend -
b- The divisor
The quotient a / b, or an error if b is zero.
Returns a DivisionByZero error if b is zero.
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.
A slice containing all operations performed by this calculator.
Multiplies two numbers together.
-
a- The first factor -
b- The second factor
The product of a and b.
use calculator::Calculator;
let mut calc = Calculator::new();
assert_eq!(calc.multiply(3.0, 4.0), 12.0);Creates a new Calculator instance with an empty history.
A new Calculator with no operation history.
use calculator::Calculator;
let calc = Calculator::new();
assert!(calc.history().is_empty());Subtracts the second number from the first.
-
a- The minuend -
b- The subtrahend
The difference a - b.
use calculator::Calculator;
let mut calc = Calculator::new();
assert_eq!(calc.subtract(10.0, 4.0), 6.0);The result of a calculation, including operands and the operation performed.
The first operand
The second operand
operation: Operation
The operation that was performed
The calculated result
impl std::fmt::Display for OperationResult
A scientific calculator with additional mathematical functions.
Extends the basic Calculator with trigonometric, logarithmic, and other advanced mathematical operations.
impl ScientificCalculator
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.
Calculates the cosine of an angle.
-
angle- The angle (in degrees or radians based on settings)
The cosine of the angle.
fn ln(&self, x: f64) -> Result<f64, CalculatorError>
Calculates the natural logarithm of a number.
-
x- The number (must be positive)
The natural logarithm of x, or an error if x <= 0.
Creates a new ScientificCalculator.
By default, trigonometric functions use radians.
Raises a number to a power.
-
base- The base number -
exponent- The exponent
base raised to the power of exponent.
Sets whether to use degrees for trigonometric functions.
-
use_degrees- If true, trig functions expect degrees; if false, radians.
Calculates the sine of an angle.
-
angle- The angle (in degrees or radians based on settings)
The sine of the angle.
Calculates the square root of a number.
-
x- The number (must be non-negative)
The square root of x, or NaN if x is negative.