-
-
Notifications
You must be signed in to change notification settings - Fork 394
Add tolerance to divisible by condition #7931
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Efnilite
merged 5 commits into
SkriptLang:dev/patch
from
sovdeeth:patch/fix-divisible-by
Jun 12, 2025
Merged
Add tolerance to divisible by condition #7931
Efnilite
merged 5 commits into
SkriptLang:dev/patch
from
sovdeeth:patch/fix-divisible-by
Jun 12, 2025
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 task
Efnilite
requested changes
Jun 7, 2025
Co-authored-by: Efnilite <[email protected]>
Efnilite
approved these changes
Jun 8, 2025
Burbulinis
approved these changes
Jun 9, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
The divisible by expression is particularly susceptible to floating point error compounding during the modulo operation. This means many common divisions like
0.3/0.1
return false when they should be true, due to slightly fp inaccuracies.Solution
I may have gone a bit overboard. Nearly all reasonable cases can be fixed with a check against Skript.EPSILON, which ensures the result is within 1e-10 of evenly divisible. However, this fails when working with large or small numbers, or a combination of the two. For example, 3,000,000 / (1/3) fails as the error is slightly larger than 1e-10.
Using ULP is sadly not helpful here, since the ulp for 3 million is 0.25 (way too large) and for 1/3 it is roughly 1e-17 (way too small). A fixed epsilon also fails if the user wants to work with small numbers, as a dividend of 1e-10 or smaller will always return true, no matter the divisor.
This PR implements a solution by defaulting to 1e-10 as the tolerance, but allowing the user to manually provide a tolerance if needed. This should covers nearly all cases by default and provide options for users it does not cover.
A few extra shortcut paths were added to speed things up.
Testing Completed
Additional tests were added to CondIsDivisibleBy.sk to cover the new functionality.
Supporting Information
Completes: #7930
Related: none