Skip to content

Commit

Permalink
Replace is_zero(x) with x == 0.0
Browse files Browse the repository at this point in the history
The function itself only seems to exist as a workaround
to a clippy warn/err, but with max_ulps = 0 it is almost a normal equality operation
  • Loading branch information
FreezyLemon authored and YeungOnion committed Aug 14, 2024
1 parent 039c545 commit 258a3f9
Show file tree
Hide file tree
Showing 8 changed files with 13 additions and 26 deletions.
9 changes: 4 additions & 5 deletions src/distribution/beta.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::distribution::{Continuous, ContinuousCDF};
use crate::function::{beta, gamma};
use crate::is_zero;
use crate::statistics::*;
use crate::{Result, StatsError};
use rand::Rng;
Expand Down Expand Up @@ -359,7 +358,7 @@ impl Continuous<f64, f64> for Beta {
0.0
}
} else if self.shape_b.is_infinite() {
if is_zero(x) {
if x == 0.0 {
f64::INFINITY
} else {
0.0
Expand Down Expand Up @@ -397,7 +396,7 @@ impl Continuous<f64, f64> for Beta {
f64::NEG_INFINITY
}
} else if self.shape_b.is_infinite() {
if is_zero(x) {
if x == 0.0 {
f64::INFINITY
} else {
f64::NEG_INFINITY
Expand All @@ -408,9 +407,9 @@ impl Continuous<f64, f64> for Beta {
let aa = gamma::ln_gamma(self.shape_a + self.shape_b)
- gamma::ln_gamma(self.shape_a)
- gamma::ln_gamma(self.shape_b);
let bb = if ulps_eq!(self.shape_a, 1.0) && is_zero(x) {
let bb = if ulps_eq!(self.shape_a, 1.0) && x == 0.0 {
0.0
} else if is_zero(x) {
} else if x == 0.0 {
f64::NEG_INFINITY
} else {
(self.shape_a - 1.0) * x.ln()
Expand Down
9 changes: 4 additions & 5 deletions src/distribution/binomial.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::distribution::{Discrete, DiscreteCDF};
use crate::function::{beta, factorial};
use crate::is_zero;
use crate::statistics::*;
use crate::{Result, StatsError};
use rand::Rng;
Expand Down Expand Up @@ -207,7 +206,7 @@ impl Distribution<f64> for Binomial {
/// (1 / 2) * ln (2 * π * e * n * p * (1 - p))
/// ```
fn entropy(&self) -> Option<f64> {
let entr = if is_zero(self.p) || ulps_eq!(self.p, 1.0) {
let entr = if self.p == 0.0 || ulps_eq!(self.p, 1.0) {
0.0
} else {
(0..self.n + 1).fold(0.0, |acc, x| {
Expand Down Expand Up @@ -252,7 +251,7 @@ impl Mode<Option<u64>> for Binomial {
/// floor((n + 1) * p)
/// ```
fn mode(&self) -> Option<u64> {
let mode = if is_zero(self.p) {
let mode = if self.p == 0.0 {
0
} else if ulps_eq!(self.p, 1.0) {
self.n
Expand All @@ -275,7 +274,7 @@ impl Discrete<u64, f64> for Binomial {
fn pmf(&self, x: u64) -> f64 {
if x > self.n {
0.0
} else if is_zero(self.p) {
} else if self.p == 0.0 {
if x == 0 {
1.0
} else {
Expand Down Expand Up @@ -306,7 +305,7 @@ impl Discrete<u64, f64> for Binomial {
fn ln_pmf(&self, x: u64) -> f64 {
if x > self.n {
f64::NEG_INFINITY
} else if is_zero(self.p) {
} else if self.p == 0.0 {
if x == 0 {
0.0
} else {
Expand Down
1 change: 0 additions & 1 deletion src/distribution/students_t.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::distribution::{Continuous, ContinuousCDF};
use crate::function::{beta, gamma};
use crate::is_zero;
use crate::statistics::*;
use crate::{Result, StatsError};
use rand::Rng;
Expand Down
5 changes: 2 additions & 3 deletions src/distribution/weibull.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::distribution::{Continuous, ContinuousCDF};
use crate::function::gamma;
use crate::is_zero;
use crate::statistics::*;
use crate::{consts, Result, StatsError};
use rand::Rng;
Expand Down Expand Up @@ -311,7 +310,7 @@ impl Continuous<f64, f64> for Weibull {
fn pdf(&self, x: f64) -> f64 {
if x < 0.0 {
0.0
} else if is_zero(x) && ulps_eq!(self.shape, 1.0) {
} else if x == 0.0 && ulps_eq!(self.shape, 1.0) {
1.0 / self.scale
} else if x.is_infinite() {
0.0
Expand All @@ -336,7 +335,7 @@ impl Continuous<f64, f64> for Weibull {
fn ln_pdf(&self, x: f64) -> f64 {
if x < 0.0 {
f64::NEG_INFINITY
} else if is_zero(x) && ulps_eq!(self.shape, 1.0) {
} else if x == 0.0 && ulps_eq!(self.shape, 1.0) {
0.0 - self.scale.ln()
} else if x.is_infinite() {
f64::NEG_INFINITY
Expand Down
3 changes: 1 addition & 2 deletions src/function/beta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
use crate::error::StatsError;
use crate::function::gamma;
use crate::is_zero;
use crate::prec;
use crate::Result;
use std::f64;
Expand Down Expand Up @@ -118,7 +117,7 @@ pub fn checked_beta_reg(a: f64, b: f64, x: f64) -> Result<f64> {
} else if !(0.0..=1.0).contains(&x) {
Err(StatsError::ArgIntervalIncl("x", 0.0, 1.0))
} else {
let bt = if is_zero(x) || ulps_eq!(x, 1.0) {
let bt = if x == 0.0 || ulps_eq!(x, 1.0) {
0.0
} else {
(gamma::ln_gamma(a + b) - gamma::ln_gamma(a) - gamma::ln_gamma(b)
Expand Down
3 changes: 1 addition & 2 deletions src/function/erf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
//! related functions
use crate::function::evaluate;
use crate::is_zero;
use std::f64;

/// `erf` calculates the error function at `x`.
Expand All @@ -13,7 +12,7 @@ pub fn erf(x: f64) -> f64 {
1.0
} else if x <= 0.0 && x.is_infinite() {
-1.0
} else if is_zero(x) {
} else if x == 0.0 {
0.0
} else {
erf_impl(x, false)
Expand Down
3 changes: 1 addition & 2 deletions src/function/gamma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
use crate::consts;
use crate::error::StatsError;
use crate::is_zero;
use crate::prec;
use crate::Result;
use std::f64;
Expand Down Expand Up @@ -216,7 +215,7 @@ pub fn checked_gamma_ur(a: f64, x: f64) -> Result<f64> {
qkm1 *= big_inv;
}

if !is_zero(qk) {
if qk != 0.0 {
let r = pk / qk;
let t = ((ans - r) / r).abs();
ans = r;
Expand Down
6 changes: 0 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,6 @@ pub mod stats_tests;

mod error;

// function to silence clippy on the special case when comparing to zero.
#[inline(always)]
pub(crate) fn is_zero(x: f64) -> bool {
ulps_eq!(x, 0.0, max_ulps = 0)
}

pub use crate::error::StatsError;

/// Result type for the statrs library package that returns
Expand Down

0 comments on commit 258a3f9

Please sign in to comment.