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

Implement the Add/Div/Mul/Sub blocks #19

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open

Conversation

AlexSanches1
Copy link
Contributor

No description provided.

@artob artob changed the title Implement Add/Div/Mul/Sub blocks Implement the Add/Div/Mul/Sub blocks Dec 4, 2024
Copy link
Contributor

@SamuelSarle SamuelSarle left a 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:

  1. Anything that implements Message (so that we can send it over a port)
  2. And implements core::ops::Add (so that we can call x + y on the types)
  3. And implements num_traits::Zero (so that we can call T::zero() for the identity element as the starting value for the summing)
  4. = f64, by default use f64 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.

lib/protoflow-blocks/src/blocks/math/add.rs Outdated Show resolved Hide resolved
lib/protoflow-blocks/src/blocks/math/div.rs Outdated Show resolved Hide resolved
lib/protoflow-blocks/src/blocks/math/mul.rs Outdated Show resolved Hide resolved
lib/protoflow-blocks/src/blocks/math/sub.rs Outdated Show resolved Hide resolved
@SamuelSarle SamuelSarle assigned AlexSanches1 and unassigned artob Dec 9, 2024
lib/protoflow-blocks/src/blocks/math/sub.rs Outdated Show resolved Hide resolved
lib/protoflow-blocks/src/blocks/math/mul.rs Outdated Show resolved Hide resolved
lib/protoflow-blocks/src/blocks/math/div.rs Outdated Show resolved Hide resolved
lib/protoflow-blocks/src/blocks/math/add.rs Outdated Show resolved Hide resolved
AlexSanches1 and others added 4 commits December 9, 2024 22:35
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>
@AlexSanches1 AlexSanches1 assigned artob and unassigned AlexSanches1 Dec 13, 2024
@artob artob added the enhancement New feature or request label Dec 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants