-
Notifications
You must be signed in to change notification settings - Fork 98
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
Implement clamp #377
base: master
Are you sure you want to change the base?
Implement clamp #377
Conversation
src/system.rs
Outdated
@@ -1083,6 +1098,15 @@ macro_rules! system { | |||
value: self.value.min(other.value), | |||
} | |||
} | |||
|
|||
#[inline(always)] | |||
fn clamp(self, min: Self, max: Self) -> Self { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This implementation isn't explicitly needed. Ord
has a default implementation. I supose it's possible a manual implementation could be performant.
src/system.rs
Outdated
pub fn clamp(self, min: Self, max: Self) -> Self | ||
where | ||
V: $crate::num::Float, | ||
{ | ||
Quantity { | ||
dimension: $crate::lib::marker::PhantomData, | ||
units: $crate::lib::marker::PhantomData, | ||
value: self.value.clamp(min.value, max.value), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is failing because it doesn't know where the value.clamp
function is defined for the generic type V
. The compiler is guessing the Ord
trait because it is the only one with a clamp
method. I see a couple solutions:
- Use https://docs.rs/num/latest/num/fn.clamp.html. This may not be as efficient for
f32
/f64
. - Create another
storage_types!
block just for float and then the code will compile
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you're going to raise the MSRV to 1.50.0, then I'd rather couple to the standard behaviour.
Create another
storage_types!
block just for float
You mean, alongside the one which already exists for Float
and Complex
?
If so, I'm getting something that compiles, but fails tests because of panics in the case that max
< min
. How do you put constraints on what quickcheck generates?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Both my suggestions were in the context of raising the MSRV to 1.50. The first, using num::clamp
, could be implemented in the impl<D, U, V> Quantity<D, U, V> ...
block and would apply to all underlying storage types, however because it's using num::clamp
it may not be as performant as a direct f32::clamp
call. The second suggestion would be another storage_types!
block that is just for float
(the existing can't be used because Complex
doesn't have a clamp
method) and would essentially proxy f32
/f64
clamp
. I lean towards the second option.
Reject invalid test input using TestResult::discard();
:
Lines 112 to 114 in 7c4b27b
if !Test::approx_eq(&f1, &i1) { | |
return TestResult::discard(); | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should I add some #[should_panic]
tests? If so, I'm not sure how these fit in to the test strucre.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, something like ???
with #[should_panic]
can be added as a non-quickcheck test up around line 263.
@iliekturtles Is there any way I can help get this merged? The last failure seems to be about old MSRV |
@dnsco, @jacg I took a brief look and it seems like rebasing+squashing on master fixes the previous build failures. |
clamp
has two distinct implementations, one inOrd
(which floats aren't) and another inf{32,64}
.I don't know how to separate these inside
uom
.