const-trig

Crates.ioconst-trig
lib.rsconst-trig
version0.0.2
sourcesrc
created_at2022-02-02 07:27:43.791824
updated_at2022-12-24 18:05:47.646695
descriptionRust crate providing const trig functions
homepage
repositoryhttps://github.com/Roman-Tarasenko-27/const-trig
max_upload_size
id525578
size15,866
Orbital Station (OrbitalStation)

documentation

README

Const-trig

This crate exists to provide const versions of functions, such as cos, sin, etc.

Note that this crate requires nightly Rust to unify behaviour of functions.

Crate is no longer needed when all this function will be const in std.

Any function, such as cos, sin from std can be accessed as const_trig::cos.

There aren't all trig functions, but if you want to add them, you can open a GitHub issue (someday I will read it) :) Usage:

use const_trig::{ToDegrees, ToRadians};

macro_rules! cmp {
   ($fn:ident, $e:expr) => {
       println!(concat!("const_trig::", stringify!($fn), " = {}"), const_trig::$fn($e, None));
       println!(concat!("std ", stringify!($fn), " = {}\n"), $e.$fn());
   };

   ($fn:ident, $lib:expr, $std:expr) => {
       println!(concat!("const_trig::", stringify!($fn), " = {}"), const_trig::$fn($lib, None));
       println!(concat!("std ", stringify!($fn), " = {}\n"), $std.$fn());
   };

   (! $fn:ident, $e:expr, $fnstd:ident) => {
       println!(concat!("const_trig::", stringify!($fn), " = {}"), const_trig::$fn($e, None));
       println!(concat!("std ", stringify!($fnstd), " = {}\n"), $e.$fnstd());
   };
}

fn main() {
   let sixty_degrees = 60.0f32.degrees().radians().get();
   cmp!(sin, sixty_degrees.radians(), sixty_degrees);
   cmp!(cos, sixty_degrees.radians(), sixty_degrees);

   cmp!(sqrt, 4.0f64);
   cmp!(sqrt, 2.0f64);

   cmp!(ln, 10.0f64);
   cmp!(ln, core::f64::consts::E);

   cmp!(! lg, core::f64::consts::E, log10);

   cmp!(! lb, 10.0f64, log2);

   println!("const_trig::log = {}", const_trig::log(3.5, 10.0f64, None));
   println!("std log = {}", 3.5f64.log(10.0));

   println!("{}", const_trig::root(34.0, 5, None))
}
Commit count: 10

cargo fmt