StractOrg / stract

web search done right
https://stract.com
GNU Affero General Public License v3.0
1.94k stars 43 forks source link

Support '%' as modulo operator in calculator #190

Closed Ashvith10 closed 1 month ago

Ashvith10 commented 2 months ago

When I try calculating 100 % 12, I get 13 as the answer.

Screen Shot 2024-04-05 at 21 15 46

oeb25 commented 2 months ago

I did not look into the code, but it seems as if x%y is interpreted as (x/100)+y and not as modulo. For modulo one would need x mod y.

Note that we are using fend for evaluating everything in the calculator.

mikkeldenker commented 2 months ago

Yea it seems that fend always treats % as a unit. They have an open issue to also support it as a module operator, but it seems that it might not be trivial to implement in their parser (https://github.com/printfn/fend/issues/265).

I kinda want to label this as a bug as I agree this is not what the user expects. I don't know exactly how we should fix it though. I think we could either (in order of ascending difficulty and flexibility):

  1. Hot fix: replace all ([\d]+) *% *([\d]+) with $1 mod $2.
  2. It seems that the maintainer of fend wants this as well, so we might be able to fix it in their parser and upstream it.
  3. Write our own calculator.
printfn commented 1 month ago

Hello, fend maintainer here! I've implemented a fix for this in v1.4.8.

mikkeldenker commented 1 month ago

Awesome! Thanks for the heads up