Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 43 additions & 24 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ impl Into<f64> for NotNaN<f64> {

/// Creates a NotNaN value from a Float.
///
/// Panics if the provided value is NaN.
/// Panics if the provided value is NaN or the computation results in NaN
impl<T: Float> From<T> for NotNaN<T> {
fn from(v: T) -> Self {
assert!(!v.is_nan());
Expand Down Expand Up @@ -239,35 +239,38 @@ impl<T: Float> Add for NotNaN<T> {

/// Adds a float directly.
///
/// Panics if the provided value is NaN.
/// Panics if the provided value is NaN or the computation results in NaN
impl<T: Float> Add<T> for NotNaN<T> {
type Output = Self;

fn add(self, other: T) -> Self {
assert!(!other.is_nan());
NotNaN(self.0 + other)
NotNaN::new(self.0 + other).expect("Addition resulted in NaN")
}
}

impl AddAssign for NotNaN<f64> {
fn add_assign(&mut self, other: Self) {
self.0 += other.0;
assert!(!self.0.is_nan(), "Addition resulted in NaN")
}
}

impl AddAssign for NotNaN<f32> {
fn add_assign(&mut self, other: Self) {
self.0 += other.0;
assert!(!self.0.is_nan(), "Addition resulted in NaN")
}
}

/// Adds a float directly.
///
/// Panics if the provided value is NaN.
/// Panics if the provided value is NaN or the computation results in NaN
impl AddAssign<f64> for NotNaN<f64> {
fn add_assign(&mut self, other: f64) {
assert!(!other.is_nan());
self.0 += other;
assert!(!self.0.is_nan(), "Addition resulted in NaN")
}
}

Expand All @@ -278,90 +281,97 @@ impl AddAssign<f32> for NotNaN<f32> {
fn add_assign(&mut self, other: f32) {
assert!(!other.is_nan());
self.0 += other;
assert!(!self.0.is_nan(), "Addition resulted in NaN")
}
}

impl<T: Float> Sub for NotNaN<T> {
type Output = Self;

fn sub(self, other: Self) -> Self {
NotNaN(self.0 - other.0)
NotNaN::new(self.0 - other.0).expect("Subtraction resulted in NaN")
}
}

/// Subtracts a float directly.
///
/// Panics if the provided value is NaN.
/// Panics if the provided value is NaN or the computation results in NaN
impl<T: Float> Sub<T> for NotNaN<T> {
type Output = Self;

fn sub(self, other: T) -> Self {
assert!(!other.is_nan());
NotNaN(self.0 - other)
NotNaN::new(self.0 - other).expect("Subtraction resulted in NaN")
}
}

impl SubAssign for NotNaN<f64> {
fn sub_assign(&mut self, other: Self) {
self.0 -= other.0;
assert!(!self.0.is_nan(), "Subtraction resulted in NaN")
}
}

impl SubAssign for NotNaN<f32> {
fn sub_assign(&mut self, other: Self) {
self.0 -= other.0;
assert!(!self.0.is_nan(), "Subtraction resulted in NaN")
}
}

/// Subtracts a float directly.
///
/// Panics if the provided value is NaN.
/// Panics if the provided value is NaN or the computation results in NaN
impl SubAssign<f64> for NotNaN<f64> {
fn sub_assign(&mut self, other: f64) {
assert!(!other.is_nan());
self.0 -= other;
assert!(!self.0.is_nan(), "Subtraction resulted in NaN")
}
}

/// Subtracts a float directly.
///
/// Panics if the provided value is NaN.
/// Panics if the provided value is NaN or the computation results in NaN
impl SubAssign<f32> for NotNaN<f32> {
fn sub_assign(&mut self, other: f32) {
assert!(!other.is_nan());
self.0 -= other;
assert!(!self.0.is_nan(), "Subtraction resulted in NaN")
}
}

impl<T: Float> Mul for NotNaN<T> {
type Output = Self;

fn mul(self, other: Self) -> Self {
NotNaN(self.0 * other.0)
NotNaN::new(self.0 * other.0).expect("Multiplication resulted in NaN")
}
}

/// Multiplies a float directly.
///
/// Panics if the provided value is NaN.
/// Panics if the provided value is NaN or the computation results in NaN
impl<T: Float> Mul<T> for NotNaN<T> {
type Output = Self;

fn mul(self, other: T) -> Self {
assert!(!other.is_nan());
NotNaN(self.0 * other)
NotNaN::new(self.0 * other).expect("Multiplication resulted in NaN")
}
}

impl MulAssign for NotNaN<f64> {
fn mul_assign(&mut self, other: Self) {
self.0 *= other.0;
assert!(!self.0.is_nan(), "Multiplication resulted in NaN")
}
}

impl MulAssign for NotNaN<f32> {
fn mul_assign(&mut self, other: Self) {
self.0 *= other.0;
assert!(!self.0.is_nan(), "Multiplication resulted in NaN")
}
}

Expand All @@ -377,123 +387,132 @@ impl MulAssign<f64> for NotNaN<f64> {

/// Multiplies a float directly.
///
/// Panics if the provided value is NaN.
/// Panics if the provided value is NaN or the computation results in NaN
impl MulAssign<f32> for NotNaN<f32> {
fn mul_assign(&mut self, other: f32) {
assert!(!other.is_nan());
self.0 *= other;
assert!(!self.0.is_nan(), "Multiplication resulted in NaN")
}
}

impl<T: Float> Div for NotNaN<T> {
type Output = Self;

fn div(self, other: Self) -> Self {
NotNaN(self.0 / other.0)
NotNaN::new(self.0 / other.0).expect("Division resulted in NaN")
}
}

/// Divides a float directly.
///
/// Panics if the provided value is NaN.
/// Panics if the provided value is NaN or the computation results in NaN
impl<T: Float> Div<T> for NotNaN<T> {
type Output = Self;

fn div(self, other: T) -> Self {
assert!(!other.is_nan());
NotNaN(self.0 / other)
NotNaN::new(self.0 / other).expect("Division resulted in NaN")
}
}

impl DivAssign for NotNaN<f64> {
fn div_assign(&mut self, other: Self) {
self.0 /= other.0;
assert!(!self.0.is_nan(), "Division resulted in NaN")
}
}

impl DivAssign for NotNaN<f32> {
fn div_assign(&mut self, other: Self) {
self.0 /= other.0;
assert!(!self.0.is_nan(), "Division resulted in NaN")
}
}

/// Divides a float directly.
///
/// Panics if the provided value is NaN.
/// Panics if the provided value is NaN or the computation results in NaN
impl DivAssign<f64> for NotNaN<f64> {
fn div_assign(&mut self, other: f64) {
assert!(!other.is_nan());
self.0 /= other;
assert!(!self.0.is_nan(), "Division resulted in NaN")
}
}

/// Divides a float directly.
///
/// Panics if the provided value is NaN.
/// Panics if the provided value is NaN or the computation results in NaN
impl DivAssign<f32> for NotNaN<f32> {
fn div_assign(&mut self, other: f32) {
assert!(!other.is_nan());
self.0 /= other;
assert!(!self.0.is_nan(), "Division resulted in NaN")
}
}

impl<T: Float> Rem for NotNaN<T> {
type Output = Self;

fn rem(self, other: Self) -> Self {
NotNaN(self.0 % other.0)
NotNaN::new(self.0 % other.0).expect("Rem resulted in NaN")
}
}

/// Calculates `%` with a float directly.
///
/// Panics if the provided value is NaN.
/// Panics if the provided value is NaN or the computation results in NaN
impl<T: Float> Rem<T> for NotNaN<T> {
type Output = Self;

fn rem(self, other: T) -> Self {
assert!(!other.is_nan());
NotNaN(self.0 % other)
NotNaN::new(self.0 % other).expect("Rem resulted in NaN")
}
}

impl RemAssign for NotNaN<f64> {
fn rem_assign(&mut self, other: Self) {
self.0 %= other.0;
assert!(!self.0.is_nan(), "Rem resulted in NaN")
}
}

impl RemAssign for NotNaN<f32> {
fn rem_assign(&mut self, other: Self) {
self.0 %= other.0;
assert!(!self.0.is_nan(), "Rem resulted in NaN")
}
}

/// Calculates `%=` with a float directly.
///
/// Panics if the provided value is NaN.
/// Panics if the provided value is NaN or the computation results in NaN
impl RemAssign<f64> for NotNaN<f64> {
fn rem_assign(&mut self, other: f64) {
assert!(!other.is_nan());
self.0 %= other;
assert!(!self.0.is_nan(), "Rem resulted in NaN")
}
}

/// Calculates `%=` with a float directly.
///
/// Panics if the provided value is NaN.
/// Panics if the provided value is NaN or the computation results in NaN
impl RemAssign<f32> for NotNaN<f32> {
fn rem_assign(&mut self, other: f32) {
assert!(!other.is_nan());
self.0 %= other;
assert!(!self.0.is_nan(), "Rem resulted in NaN")
}
}

impl<T: Float> Neg for NotNaN<T> {
type Output = Self;

fn neg(self) -> Self {
NotNaN(-self.0)
NotNaN::new(-self.0).expect("Negation resulted in NaN")
}
}

Expand Down