How to use math functions and constants in Rust

1 Answer

0 votes
// Program Title: Rust Math Functions Demonstration

use std::f64::consts::*; // import math constants like PI, E

fn main() {
    // ============================
    // Constants
    // ============================
    let pi_value = PI; // π constant
    let e_value = E;   // Euler's number

    println!("Constants:");
    println!("  pi = {}", pi_value);
    println!("  e  = {}", e_value);
    println!();

    // ============================
    // Core Functions
    // ============================
    let num_x = 2.5_f64; // sample value

    println!("Core Functions:");
    println!("  sqrt(2.5) = {}", num_x.sqrt());      // square root
    println!("  exp(2.5)  = {}", num_x.exp());       // e^x
    println!("  ln(2.5)   = {}", num_x.ln());        // natural log
    println!("  log10(2.5)= {}", num_x.log10());     // base-10 log
    println!("  powf(2.5,3)= {}", num_x.powf(3.0));  // x^3
    println!();

    // ============================
    // Trigonometric Functions
    // ============================
    let angle = PI / 4.0; // 45 degrees in radians

    println!("Trigonometric Functions:");
    println!("  sin(pi/4) = {}", angle.sin()); // sine
    println!("  cos(pi/4) = {}", angle.cos()); // cosine
    println!("  tan(pi/4) = {}", angle.tan()); // tangent
    println!("  asin(0.5) = {}", 0.5_f64.asin()); // arcsine
    println!("  acos(0.5) = {}", 0.5_f64.acos()); // arccosine
    println!("  atan(1.0) = {}", 1.0_f64.atan()); // arctangent
    println!();

    // ============================
    // Hyperbolic Functions
    // ============================
    println!("Hyperbolic Functions:");
    println!("  sinh(1) = {}", 1.0_f64.sinh()); // hyperbolic sine
    println!("  cosh(1) = {}", 1.0_f64.cosh()); // hyperbolic cosine
    println!("  tanh(1) = {}", 1.0_f64.tanh()); // hyperbolic tangent
    println!();

    // ============================
    // Rounding Functions
    // ============================
    let num_y = -2.7_f64; // negative number for rounding tests

    println!("Rounding Functions:");
    println!("  floor(-2.7) = {}", num_y.floor()); // round down
    println!("  ceil(-2.7)  = {}", num_y.ceil());  // round up
    println!("  round(-2.7) = {}", num_y.round()); // nearest integer
    println!("  trunc(-2.7) = {}", num_y.trunc()); // remove decimals
    println!();

    // ============================
    // Min / Max / Clamp
    // ============================
    let num_a: f64 = 10.0;          // explicitly f64
    let num_b: f64 = 20.0;          // explicitly f64
    let value_to_clamp: f64 = 15.0; // explicitly f64

    println!("Min/Max/Clamp:");
    println!("  min(10,20) = {}", f64::min(num_a, num_b)); // smaller of two
    println!("  max(10,20) = {}", f64::max(num_a, num_b)); // larger of two

    let clamped = value_to_clamp.clamp(0.0_f64, 10.0_f64); // clamp 15 to range 0–10
    println!("  clamp(15,0,10) = {}", clamped);

    // clamp manually
    let clamped = value_to_clamp.clamp(0.0, 10.0); // clamp 15 to range 0–10
    println!("  clamp(15,0,10) = {}", clamped);
    println!();

    // ============================
    // Bitwise Math (Integers)
    // ============================
    let byte_x: u8 = 0xAA; // 10101010 in hex
    let byte_y: u8 = 0xCC; // 11001100 in hex

    println!("Bitwise Math:");
    println!("  X & Y = {:X}", byte_x & byte_y); // AND
    println!("  X | Y = {:X}", byte_x | byte_y); // OR
    println!("  X ^ Y = {:X}", byte_x ^ byte_y); // XOR
    println!("  ~X    = {:X}", !byte_x);         // NOT
    println!("  X << 2 = {:X}", byte_x << 2);    // left shift
    println!("  Y >> 3 = {:X}", byte_y >> 3);    // right shift
    println!();

    // ============================
    // Additional Useful Math
    // ============================
    println!("Additional Math:");
    println!("  abs(-3.14) = {}", (-3.14_f64).abs()); // absolute value
    println!("  fmod(10,3) = {}", 10.0_f64 % 3.0_f64); // remainder
    println!("  hypot(3,4) = {}", 3.0_f64.hypot(4.0)); // sqrt(x²+y²)
    println!("  deg2rad(180) = {}", 180.0_f64.to_radians()); // degrees → radians
    println!("  rad2deg(pi) = {}", PI.to_degrees());         // radians → degrees
}


/* 
run:

Constants:
  pi = 3.141592653589793
  e  = 2.718281828459045

Core Functions:
  sqrt(2.5) = 1.5811388300841898
  exp(2.5)  = 12.182493960703473
  ln(2.5)   = 0.9162907318741551
  log10(2.5)= 0.3979400086720376
  powf(2.5,3)= 15.625

Trigonometric Functions:
  sin(pi/4) = 0.7071067811865475
  cos(pi/4) = 0.7071067811865476
  tan(pi/4) = 0.9999999999999999
  asin(0.5) = 0.5235987755982989
  acos(0.5) = 1.0471975511965979
  atan(1.0) = 0.7853981633974483

Hyperbolic Functions:
  sinh(1) = 1.1752011936438014
  cosh(1) = 1.5430806348152437
  tanh(1) = 0.7615941559557649

Rounding Functions:
  floor(-2.7) = -3
  ceil(-2.7)  = -2
  round(-2.7) = -3
  trunc(-2.7) = -2

Min/Max/Clamp:
  min(10,20) = 10
  max(10,20) = 20
  clamp(15,0,10) = 10
  clamp(15,0,10) = 10

Bitwise Math:
  X & Y = 88
  X | Y = EE
  X ^ Y = 66
  ~X    = 55
  X << 2 = A8
  Y >> 3 = 19

Additional Math:
  abs(-3.14) = 3.14
  fmod(10,3) = 1
  hypot(3,4) = 5
  deg2rad(180) = 3.141592653589793
  rad2deg(pi) = 180
  
*/

 



answered Apr 30 by avibootz
...