Skip to content

Commit

Permalink
Removed libm and switch t num-traits
Browse files Browse the repository at this point in the history
Closes #128
  • Loading branch information
ChristopherRabotin committed Jul 31, 2022
1 parent 3cd30c5 commit ec8a0f8
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 19 deletions.
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ readme = "README.md"
license = "Apache-2.0"

[dependencies]
libm = "0.2.2"
serde = {version = "1.0.137", optional = true}
regex = {version = "1.5.5", optional = true}
serde_derive = {version = "1.0.137", optional = true}
[dependencies.num-traits]
version = "0.2"
default-features = false

[dev-dependencies]
criterion = "0.3.5"
Expand Down
12 changes: 5 additions & 7 deletions src/duration.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use libm::{fabs, floor, pow};

#[cfg(feature = "std")]
use crate::ParsingErrors;
use crate::{
Expand Down Expand Up @@ -493,14 +491,14 @@ impl Mul<f64> for Duration {
let ten: f64 = 10.0;

loop {
if fabs(floor(new_val) - new_val) < f64::EPSILON {
if (new_val.floor() - new_val).abs() < f64::EPSILON {
// Yay, we've found the precision of this number
break;
}
// Multiply by the precision
// https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=b760579f103b7192c20413ebbe167b90
p += 1;
new_val = q * pow(ten, f64::from(p));
new_val = q * ten.powi(p);
}

Duration::from_total_nanoseconds(
Expand Down Expand Up @@ -551,7 +549,7 @@ impl Mul<f64> for Unit {
Unit::Microsecond => q * (NANOSECONDS_PER_MICROSECOND as f64),
Unit::Nanosecond => q,
};
if fabs(total_ns) < (i64::MAX as f64) {
if total_ns.abs() < (i64::MAX as f64) {
Duration::from_truncated_nanoseconds(total_ns as i64)
} else {
Duration::from_total_nanoseconds(total_ns as i128)
Expand Down Expand Up @@ -581,7 +579,7 @@ macro_rules! impl_ops_for_type {
Freq::KiloHertz => NANOSECONDS_PER_MILLISECOND as f64 / (q as f64),
Freq::Hertz => (NANOSECONDS_PER_SECOND as f64) / (q as f64),
};
if fabs(total_ns) < (i64::MAX as f64) {
if total_ns.abs() < (i64::MAX as f64) {
Duration::from_truncated_nanoseconds(total_ns as i64)
} else {
Duration::from_total_nanoseconds(total_ns as i128)
Expand Down Expand Up @@ -655,7 +653,7 @@ impl fmt::LowerExp for Duration {
// Prints the duration with appropriate units
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let seconds_f64 = self.in_seconds();
let seconds_f64_abs = fabs(seconds_f64);
let seconds_f64_abs = seconds_f64.abs();
if seconds_f64_abs < 1e-5 {
fmt::Display::fmt(&(seconds_f64 * 1e9), f)?;
write!(f, " ns")
Expand Down
18 changes: 8 additions & 10 deletions src/epoch.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use libm::{fabs, sin, trunc};

use crate::duration::{Duration, Unit};
use crate::{
Errors, TimeSystem, DAYS_GPS_TAI_OFFSET, ET_EPOCH_S, J1900_OFFSET, J2000_OFFSET, MJD_OFFSET,
Expand Down Expand Up @@ -303,9 +301,9 @@ impl Epoch {
let g_rad = (PI / 180.0) * (357.528 + 35_999.050 * tt_centuries_j2k);

// Decimal does not provide trig functions, so let's define the parts of the trig separately.
let inner = g_rad + 0.0167 * sin(g_rad);
let inner = g_rad + 0.0167 * g_rad.sin();

Self(tt_duration + ((ET_EPOCH_S as f64) - (0.001_658 * sin(inner))) * Unit::Second)
Self(tt_duration + ((ET_EPOCH_S as f64) - (0.001_658 * inner.sin())) * Unit::Second)
}

#[must_use]
Expand Down Expand Up @@ -815,23 +813,23 @@ impl Epoch {
let inner = self.inner_g_rad();

self.as_tt_duration() - (ET_EPOCH_S * Unit::Second)
+ (0.001_658 * sin(inner)) * Unit::Second
+ (0.001_658 * inner.sin()) * Unit::Second
}

#[must_use]
/// Returns the Dynamic Barycentric Time (TDB) (higher fidelity SPICE ephemeris time) whose epoch is 2000 JAN 01 noon TAI (cf. <https://gssc.esa.int/navipedia/index.php/Transformations_between_Time_Systems#TDT_-_TDB.2C_TCB>)
pub fn as_tdb_seconds(&self) -> f64 {
// Note that we redo the calculation of as_tdb_duration to save computational cost
let inner = self.inner_g_rad();
self.as_tt_seconds() - (ET_EPOCH_S as f64) + (0.001_658 * sin(inner))
self.as_tt_seconds() - (ET_EPOCH_S as f64) + (0.001_658 * inner.sin())
}

/// For TDB computation, we're using f64 only because BigDecimal is far too slow for Nyx (uses FromStr).
fn inner_g_rad(&self) -> f64 {
use core::f64::consts::PI;
let g_rad = (PI / 180.0) * (357.528 + 35_999.050 * self.as_tt_centuries_j2k());

g_rad + 0.0167 * sin(g_rad)
g_rad + 0.0167 * g_rad.sin()
}

#[must_use]
Expand All @@ -853,7 +851,7 @@ impl Epoch {
#[must_use]
pub fn as_jde_tdb_duration(&self) -> Duration {
let inner = self.inner_g_rad();
let tdb_delta = (0.001_658 * sin(inner)) * Unit::Second;
let tdb_delta = (0.001_658 * inner.sin()) * Unit::Second;
self.as_jde_tt_duration() + tdb_delta
}

Expand Down Expand Up @@ -1456,7 +1454,7 @@ fn div_rem_f64(me: f64, rhs: f64) -> (i32, f64) {
}

fn div_euclid_f64(lhs: f64, rhs: f64) -> f64 {
let q = trunc(lhs / rhs);
let q = (lhs / rhs).trunc();
if lhs % rhs < 0.0 {
return if rhs > 0.0 { q - 1.0 } else { q + 1.0 };
}
Expand All @@ -1466,7 +1464,7 @@ fn div_euclid_f64(lhs: f64, rhs: f64) -> f64 {
fn rem_euclid_f64(lhs: f64, rhs: f64) -> f64 {
let r = lhs % rhs;
if r < 0.0 {
r + fabs(rhs)
r + rhs.abs()
} else {
r
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ pub mod prelude {
pub use {Duration, Epoch, Freq, Frequencies, TimeSeries, TimeUnits, Unit};
}

extern crate libm;
extern crate num_traits;

#[cfg(feature = "std")]
extern crate serde;
Expand Down

0 comments on commit ec8a0f8

Please sign in to comment.