#21 % Modulo operator broken


typing in "1 % 32" results in .32, which is not modulo. I see what it's trying to do, it thinks i'm just typing in 32%. If there is a number after %, it should consider it modulo. Using libqalculate-0.9.7-4.fc17.x86_64.


    • labels: 854421 -->
  • Qalculate treats % as percent, and so 1%32 is treated as (1%) times 32. To get modulus, use mod(num,den).

    I suppose Qalculate could have an option to use % as a modulus operator. Therefore, I will move this ticket to "Feature Requests" and keep it open.

  • Cousteau

    I disagree about `%` being used for modulo. I guess it could be treated differently if it's a unary postfix operator or a binary operator (similarly to `-` which can mean either subtraction or minus), but I think that things like `20% X` can be useful and shouldn't require an extra `*` like `20%*X`.

    Instead, I'd make a `mod` operator (and a `rem` operator), so that `1 mod 32` is the same as `mod(1; 32)`. I think this is possible because Qalculate! is able to tell things like `day` and `day()` apart, although I don't know what would happen with things like `X mod (Y*Z)`.

    About the `%` operator, there seems to be some discussion about its meaning for negative numbers. Some languages such as Python treat it as Qalculate!'s `mod` (result has the same sign as denominator, so it goes from 0 to denominator), but in some other languages (e.g. C99, C++ or Javascript) it works as `rem` (same sign as numerator).