rustamath_mks

Crates.iorustamath_mks
lib.rsrustamath_mks
version0.1.1
sourcesrc
created_at2023-04-05 04:23:14.688419
updated_at2023-04-10 15:55:42.393699
descriptionPhysical constants and MKS system of units
homepagehttps://github.com/igorlesik/rustamath_mks
repositoryhttps://github.com/igorlesik/rustamath_mks
max_upload_size
id830654
size61,138
(igorlesik)

documentation

README

Rustamath MKS

MIT licensed CI

Rustamath MKS is Rust library with support for MKS system of units and values of physical constants in MKS.

List of all constants

Here is an example that demonstrates the basic usage, it can be run with cargo test simple_pendulum -- --nocapture in the source directory.

#[test]
fn simple_pendulum() {
    // simple pendulum period formula is `T = 2*Pi*sqrt(L/g)`
    let pendulum_len = MksVal::new(6.0, f64::FOOT, FOOT_UNIT);
    let g = MksVal::new(1.0, f64::GRAV_ACCEL, GRAV_ACCEL_UNIT);

    println!("Pendulum length is {:.2} {}", pendulum_len.val, pendulum_len.unit);
    println!("G on Earth is {:.2} {}", g.val, g.unit);

    assert_eq!(pendulum_len.unit.to_string(), "[m]");
    assert_eq!(g.unit.to_string(), "[m / s^2]");

    let pendulum_len_over_accel = pendulum_len / g;
    assert!(pendulum_len_over_accel.unit == TIME_UNIT * TIME_UNIT);

    let pi_x_2 = MksVal::new_scalar(2.0 * std::f64::consts::PI);

    let period = pi_x_2 * pendulum_len_over_accel.sqrt();
    assert!(period.unit == TIME_UNIT);

    println!("Pendulum period is {:.2} {}", period.val, period.unit);
    assert_eq!(period.unit.to_string(), "[s]");
}

And the output is:

Pendulum length is 1.83 [m]
G on Earth is 9.81 [m / s^2]
Pendulum period is 2.71 [s]

(Check the result with any online calculator, for example https://www.omnicalculator.com/physics/simple-pendulum)

This crate provides:

  • Physical constants, such as the speed of light, c, and gravitational constant, G. The values are available in the standard MKSA unit system (meters, kilograms, seconds, amperes). For example: let half_speed_of_light = f64::SPEED_OF_LIGHT / 2.0;.
  • MKS unit type, for example: assert_eq!(SPEED_OF_LIGHT_UNIT * TIME_UNIT, LIGHT_YEAR_UNIT);.
  • Printing unit as a string, for example: assert_eq!(&SPEED_OF_LIGHT_UNIT.to_string(), "[m / s]");.
  • Values with units attached, for example: let pendulum_len = MksVal::new(6.0, f64::FOOT, FOOT_UNIT);.
  • Operations on values, for example: let pendulum_len_over_accel = pendulum_len / g;.
Commit count: 12

cargo fmt