-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Make Scratch 3 comparisons with +/-Infinity match Scratch 2 #1754
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
Conversation
Nice! My changes in #1701 are almost the same, except you treat lowercase infinity as a number (this isn't needed because string comparison works correctly), this is probably a good idea. My PR was closed because it was alleged that the issue may have been wider than this Infinity minus Infinity issue and so should be kept internal. That perspective (which I think might be mistaken) would probably apply to this PR too |
29d5cb3
to
43c5d5c
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Stylistic suggestions
@joker314 is right! In scratch 2:
In scratch 3, the changes I introduced will make all three of these equal The question here is: should scratch 3 mimic this different treatment in of |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Initial review: two minor comments to start addressing,
-
From a quick search through our code base, it looks like there is one place (previously added by me, oops!) where we use
Cast.toNumber(value)
where we probably meanNumber(value)
. This is in src/engine/comment.js, do you mind changing the calls there in this PR? -
Not strictly necessary, since you wrote a lot of tests for this PR (thank you!), but a nice to have, we might want to add some execute integration tests like the ones found in
test/fixtures/execute/
so that we can ensure that 3.0 behaves the same way as 2.0
hi @kchadha, I addressed the Caste.toNumber in the last commit! Thanks for catching that. I think you are right, there should be an execute integration tests. I'll try to write one next week! Are these tests always run on both scratch 2 and scratch 3? When you get back to reviewing, please look closely at all the unit tests and make sure this is the way we want scratch 3 to behave. There are a few points of divergence from Scratch 2. When writing the tests, I didn't realize that the string "infinity" is treated differently in a comparison block than a math block (like minus or division). In Scratch 3 -- should we copy the scratch 2 behavior, or make the treatment of the string "infinity" the same in comparison and math blocks? The unit tests are written assuming that the string "infinity" is treated the same across all blocks. |
@spectranaut: @kchadha and I were talking about this and feel like the use of "Infinity" in comparison and math blocks is probably an edge case that only very high-level Scratchers use, not to mention it only takes the English word, and we should just keep its behavior the same in scratch 3 as it was in scratch 2 (we want to avoid changing things unless they are a big help to understanding). It looks like the behavior in Scratch 2 is that all the math operation blocks only recognize capitalized Infinity, and the equals block is casting both sides to string and doing a case insensitive comparison. I'll try to dig around more in the flash code to find what it is exactly. |
Here is where we define the function of the blocks. So +, -, *, / see their arguments as numargs, and <, > = see their arguments as args Here is where numarg and arg are defined. numarg is just casting to AS3 Number Here is where compare is defined for >, <, =. It looks like it's trying to compare as numbers and, if that fails, compares the two values as strings. |
Note that This has a check for at least one digit, using a loop. We can probably use a regular expression instead: it's looking for a digit character, so that a string of just whitespace will not be cast to 0, but instead result in NaN, However, this also (accidentally?) excludes Infinity and -Infinity, since neither contain digits. Hence, the comparison blocks do not consider those numbers. In Scratch 3.0, we have a utility Should we change the utility to be something which casts to a number (like |
In the same line of reasoning as before, I think we should keep the behavior the same as it was in Scratch 2. So Infinity will equal infinity in the equals block. |
83a9f55
to
70f00e3
Compare
This looks good. Just a slight compatibility issue, not in the comparison block, but caused by the same root problem: Take a look at this incompatibility (in src/blocks/scratch3_looks.js):
In Scratch 2.0, Infinity is not considered a number (since it contains no digits), and is therefore considered a non-existent costume--remaining at costume 2. However, the current Scratch 3.0 implementation parses this as a number, which causes it to go to the first costume (1). I think maybe creating a method to match 2.0's |
@joker314 let's make a separate bug for instances like that where Infinity is getting interpreted incorrectly as a string/number |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great! Just the one lint fix is needed to make Travis pass.
Sorry I didn't catch that, fixed @fsih ! :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thank you!
Resolves
Resolves #1699
Proposed Changes
This PR makes changes to
scratch-vm/src/util/cast.js
'sCast.compare
:*
Cast.compare
relies on the javascript subtraction operator (ifv1 - v2
=== 0, then we they are equal) to compare numbers. This will not work withInfinty
becauseInfinity - Infinity = NaN
, so there is extra logic to handle this case.Reason for Changes
Scratch 3 does not treat infinity the same as Scratch 2.
Test Coverage
In this PR I added two additional unit test files. You can also see the original Scratch2 test of comparisons here: https://scratch.mit.edu/projects/260178207/