Skip to content

Commit 51b05bb

Browse files
committed
Add Angle.
(Also fixed spelling of 'constant').
1 parent 4cafb4d commit 51b05bb

File tree

14 files changed

+97
-24
lines changed

14 files changed

+97
-24
lines changed

examples/format_test.rs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,5 @@
11
extern crate measurements;
2-
use measurements::Temperature;
3-
use measurements::Length;
4-
use measurements::Pressure;
5-
use measurements::Volume;
6-
use measurements::Mass;
7-
use measurements::Speed;
8-
use measurements::Acceleration;
9-
use measurements::Energy;
10-
use measurements::Power;
11-
use measurements::Force;
2+
use measurements::*;
123

134
fn main() {
145
for power in -12..12 {

src/acceleration.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! Types and contants for handling acceleration.
1+
//! Types and constants for handling acceleration.
22
33
use super::measurement::*;
44
use super::length;
@@ -16,7 +16,7 @@ use super::length;
1616
/// let track = Length::from_miles(0.25);
1717
/// let finish = Speed::from_miles_per_hour(120.0);
1818
/// let time = Duration::new(10, 0);
19-
/// let accel = finish / time;
19+
/// let accel: Acceleration = finish / time;
2020
/// println!("You accelerated over {} at an average of {}", track, accel);
2121
/// ```
2222
#[derive(Copy, Clone, Debug)]

src/angle.rs

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
//! Types and constants for handling angles
2+
3+
use super::measurement::*;
4+
use ::std::f64::consts::PI;
5+
6+
/// The 'Angle' struct can be used to deal with angles in a common way.
7+
///
8+
/// # Example
9+
///
10+
/// ```
11+
/// use measurements::Angle;
12+
///
13+
/// let whole_cake = Angle::from_degrees(360.0);
14+
/// let pieces = 6.0;
15+
/// let slice = whole_cake / pieces;
16+
/// println!("Each slice will be {} degrees", slice.as_degrees());
17+
/// ```
18+
#[derive(Copy, Clone, Debug)]
19+
pub struct Angle {
20+
radians: f64,
21+
}
22+
23+
/// Number of degrees in a radian
24+
pub const RADIAN_DEGREE_FACTOR: f64 = 180.0 / PI;
25+
26+
impl Angle {
27+
28+
/// Create a new Angle from a floating point value in degrees
29+
pub fn from_degrees(degrees: f64) -> Self {
30+
Angle::from_radians(degrees / RADIAN_DEGREE_FACTOR)
31+
}
32+
33+
/// Create a new Angle from a floating point value in radians
34+
pub fn from_radians(radians: f64) -> Self {
35+
Angle { radians: radians }
36+
}
37+
38+
/// Convert this Angle to a floating point value in degrees
39+
pub fn as_degrees(&self) -> f64 {
40+
self.radians * RADIAN_DEGREE_FACTOR
41+
}
42+
43+
/// Convert this Angle to a floating point value in radians
44+
pub fn as_radians(&self) -> f64 {
45+
self.radians
46+
}
47+
}
48+
49+
impl Measurement for Angle {
50+
fn as_base_units(&self) -> f64 {
51+
self.radians
52+
}
53+
54+
fn from_base_units(units: f64) -> Self {
55+
Self::from_radians(units)
56+
}
57+
58+
fn as_base_units_name(&self) -> &'static str {
59+
"rad"
60+
}
61+
}
62+
63+
implement_measurement! { Angle }
64+
65+
#[cfg(test)]
66+
mod test {
67+
use angle::*;
68+
use test_utils::assert_almost_eq;
69+
70+
#[test]
71+
fn radians() {
72+
let i1 = Angle::from_degrees(360.0);
73+
let r1 = i1.as_radians();
74+
let i2 = Angle::from_radians(PI);
75+
let r2 = i2.as_degrees();
76+
assert_almost_eq(r1, 2.0 * PI);
77+
assert_almost_eq(r2, 180.0);
78+
}
79+
}

src/area.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! Types and contants for handling areas.
1+
//! Types and constants for handling areas.
22
33
use super::measurement::*;
44
use super::length;

src/energy.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! Types and contants for handling energy.
1+
//! Types and constants for handling energy.
22
33
use super::measurement::*;
44

src/force.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! Types and contants for handling force.
1+
//! Types and constants for handling force.
22
33
use super::measurement::*;
44

@@ -23,7 +23,7 @@ pub const DYNES_PER_NEWTON: f64 = 1e5;
2323
///
2424
/// let metric_ton = Mass::from_metric_tons(1.0);
2525
/// let gravity = Acceleration::from_meters_per_second_per_second(9.81);
26-
/// let force = metric_ton * gravity; // F=ma
26+
/// let force: Force = metric_ton * gravity; // F=ma
2727
/// println!(
2828
/// "One metric ton exerts a force of {} due to gravity",
2929
/// force);

src/length.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! Types and contants for handling lengths (or distances).
1+
//! Types and constants for handling lengths (or distances).
22
33
use super::measurement::*;
44

src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ pub use force::Force;
4545
pub mod area;
4646
pub use area::Area;
4747

48+
pub mod angle;
49+
pub use angle::Angle;
50+
4851
pub mod prelude;
4952

5053
/// For given types A, B and C, implement, using base units:

src/mass.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! Types and contants for handling masses.
1+
//! Types and constants for handling masses.
22
33
use super::measurement::*;
44

src/power.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! Types and contants for handling power.
1+
//! Types and constants for handling power.
22
33
use super::measurement::*;
44

0 commit comments

Comments
 (0)