Duet3D Logo Duet3D
    • Tags
    • Documentation
    • Order
    • Register
    • Login

    Quotient and Modulo Mathematical Operations Request

    Scheduled Pinned Locked Moved
    General Discussion
    3
    8
    504
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • raykholoundefined
      raykholo
      last edited by

      Hi David,

      Would it be possible to have the mathematical operations of "Quotient" (divide without remainder) and Modulo (just the remainder)? I did not see any reference to these in the Meta Commands documentation.

      I want to build a filament changer (MMU2 style to feed different filament types/ colors) on top of a tool changer (grab different nozzle sizes) so that any combination of filament/ color and nozzle size is possible.

      In an example of 3 tools (say a 0.25, 0.4, and 0.8 volcano nozzle sizes) and 5 filament colors, we have 15 combinations to resolve. I would use the existing T# functionality to handle tool changing, and would write a similar construct in Macros to handle the filament color changer.

      I would use a Macro to resolve the 15 possible combinations into the existing T# commands to select tool, and Macros would handle the filament switching commands.

      dcd23d8f-2f34-42e9-b49b-1ab1c75b5dc3-image.png

      As you can see in the spreadsheet image here, we would need a Quotient and Modulo operation to do this. Quotient is Tool #, Modulo is Filament Color #.

      Thanks!

      dc42undefined 1 Reply Last reply Reply Quote 0
      • dc42undefined
        dc42 administrators @raykholo
        last edited by

        This could be provided, but I think it would be simpler to provide a round-to-integer operator. Then you could get the quotient by using round(a/b) and the remainder from a-b*round(a/b).

        Duet WiFi hardware designer and firmware engineer
        Please do not ask me for Duet support via PM or email, use the forum
        http://www.escher3d.com, https://miscsolutions.wordpress.com

        1 Reply Last reply Reply Quote 0
        • A Former User?
          A Former User
          last edited by A Former User

          Maybe i missunderstood that, but

          a  b div mod  round(a/b) a-b*round(a/b)
          1  5   0   1           0              1
          0  5   0   0           0              0
          2  5   0   2           0              2
          3  5   0   3           1             -2
          4  5   0   4           1             -1
          5  5   1   0           1              0
          6  5   1   1           1              1
          7  5   1   2           1              2
          8  5   1   3           2             -2
          9  5   1   4           2             -1
          10 5   2   0           2              0
          11 5   2   1           2              1
          12 5   2   2           2              2
          13 5   2   3           3             -2
          14 5   2   4           3             -1
          

          division without remainder would be equivalent of floor(a/b) discarding, round(a/b) would round up?
          (public, don't mess it up too badly https://docs.google.com/spreadsheets/d/1jaGuxY3Faa-QBwInEJwWeWvN_0XcVKpzSUmcAWgXuP8/edit#gid=0)

          dc42undefined 1 Reply Last reply Reply Quote 0
          • dc42undefined
            dc42 administrators @A Former User
            last edited by

            I was assuming that round would behave like floor. In fact I should call it floor, not round.

            Duet WiFi hardware designer and firmware engineer
            Please do not ask me for Duet support via PM or email, use the forum
            http://www.escher3d.com, https://miscsolutions.wordpress.com

            1 Reply Last reply Reply Quote 0
            • A Former User?
              A Former User
              last edited by

              to me mod() would be much better readability than a-b*floor(a/b), but I guess not everyone will be familiar with div() and mod() either. floor gets the job done at least 👍

              1 Reply Last reply Reply Quote 0
              • dc42undefined
                dc42 administrators
                last edited by dc42

                I could introduce the % operator to mean mod (often the same as remainder), but integer divide would need another symbol. I deliberately made the / operator mean floating point division, so that users needn't worry about whether the operands are floats or ints.

                Duet WiFi hardware designer and firmware engineer
                Please do not ask me for Duet support via PM or email, use the forum
                http://www.escher3d.com, https://miscsolutions.wordpress.com

                1 Reply Last reply Reply Quote 0
                • raykholoundefined
                  raykholo
                  last edited by raykholo

                  I agree that having to use a-b*floor(a/b) for remainder hurts readability, especially when inside complex expressions. Please please please have a dedicated mod operation. Either syntax of mod(a,b) or a%b is completely fine.

                  For the quotient, rounding is surely another option and floor(a,b) would be fine for our purposes.

                  Also, please note that I used syntax of function (a,b) and not function (a/b) above. I have usually seen it as comma separated parameters and I am concerned that using / would confuse the system into thinking it is division inside the function.

                  Thank you very much for the consideration.

                  1 Reply Last reply Reply Quote 0
                  • dc42undefined
                    dc42 administrators
                    last edited by

                    I have implemented floor(x) and mod(x, y) in the latest internal builds of RRF3 at https://www.dropbox.com/sh/3azy1njy3ayjsbp/AACquxr2m00eV568RZg5QG5wa?dl=0.

                    Duet WiFi hardware designer and firmware engineer
                    Please do not ask me for Duet support via PM or email, use the forum
                    http://www.escher3d.com, https://miscsolutions.wordpress.com

                    1 Reply Last reply Reply Quote 1
                    • First post
                      Last post
                    Unless otherwise noted, all forum content is licensed under CC-BY-SA