forked from SkriptLang/Skript
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prevent long overflow in arithemetic (SkriptLang#7210)
* catch long overflows * don't compromise with precision uses Math.xExact code to catch overflows instead of relying on doubles. * Apply suggestions from code review Co-authored-by: Efnilite <[email protected]> --------- Co-authored-by: Efnilite <[email protected]>
- Loading branch information
1 parent
541dc12
commit 44a5f24
Showing
2 changed files
with
71 additions
and
10 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
test "long overflow, addition": | ||
set {_double-const} to 9000000000000000000.0 | ||
set {_long-const} to 9000000000000000000 | ||
|
||
loop 100 times: | ||
set {_double} to {_double} + {_double-const} | ||
set {_long} to {_long} + {_long-const} | ||
assert {_double} is {_long} with "long value did not overflow to a double" | ||
|
||
assert {_long} is 9000000000000000000 * 100.0 with "wrong final value" | ||
|
||
test "long underflow, subtraction": | ||
set {_double-const} to 9000000000000000000.0 | ||
set {_long-const} to 9000000000000000000 | ||
|
||
loop 100 times: | ||
set {_double} to {_double} - {_double-const} | ||
set {_long} to {_long} - {_long-const} | ||
assert {_double} is {_long} with "long value did not overflow to a double" | ||
|
||
assert {_long} is 9000000000000000000 * -100.0 with "wrong final value" | ||
|
||
test "long overflow, multiplication": | ||
set {_double} to 10.0 | ||
set {_long} to 10 | ||
|
||
loop 100 times: | ||
set {_double} to {_double} * 10 | ||
set {_long} to {_long} * 10 | ||
assert {_double} is {_long} with "long value did not overflow to a double" | ||
|
||
# the 6 is due to floating point error | ||
assert {_long} is 100000000000000060000000000000000000000000000000000000000000000000000000000000000000000000000000000000 with "wrong final value" | ||
|