cos/sin idk
init
| -rw-r--r-- | .gitignore | 2 | ||||
| -rw-r--r-- | Cargo.toml | 10 | ||||
| -rw-r--r-- | LICENSE | 21 | ||||
| -rw-r--r-- | README.md | 7 | ||||
| -rw-r--r-- | rust-toolchain.toml | 2 | ||||
| -rw-r--r-- | src/lib.rs | 37 |
6 files changed, 79 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..96ef6c0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/target +Cargo.lock diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..f642719 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "sine" +version = "0.1.0" +edition = "2021" +description = "cos/sin" +repository = "https://github.com/bend-n/sine" +license = "MIT" + +[dependencies] +umath = "0.0.3" @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2023 bendn + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..65fbb1b --- /dev/null +++ b/README.md @@ -0,0 +1,7 @@ +## sine + +rust lib, with 2 functions. can you guess which? + +these are approximations that are less accurate than the std. + +dont expect these implementations to be too good, i just wanted to stop copy pasting stuff around.
\ No newline at end of file diff --git a/rust-toolchain.toml b/rust-toolchain.toml new file mode 100644 index 0000000..5d56faf --- /dev/null +++ b/rust-toolchain.toml @@ -0,0 +1,2 @@ +[toolchain] +channel = "nightly" diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..612dafe --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,37 @@ +#![doc = include_str!("../README.md")] +use std::f32::consts::{PI, TAU}; +use umath::FF32; +const FRACT_PI_2: f32 = PI / 2.0; + +#[inline] +/// Calaculates the cosine of x, approximately. cant say how approximately. +pub fn cos(x: f32) -> f32 { + let x = unsafe { FF32::new(x) }; + let mut y = x * unsafe { FF32::new(1.0 / TAU) }; + y -= 0.25 + (y + 0.25).floor(); + *(y * 16.0 * (y.abs() - 0.5)) +} + +#[inline] +/// Calculates the sine of x, approximately. cant say how approximately. +pub fn sin(x: f32) -> f32 { + cos(x - FRACT_PI_2) +} + +/// extension crate for cos/sin. +pub trait Sine { + /// faster cos + fn cosf(self) -> Self; + /// faster sin + fn sinf(self) -> Self; +} + +impl Sine for f32 { + fn cosf(self) -> Self { + cos(self) + } + + fn sinf(self) -> Self { + sin(self) + } +} |