Skip to content

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
merged 5 commits into from
Jun 12, 2025

Conversation

sovdeeth
Copy link
Member

@sovdeeth sovdeeth commented Jun 7, 2025

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

@sovdeeth sovdeeth added the bug An issue that needs to be fixed. Alternatively, a PR fixing an issue. label Jun 7, 2025
@sovdeeth sovdeeth requested a review from a team as a code owner June 7, 2025 07:58
@sovdeeth sovdeeth requested review from Burbulinis and TheMug06 and removed request for a team June 7, 2025 07:58
@sovdeeth sovdeeth moved this to In Review in 2.12 Release Jun 7, 2025
@skriptlang-automation skriptlang-automation bot added the needs reviews A PR that needs additional reviews label Jun 7, 2025
@sovdeeth sovdeeth linked an issue Jun 7, 2025 that may be closed by this pull request
1 task
@sovdeeth sovdeeth requested a review from Efnilite June 7, 2025 21:03
@github-project-automation github-project-automation bot moved this from In Review to Awaiting Merge in 2.12 Release Jun 9, 2025
@skriptlang-automation skriptlang-automation bot added patch-ready A PR/issue that has been approved and is ready to be merged/closed for the next patch version. and removed needs reviews A PR that needs additional reviews labels Jun 9, 2025
@sovdeeth sovdeeth marked this pull request as draft June 12, 2025 05:44
@skriptlang-automation skriptlang-automation bot removed the patch-ready A PR/issue that has been approved and is ready to be merged/closed for the next patch version. label Jun 12, 2025
@sovdeeth sovdeeth marked this pull request as ready for review June 12, 2025 05:45
@skriptlang-automation skriptlang-automation bot added the patch-ready A PR/issue that has been approved and is ready to be merged/closed for the next patch version. label Jun 12, 2025
@Efnilite Efnilite merged commit 36b57d8 into SkriptLang:dev/patch Jun 12, 2025
5 checks passed
@skriptlang-automation skriptlang-automation bot added the completed The issue has been fully resolved and the change will be in the next Skript update. label Jun 12, 2025
@github-project-automation github-project-automation bot moved this from Awaiting Merge to Done in 2.12 Release Jun 12, 2025
@skriptlang-automation skriptlang-automation bot removed the patch-ready A PR/issue that has been approved and is ready to be merged/closed for the next patch version. label Jun 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug An issue that needs to be fixed. Alternatively, a PR fixing an issue. completed The issue has been fully resolved and the change will be in the next Skript update.
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

Evenly Divisible has problems with decimals
3 participants