Crates.io | rustamath_mks |
lib.rs | rustamath_mks |
version | 0.1.1 |
source | src |
created_at | 2023-04-05 04:23:14.688419 |
updated_at | 2023-04-10 15:55:42.393699 |
description | Physical constants and MKS system of units |
homepage | https://github.com/igorlesik/rustamath_mks |
repository | https://github.com/igorlesik/rustamath_mks |
max_upload_size | |
id | 830654 |
size | 61,138 |
Rustamath MKS is Rust library with support for MKS system of units and values of physical constants in MKS.
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:
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;
.assert_eq!(SPEED_OF_LIGHT_UNIT * TIME_UNIT, LIGHT_YEAR_UNIT);
.assert_eq!(&SPEED_OF_LIGHT_UNIT.to_string(), "[m / s]");
.let pendulum_len = MksVal::new(6.0, f64::FOOT, FOOT_UNIT);
.let pendulum_len_over_accel = pendulum_len / g;
.