-
Notifications
You must be signed in to change notification settings - Fork 6
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 the Add
/Div
/Mul
/Sub
blocks
#19
base: master
Are you sure you want to change the base?
Conversation
Add
/Div
/Mul
/Sub
blocks
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.
LGTM otherwise but I suggest to change the while self.input.recv { ..., self.output.send(&res) }
to while self.input.recv { ... } self.output.send(&result.unwrap_or(<default-value>))
with <default-value>
being 0.0
for Add
and Sub
blocks, and 1.0
for Mul
and Div
blocks.
Reasoning for the defaults comes from the identity elements of addition or multiplication, e.g. adding/subtracting 0
does nothing and multiplying/diving by 1
does nothing and thus they're used as the values when the input set is empty, adding no numbers together equals 0 and multiplying no numbers together equals 1.
Also noting that as a future improvement, it's quite easy to switch these implementations to support any numerical types (f32,f64,u32,u64, etc.) by defining the blocks with generics as such:
pub struct Add<T: Message + core::ops::Add<Output = T> + num_traits::Zero = f64> { /* ... */ }
pub struct Sub<T: Message + core::ops::Sub<Output = T> + num_traits::Zero = f64> { /* ... */ }
pub struct Mul<T: Message + core::ops::Mul<Output = T> + num_traits::One = f64> { /* ... */ }
pub struct Div<T: Message + core::ops::Div<Output = T> + PartialEq + num_traits::One + num_traits::Zero = f64> { /* ... */ }
In summary what those generics are saying is, taking for example the Add
block:
- Anything that implements
Message
(so that we can send it over a port) - And implements
core::ops::Add
(so that we can callx + y
on the types) - And implements
num_traits::Zero
(so that we can callT::zero()
for the identity element as the starting value for the summing) = f64
, by default usef64
if nothing else was provided.
Let me know if you want the patch for those changes for this PR, otherwise I'll open another PR after this is merged.
Co-authored-by: Samuel Sarle <samuel@sarle.com> Signed-off-by: Oleksandr Yakovenko <35838571+AlexSanches1@users.noreply.github.com>
Co-authored-by: Samuel Sarle <samuel@sarle.com> Signed-off-by: Oleksandr Yakovenko <35838571+AlexSanches1@users.noreply.github.com>
Co-authored-by: Samuel Sarle <samuel@sarle.com> Signed-off-by: Oleksandr Yakovenko <35838571+AlexSanches1@users.noreply.github.com>
Co-authored-by: Samuel Sarle <samuel@sarle.com> Signed-off-by: Oleksandr Yakovenko <35838571+AlexSanches1@users.noreply.github.com>
No description provided.