Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

modulo interval arithmetic produces incorrect results #132

Open
lukevalenty opened this issue Oct 23, 2022 · 8 comments
Open

modulo interval arithmetic produces incorrect results #132

lukevalenty opened this issue Oct 23, 2022 · 8 comments

Comments

@lukevalenty
Copy link

The modulo interval arithmetic produces incorrect results. The resulting interval can be smaller than the possible values that could arise at runtime. I have an example on GitHub that produces this incorrect behavior in boost 1.80.

https://godbolt.org/z/36M3za3Px

    safe_signed_range<0, 100, native, loose_trap_policy> a{const_safe_t<50>{}};
    safe_signed_range<1, 100, native, loose_trap_policy> b{const_safe_t<100>{}};

    auto c = a % b;

c ends up with an interval of 0 to 0 inclusive, even though it contains the value 50.

@robertramey
Copy link
Member

I've looked at this and it appears you're right. I'll fix this. But now I'm bogged down in some other stuff so it will take a little while. Looks to me that the correct result for you example would be [0, 100) - range 0 - 99. Would you like to verify this? Maybe propose a fix?

@lukevalenty
Copy link
Author

Looks to me that the correct result for you example would be [0, 100) - range 0 - 99. Would you like to verify this?

Yes, 0 - 99 would be the correct result.

Maybe propose a fix?

I can do that. I'll take a look at the problem today and see if I can't propose a good solution.

@lukevalenty
Copy link
Author

Maybe it will take a few days 😅

@lukevalenty
Copy link
Author

Here's an algorithm that appears to work so far with some testing I've done. There's a lot to it:

https://stackoverflow.com/questions/31057473/calculating-the-modulo-of-two-intervals

@lukevalenty
Copy link
Author

Here's an algorithm that appears to work so far with some testing I've done. There's a lot to it:

https://stackoverflow.com/questions/31057473/calculating-the-modulo-of-two-intervals

This algorithm also has bugs. It gives the incorrect result for [50, 50] % [-200, -200]. I'm sure it does more things wrong. I'm going to look for other implementations/algorithms and see what's out there.

@robertramey
Copy link
Member

robertramey commented Nov 12, 2022 via email

@lukevalenty
Copy link
Author

lukevalenty commented Nov 13, 2022

Whelp, there's another wrinkle here. Python says 50 % -200 == -150, but C++ says 50 % -150 == 50.

https://godbolt.org/z/vYvP8M1rb

So the % operator in C++ isn't actually modulo, but something similar.

https://stackoverflow.com/questions/12089514/real-modulo-operator-in-c-c

Maybe the given algorithm is just fine for C++ % operator.

@robertramey
Copy link
Member

love this thread. Welcome to the world of library development!

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants