Crates.io | list_math |
lib.rs | list_math |
version | 0.3.4 |
source | src |
created_at | 2021-03-18 15:15:14.659466 |
updated_at | 2024-08-18 00:04:15.626352 |
description | a crate for doing math on a list including average, smallest, largest, and standard deviation |
homepage | |
repository | |
max_upload_size | |
id | 370546 |
size | 8,925 |
This crate provides functions for performing basic mathematical operations on lists in Rust.
Add the following line to your Cargo.toml
file:
[dependencies]
list_math = "0.3.4"
Then run cargo build
to install the crate.
The largest function finds the largest element in a list of isize
values.
use list_math::largest;
fn main() {
let numbers = vec![ 3 , 10 , - 1 , 5 ];
let biggest = largest(&numbers);
println!("The largest number is: {}", biggest);
}
This code will print:
The largest number is: 10
The smallest function finds the smallest element in a list of isize
values.
use list_math::smallest;
fn main() {
let numbers = vec![ 3 , 10 , - 1 , 5 ];
let biggest = smallest(&numbers);
println!("The smallest number is: {}", biggest);
}
This code will print:
The smallest number is: -1
The AverageList struct helps you keep track of a running average of i32
values.
use list_math::AverageList;
fn main() {
let mut avg_list = AverageList::new();
avg_list.add( 2 );
avg_list.add( 8 );
avg_list.add( 4 );
println!("Current average: {}", avg_list.average()); // Output: 4.
avg_list.remove(); // Removes last element (4)
println!("Average after removal: {}", avg_list.average()); // Output: 5.
}
This code will print:
Current average: 4.
Average after removal: 5.
Methods: ● new: Creates a new AverageList with an initial average of 0. ● add(value): Adds a new value to the list and updates the average. ● remove:Removes the last added value from the list and updates the average (returns None if the list is empty). ● average: Returns the current average as a f64.
This function calculates the standard deviation of a list of floating-point numbers. The standard deviation is a measure of how spread out the data is from its mean (average).
data: A slice of type &[f64]
. This represents a reference to a slice of floating-point numbers.
Option<f64>
: The function returns an Option of type f64. The Option type can hold either Some(value) where value is the calculated standard deviation or None if there's an error (e.g., empty data list).
Error Handling:
The function handles the case of an empty data list by returning None.
The function first checks if the input data is empty. If not empty, it calculates the mean of the data. It then iterates through the data and computes the squared deviations from the mean for each element. Bessel's correction is applied by dividing the sum of squared deviations by data.len() as f64 - 1.0. This adjusts for the bias introduced when estimating the population mean from a sample. Finally, the function returns the square root of the variance (sum of squared deviations divided by the adjusted number of elements), which is the standard deviation.
fn main() {
let numbers = vec![1.0, 3.0, 5.0, 7.0, 9.0];
let std_dev = standard_deviation(&numbers);
if let Some(std_dev) = std_dev {
println!("Standard deviation: {}", std_dev);
} else {
println!("Empty data list");
}
}
Note: This function assumes the data type is f64 for floating-point numbers.
We welcome contributions to this crate! Please message me at piscitello284@gmail.com for details on how to get involved.
Submit an issue at https://github.com/jameson-pi/list_math if you encounter anything weird.
This crate is licensed under the MIT License.