Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a copysign function to f32 and f64 #55169

Merged
merged 3 commits into from
Oct 19, 2018
Merged
Show file tree
Hide file tree
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
29 changes: 29 additions & 0 deletions src/libstd/f32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,35 @@ impl f32 {
}
}

/// Returns a number composed of the magnitude of one number and the sign of
/// another.
///
/// Equal to `self` if the sign of `self` and `y` are the same, otherwise
/// equal to `-y`. If `self` is a `NAN`, then a `NAN` with the sign of `y`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"otherwise equal to -self", and not "-y", right? Or am I confused now?

Also: I'd rephrase the first sentence to "Returns a number composed of the magnitude of self and the sign of y". That's more explicit.

And yes, I know the PR is already merged, but I guess we should fix this in a new PR, if I'm right about this typo.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@LukasKalbertodt You're absolutely right. I'm happy to fix this in a followup PR. Good catch!

/// is returned.
///
/// # Examples
///
/// ```
/// #![feature(copysign)]
/// use std::f32;
///
/// let f = 3.5_f32;
///
/// assert_eq!(f.copysign(0.42), 3.5_f32);
/// assert_eq!(f.copysign(-0.42), -3.5_f32);
/// assert_eq!((-f).copysign(0.42), 3.5_f32);
/// assert_eq!((-f).copysign(-0.42), -3.5_f32);
///
/// assert!(f32::NAN.copysign(1.0).is_nan());
/// ```
#[inline]
#[must_use]
#[unstable(feature="copysign", issue="55169")]
pub fn copysign(self, y: f32) -> f32 {
unsafe { intrinsics::copysignf32(self, y) }
}

/// Fused multiply-add. Computes `(self * a) + b` with only one rounding
/// error, yielding a more accurate result than an unfused multiply-add.
///
Expand Down
29 changes: 29 additions & 0 deletions src/libstd/f64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,35 @@ impl f64 {
}
}

/// Returns a number composed of the magnitude of one number and the sign of
/// another.
///
/// Equal to `self` if the sign of `self` and `y` are the same, otherwise
/// equal to `-y`. If `self` is a `NAN`, then a `NAN` with the sign of `y`
/// is returned.
///
/// # Examples
///
/// ```
/// #![feature(copysign)]
/// use std::f64;
///
/// let f = 3.5_f64;
///
/// assert_eq!(f.copysign(0.42), 3.5_f64);
/// assert_eq!(f.copysign(-0.42), -3.5_f64);
/// assert_eq!((-f).copysign(0.42), 3.5_f64);
/// assert_eq!((-f).copysign(-0.42), -3.5_f64);
///
/// assert!(f64::NAN.copysign(1.0).is_nan());
/// ```
#[inline]
#[must_use]
#[unstable(feature="copysign", issue="55169")]
pub fn copysign(self, y: f64) -> f64 {
unsafe { intrinsics::copysignf64(self, y) }
}

/// Fused multiply-add. Computes `(self * a) + b` with only one rounding
/// error, yielding a more accurate result than an unfused multiply-add.
///
Expand Down