Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const PI: f64 = 3.141592653589793238462643383279502884197169399375105820974944592307816406286;
- const SMALL_ANGLE: f64 = 0.1;
- enum Angle {
- Rad(f64),
- Deg(f64),
- }
- fn sine(angle: Angle) -> f64 {
- use Angle::{Deg, Rad};
- match angle {
- Deg(theta) => sine(Rad(theta * 2.0 * PI / 360.0)),
- Rad(theta) => if theta <= SMALL_ANGLE {
- theta
- } else {
- 2.0 * sine(Rad(theta / 2.0)) * cosine(Rad(theta / 2.0))
- },
- }
- }
- fn cosine(angle: Angle) -> f64 {
- use Angle::{Deg, Rad};
- match angle {
- Deg(theta) => cosine(Rad(theta * 2.0 * PI / 360.0)),
- Rad(theta) => if theta <= SMALL_ANGLE {
- 1.0 - theta * theta / 2.0
- } else {
- let cos_v = cosine(Rad(theta / 2.0));
- let sin_v = sine(Rad(theta / 2.0));
- cos_v * cos_v - sin_v * sin_v
- },
- }
- }
- fn tangent(angle: Angle) -> f64 {
- use Angle::{Deg, Rad};
- match angle {
- Deg(theta) => tangent(Rad(theta * 2.0 * PI / 360.0)),
- Rad(theta) => if theta <= SMALL_ANGLE {
- theta
- } else {
- let tan_v = tangent(Rad(theta / 2.0));
- 2.0 * tan_v / (1.0 - tan_v * tan_v)
- },
- }
- }
- fn main() {
- println!(
- "Sin 30o: {}\nCos pi/4c: {}\nTan 45: {}",
- sine(Angle::Deg(30.0)),
- cosine(Angle::Rad(PI / 4.0)),
- tangent(Angle::Deg(45.0))
- );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement