Advertisement
Guest User

inb4 homework

a guest
May 1st, 2018
355
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 1.43 KB | None | 0 0
  1. const PI: f64 = 3.141592653589793238462643383279502884197169399375105820974944592307816406286;
  2. const SMALL_ANGLE: f64 = 0.1;
  3.  
  4. enum Angle {
  5.     Rad(f64),
  6.     Deg(f64),
  7. }
  8.  
  9. fn sine(angle: Angle) -> f64 {
  10.     use Angle::{Deg, Rad};
  11.     match angle {
  12.         Deg(theta) => sine(Rad(theta * 2.0 * PI / 360.0)),
  13.         Rad(theta) => if theta <= SMALL_ANGLE {
  14.             theta
  15.         } else {
  16.             2.0 * sine(Rad(theta / 2.0)) * cosine(Rad(theta / 2.0))
  17.         },
  18.     }
  19. }
  20.  
  21. fn cosine(angle: Angle) -> f64 {
  22.     use Angle::{Deg, Rad};
  23.     match angle {
  24.         Deg(theta) => cosine(Rad(theta * 2.0 * PI / 360.0)),
  25.         Rad(theta) => if theta <= SMALL_ANGLE {
  26.             1.0 - theta * theta / 2.0
  27.         } else {
  28.             let cos_v = cosine(Rad(theta / 2.0));
  29.             let sin_v = sine(Rad(theta / 2.0));
  30.             cos_v * cos_v - sin_v * sin_v
  31.         },
  32.     }
  33. }
  34.  
  35. fn tangent(angle: Angle) -> f64 {
  36.     use Angle::{Deg, Rad};
  37.     match angle {
  38.         Deg(theta) => tangent(Rad(theta * 2.0 * PI / 360.0)),
  39.         Rad(theta) => if theta <= SMALL_ANGLE {
  40.             theta
  41.         } else {
  42.             let tan_v = tangent(Rad(theta / 2.0));
  43.             2.0 * tan_v / (1.0 - tan_v * tan_v)
  44.         },
  45.     }
  46. }
  47.  
  48. fn main() {
  49.     println!(
  50.         "Sin 30o: {}\nCos pi/4c: {}\nTan 45: {}",
  51.         sine(Angle::Deg(30.0)),
  52.         cosine(Angle::Rad(PI / 4.0)),
  53.         tangent(Angle::Deg(45.0))
  54.     );
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement