Skip to content

Commit

Permalink
suggestions and clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
aannleax committed Jan 13, 2025
1 parent 071a555 commit ff94d26
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 24 deletions.
20 changes: 10 additions & 10 deletions nemo-physical/src/datatypes/double.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use super::{run_length_encodable::FloatingStep, FloorToUsize, RunLengthEncodable
#[cfg(test)]
use quickcheck::{Arbitrary, Gen};

/// Wrapper for [f64`] that does not allow [`f64::NAN] values.
/// Wrapper for [f64] that excludes [f64::NAN] and infinite values
#[derive(Copy, Clone, Debug, PartialEq, Default)]
pub struct Double(f64);

Expand All @@ -33,7 +33,7 @@ impl Double {
/// Returns an error if `value` is [f32::NAN] or infinite.
pub fn new(value: f64) -> Result<Self, ReadingError> {
if !value.is_finite() {
return Err(ReadingError::InvalidFloat.into());
return Err(ReadingError::InvalidFloat);
}

Ok(Self(value))
Expand All @@ -44,7 +44,7 @@ impl Double {
/// # Panics
/// Panics if `value` is [f64::NAN] or not finite.
pub fn from_number(value: f64) -> Self {
if value.is_nan() {
if !value.is_finite() {
panic!("floating point values must be finite")
}

Expand All @@ -53,7 +53,7 @@ impl Double {

/// Computes the absolute value.
pub(crate) fn abs(self) -> Self {
Double::new(self.0.abs()).expect("operation returned in invalid float")
Double::new(self.0.abs()).expect("operation returns valid float")
}

/// Returns the logarithm of the number with respect to an arbitrary base.
Expand All @@ -63,34 +63,34 @@ impl Double {

/// Computes the sine of a number (in radians).
pub(crate) fn sin(self) -> Self {
Double::new(self.0.sin()).expect("operation returned in invalid float")
Double::new(self.0.sin()).expect("operation returns valid float")
}

/// Computes the cosine of a number (in radians).
pub(crate) fn cos(self) -> Self {
Double::new(self.0.cos()).expect("operation returned in invalid float")
Double::new(self.0.cos()).expect("operation returns valid float")
}

/// Computes the tangent of a number (in radians).
pub(crate) fn tan(self) -> Self {
Double::new(self.0.tan()).expect("operation returned in invalid float")
Double::new(self.0.tan()).expect("operation returns valid float")
}

/// Returns the nearest integer to `self`.
/// If a value is half-way between two integers, round away from 0.0.
pub(crate) fn round(self) -> Self {
Double::new(self.0.round()).expect("operation returned in invalid float")
Double::new(self.0.round()).expect("operation returns valid float")
}

/// Returns the nearest integer to `self`.
/// If a value is half-way between two integers, round away from 0.0.
pub(crate) fn ceil(self) -> Self {
Double::new(self.0.ceil()).expect("operation returned in invalid float")
Double::new(self.0.ceil()).expect("operation returns valid float")
}

/// Returns the largest integer less than or equal to `self`.
pub(crate) fn floor(self) -> Self {
Double::new(self.0.floor()).expect("operation returned in invalid float")
Double::new(self.0.floor()).expect("operation returns valid float")
}
}

Expand Down
28 changes: 14 additions & 14 deletions nemo-physical/src/datatypes/float.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::{
#[cfg(test)]
use quickcheck::{Arbitrary, Gen};

/// Wrapper for [f32] that does not allow [f32::NAN] values.
/// Wrapper for [f32] that excludes [f32::NAN] and infinite values
#[derive(Copy, Clone, Debug, PartialEq, Default)]
pub struct Float(f32);

Expand All @@ -30,9 +30,9 @@ impl Float {
///
/// # Errors
/// Returns an error if `value` is [f32::NAN] or infinite.
pub fn new(value: f32) -> Result<Self, Error> {
pub fn new(value: f32) -> Result<Self, ReadingError> {
if !value.is_finite() {
return Err(ReadingError::InvalidFloat.into());
return Err(ReadingError::InvalidFloat);
}

Ok(Float(value))
Expand All @@ -52,7 +52,7 @@ impl Float {

/// Computes the absolute value.
pub(crate) fn abs(self) -> Self {
Float::new(self.0.abs()).expect("operation returned in invalid float")
Float::new(self.0.abs()).expect("operation returns valid float")
}

/// Returns the logarithm of the number with respect to an arbitrary base.
Expand All @@ -62,33 +62,33 @@ impl Float {

/// Computes the sine of a number (in radians).
pub(crate) fn sin(self) -> Self {
Float::new(self.0.sin()).expect("operation returned in invalid float")
Float::new(self.0.sin()).expect("operation returns valid float")
}

/// Computes the cosine of a number (in radians).
pub(crate) fn cos(self) -> Self {
Float::new(self.0.cos()).expect("operation returned in invalid float")
Float::new(self.0.cos()).expect("operation returns valid float")
}

/// Computes the tangent of a number (in radians).
pub(crate) fn tan(self) -> Self {
Float::new(self.0.tan()).expect("operation returned in invalid float")
Float::new(self.0.tan()).expect("operation returns valid float")
}

/// Returns the nearest integer to `self`.
/// If a value is half-way between two integers, round away from 0.0.
pub(crate) fn round(self) -> Self {
Float::new(self.0.round()).expect("operation returned in invalid float")
Float::new(self.0.round()).expect("operation returns valid float")
}

/// Returns the smallest integer greater than or equal to `self`.
pub(crate) fn ceil(self) -> Self {
Float::new(self.0.ceil()).expect("operation returned in invalid float")
Float::new(self.0.ceil()).expect("operation returns valid float")
}

/// Returns the largest integer less than or equal to `self`.
pub(crate) fn floor(self) -> Self {
Float::new(self.0.floor()).expect("operation returned in invalid float")
Float::new(self.0.floor()).expect("operation returns valid float")
}
}

Expand Down Expand Up @@ -179,7 +179,7 @@ impl fmt::Display for Float {
}

impl TryFrom<f32> for Float {
type Error = Error;
type Error = ReadingError;

fn try_from(value: f32) -> Result<Self, Self::Error> {
Self::new(value)
Expand All @@ -196,9 +196,9 @@ impl TryFrom<usize> for Float {
type Error = Error;

fn try_from(value: usize) -> Result<Self, Self::Error> {
f32::from_usize(value)
.ok_or(Error::UsizeToFloatingPointValue(value))
.and_then(Float::new)
let res = f32::from_usize(value).ok_or(Error::UsizeToFloatingPointValue(value))?;

Ok(Float::new(res)?)
}
}

Expand Down

0 comments on commit ff94d26

Please sign in to comment.