You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The .is_nan(), .is_infinite(), .is_finite(), .is_normal(), .classify(), .is_sign_positive(), and .is_sign_negative() methods of the Float and FloatCore traits take ownership of self. This is problematic when writing generic code for container types which contain T: Float because in many cases, only &T is available and it's not guaranteed that T implements Copy or Clone. Taking ownership is also problematic for arbitrary precision floating point types which are expensive to clone. I can't think of any reasonable case where taking ownership would be necessary to implement these methods.
Taking &self instead of self should not incur any performance penalty for f32 and f64 as long as the call is inlined; the #[inline] attribute is already included on all of those methods. As a demonstration, call() and call_with_ref() generate the same assembly after optimization in this example (is_nan() and is_nan_ref() are inlined):
I propose doing either one of the of the following for .is_nan(), .is_infinite(), .is_finite(), .is_normal(), .classify(), .is_sign_positive(), and .is_sign_negative():
Change the existing methods to take &self instead of self.
Add methods (e.g. .is_nan_ref() or .is_nan_borrowed()) which take &self instead of self.
The text was updated successfully, but these errors were encountered:
This is problematic when writing generic code for container types which contain T: Float because in many cases, only &T is available and it's not guaranteed that T implements Copy or Clone.
Float and FloatCore both do currently require Copy, and the compiler should let you use that implicitly for any T: Float or T: FloatCore.
It's a separate question whether they should require Copy. It would probably be nicer to limit that to a PrimFloat analogue to PrimInt. I agree that all those methods should use &self if we didn't have a Copy bound. This would of course be a breaking change -- #47.
The
.is_nan()
,.is_infinite()
,.is_finite()
,.is_normal()
,.classify()
,.is_sign_positive()
, and.is_sign_negative()
methods of theFloat
andFloatCore
traits take ownership ofself
. This is problematic when writing generic code for container types which containT: Float
because in many cases, only&T
is available and it's not guaranteed thatT
implementsCopy
orClone
. Taking ownership is also problematic for arbitrary precision floating point types which are expensive to clone. I can't think of any reasonable case where taking ownership would be necessary to implement these methods.Taking
&self
instead ofself
should not incur any performance penalty forf32
andf64
as long as the call is inlined; the#[inline]
attribute is already included on all of those methods. As a demonstration,call()
andcall_with_ref()
generate the same assembly after optimization in this example (is_nan()
andis_nan_ref()
are inlined):I propose doing either one of the of the following for
.is_nan()
,.is_infinite()
,.is_finite()
,.is_normal()
,.classify()
,.is_sign_positive()
, and.is_sign_negative()
:Change the existing methods to take
&self
instead ofself
.Add methods (e.g.
.is_nan_ref()
or.is_nan_borrowed()
) which take&self
instead ofself
.The text was updated successfully, but these errors were encountered: