techmore.in

Rust - Maths Functions

Rust provides various mathematical functions through its standard library. These functions are available for both integer and floating-point types, and they allow you to perform a wide range of mathematical operations. Here’s an overview of the key mathematical functions and operations in Rust.

1. Basic Arithmetic Operations

Rust supports basic arithmetic operations like addition, subtraction, multiplication, division, and remainder.

Examples:

rust
fn main() { let a = 10; let b = 4; // Addition let sum = a + b; println!("Sum: {}", sum); // Subtraction let difference = a - b; println!("Difference: {}", difference); // Multiplication let product = a * b; println!("Product: {}", product); // Division let quotient = a / b; println!("Quotient: {}", quotient); // Remainder let remainder = a % b; println!("Remainder: {}", remainder); }

2. Floating-Point Operations

Rust provides more advanced mathematical functions, particularly for floating-point numbers, via the f32 and f64 types. These functions are available directly on these types as methods.

Examples:

rust
fn main() { let x: f64 = 2.0; let y: f64 = 3.0; // Power let power = x.powf(y); println!("2^3 = {}", power); // Square root let sqrt = x.sqrt(); println!("sqrt(2) = {}", sqrt); // Exponential (e^x) let exp = x.exp(); println!("e^2 = {}", exp); // Natural logarithm (ln) let ln = x.ln(); println!("ln(2) = {}", ln); // Base 10 logarithm let log10 = x.log10(); println!("log10(2) = {}", log10); // Base 2 logarithm let log2 = x.log2(); println!("log2(2) = {}", log2); // Sine let sin = x.sin(); println!("sin(2) = {}", sin); // Cosine let cos = x.cos(); println!("cos(2) = {}", cos); // Tangent let tan = x.tan(); println!("tan(2) = {}", tan); // Absolute value let abs = (-x).abs(); println!("abs(-2) = {}", abs); // Rounding let round = 2.5_f64.round(); println!("round(2.5) = {}", round); // Floor let floor = 2.7_f64.floor(); println!("floor(2.7) = {}", floor); // Ceiling let ceil = 2.3_f64.ceil(); println!("ceil(2.3) = {}", ceil); }

3. Integer Operations

For integer types, Rust provides several methods that can be used to perform various mathematical operations, such as finding the absolute value, checking for overflow, etc.

Examples:

rust
fn main() { let x: i32 = -10; // Absolute value let abs_value = x.abs(); println!("abs(-10) = {}", abs_value); // Checking overflow let max = i32::MAX; let min = i32::MIN; // Wrapping addition let wrap_add = max.wrapping_add(1); println!("i32::MAX + 1 (wrapping) = {}", wrap_add); // Saturating addition let sat_add = max.saturating_add(1); println!("i32::MAX + 1 (saturating) = {}", sat_add); // Checking for zero let is_zero = x.is_zero(); println!("Is zero: {}", is_zero); // Counting leading zeros let leading_zeros = x.leading_zeros(); println!("Leading zeros: {}", leading_zeros); }

4. Trigonometric Functions

Rust provides a full set of trigonometric functions for floating-point numbers.

Examples:

rust
fn main() { let angle = std::f64::consts::PI / 4.0; // 45 degrees in radians // Sine let sine = angle.sin(); println!("sin(π/4) = {}", sine); // Cosine let cosine = angle.cos(); println!("cos(π/4) = {}", cosine); // Tangent let tangent = angle.tan(); println!("tan(π/4) = {}", tangent); // Arcsine let arcsine = sine.asin(); println!("asin(sin(π/4)) = {}", arcsine); // Arccosine let arccosine = cosine.acos(); println!("acos(cos(π/4)) = {}", arccosine); // Arctangent let arctangent = tangent.atan(); println!("atan(tan(π/4)) = {}", arctangent); }

5. Constants

Rust provides several mathematical constants available in the std::f32::consts and std::f64::consts modules, such as PI, E, and more.

Examples:

rust
fn main() { println!("Pi: {}", std::f64::consts::PI); println!("Euler's number: {}", std::f64::consts::E); println!("Square root of 2: {}", std::f64::consts::SQRT_2); }

6. Min and Max

Rust provides methods to find the minimum and maximum of two values.

Examples:

rust
fn main() { let a = 10; let b = 20; let min = a.min(b); let max = a.max(b); println!("Min: {}", min); println!("Max: {}", max); }

7. Clamping Values

You can clamp a value within a specific range using the clamp method.

Example:

rust
fn main() { let value = 15; let clamped = value.clamp(10, 20); // Value is already in the range let clamped_out_of_range = value.clamp(0, 10); // Value will be clamped to 10 println!("Clamped value: {}", clamped); println!("Clamped out-of-range value: {}", clamped_out_of_range); }

Summary

  • Basic Arithmetic: Addition, subtraction, multiplication, division, and remainder.
  • Floating-Point Operations: Power, square root, logarithms, trigonometric functions, and more.
  • Integer Operations: Absolute value, overflow handling, and more.
  • Constants: Access to mathematical constants like PI and E.
  • Min and Max: Find the minimum or maximum of two values.
  • Clamping: Restrict a value within a specific range.

These functions cover most of the common mathematical operations you might need in Rust, providing a solid foundation for both simple and complex calculations.