Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ These are for demonstration purposes only.
- [x] [Manacher](./src/string/manacher.rs)
- [x] [Rabin Carp](./src/string/rabin_karp.rs)
- [x] [Reverse](./src/string/reverse.rs)
- [x] [Run Length Encoding](.src/string/run_length_encoding.rs)
- [x] [Run Length Encoding](./src/string/run_length_encoding.rs)
- [x] [Hamming Distance](./src/string/hamming_distance.rs)
- [x] [Jaro-Winkler Distance](./src/string/jaro_winkler_distance.rs)
- [x] [Suffix Tree](./src/string/suffix_tree.rs)
Expand Down
2 changes: 2 additions & 0 deletions src/math/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ mod random;
mod sieve_of_eratosthenes;
mod signum;
mod simpson_integration;
mod sine;
mod square_root;
mod trial_division;
mod zellers_congruence_algorithm;
Expand Down Expand Up @@ -82,6 +83,7 @@ pub use self::random::PCG32;
pub use self::sieve_of_eratosthenes::sieve_of_eratosthenes;
pub use self::signum::signum;
pub use self::simpson_integration::simpson_integration;
pub use self::sine::sine;
pub use self::square_root::{fast_inv_sqrt, square_root};
pub use self::trial_division::trial_division;
pub use self::zellers_congruence_algorithm::zellers_congruence_algorithm;
58 changes: 58 additions & 0 deletions src/math/sine.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Calculate Sine function.
// Formula: sine(x) = x - x^3/3! + x^5/5! - x^7/7! + ...
// Where: x = angle in randians.
// It is not a real function so I will just do 9 loops, it's just an approximation.
// Source:
// https://web.archive.org/web/20221111013039/https://www.homeschoolmath.net/teaching/sine_calculator.php

use std::f32::consts::PI;

fn factorial(num: u64) -> u64 {
(1..=num).product()
}

pub fn sine(angle: f64) -> f64 {
// Simplify the angle
let angle = angle % (2.0 * PI as f64);

let mut result = angle;
let mut a: u64 = 3;
let mut b = -1.0;

for _ in 0..9 {
result += b * (angle.powi(a as i32)) / (factorial(a) as f64);

b = -b;
a += 2;
}

result
}

#[cfg(test)]
mod tests {
use super::{sine, PI};

fn assert(angle: f64, expected_result: f64) {
// I will round the result to 3 decimal places, since it's an approximation.
assert_eq!(
format!("{:.3}", sine(angle)),
format!("{:.3}", expected_result)
);
}

#[test]
fn test_sine() {
assert(0.0, 0.0);
assert(PI as f64 / 2.0, 1.0);
assert(PI as f64 / 4.0, 1.0 / f64::sqrt(2.0));
assert(PI as f64, -0.0);
assert(PI as f64 * 3.0 / 2.0, -1.0);
assert(PI as f64 * 2.0, 0.0);
assert(PI as f64 * 2.0 * 3.0, 0.0);
assert(-PI as f64, 0.0);
assert(-PI as f64 / 2.0, -1.0);
assert(PI as f64 * 8.0 / 45.0, 0.5299192642);
assert(0.5, 0.4794255386);
}
}
2 changes: 1 addition & 1 deletion src/string/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ From [Wikipedia][hamming-distance-wiki]: In information theory, the Hamming dist

[run-length-encoding-wiki]: https://en.wikipedia.org/wiki/Run-length_encoding

### [Run Length Encoding](./run_lentgh_encoding.rs)
### [Run Length Encoding](./run_length_encoding.rs)
From [Wikipedia][run-length-encoding-wiki]: a form of lossless data compression in which runs of data (sequences in which the same data value occurs in many consecutive data elements) are stored as a single data value and count, rather than as the original run.

[hamming-distance-wiki]: https://en.wikipedia.org/wiki/Hamming_distance