Navigation

    Fuze Arena Logo
    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Popular
    • Users
    • Groups
    • Help
    • Discord

    Modulo operation on negative numbers

    Help
    3
    7
    280
    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.
    • emptytheory
      emptytheory F last edited by

      An example:
      -1%3 returns -1, not 2.

      Is it supposed to be like this?

      1 Reply Last reply Reply Quote 0
      • M
        MikeDX last edited by

        not sure how that's even possible!

        emptytheory 1 Reply Last reply Reply Quote 0
        • emptytheory
          emptytheory F @MikeDX last edited by

          @MikeDX I made my own as a workaround. I think this is correct (please tell me if I'm wrong):

          function mod(a, n)
          return a - floor(a/n) * n
          
          1 Reply Last reply Reply Quote 2
          • M
            MikeDX last edited by

            This is definitely a bug. I'm going to test it here and file an issue on our side if i can reproduce it.

            Thanks for the report and the workaround!

            Minatsu 1 Reply Last reply Reply Quote 2
            • Minatsu
              Minatsu @MikeDX last edited by

              @MikeDX I think it cannot be said as a bug.
              In general, the sign of the result of a modulo operator depends on a programming language.

              Typical results are listed here:
              https://en.wikipedia.org/wiki/Modulo_operation

              emptytheory 1 Reply Last reply Reply Quote 2
              • emptytheory
                emptytheory F @Minatsu last edited by

                @Minatsu That article is a bit above my level of comprehension, but it seems the difference between languages is normally only the sign of the result. In this case the absolute value of the result is different than what I would have expected. I'm not very familiar with other languages, but are there others that works like this?

                1 Reply Last reply Reply Quote 0
                • M
                  MikeDX last edited by

                  having spoken to Luke about this, we believe -1%3 should be -1. This fits in with the modulo operator linked in the wiki page above.

                  Behind the scenes, Fuze uses the c modulo operator.

                  1 Reply Last reply Reply Quote 2
                  • First post
                    Last post