Skip to content

Casting float to integer in LitterBox translation#1

Open
emmableu wants to merge 2 commits intose2p:masterfrom
emmableu:master
Open

Casting float to integer in LitterBox translation#1
emmableu wants to merge 2 commits intose2p:masterfrom
emmableu:master

Conversation

@emmableu
Copy link
Contributor

To resolve a bug that - when translating a Scratch program that has floating points in "change x by", "change y by", it gives an error: Exception in thread "main" java.lang.RuntimeException: Expected type integer but got 5.5, we have casted all float literals to int unless expectFloat is explicit.

However, this may have caused us to cast some that are not supposed to be integers to integers. So please let us know if there are other ways to change the translation method.

@Nina-Nicole
Copy link
Contributor

Thank you for your pull request!

However, this may have caused us to cast some that are not supposed to be integers to integers. So please let us know if there are other ways to change the translation method.

You are right here - you propose to use integer even if the ORIGINAL type is expected - we suggest to round floats to ints only if it is explicitly expected.

Furthermore, casting floats to ints cuts off the decimal places instead of rounding. That results in unneccessary imprecision and we suggest to use round() instead of casts.

In the long term we also consider to allow floats for the "change x by" and "change y by" blocks, as rounding floats in general discards relevant information, so why not keep it 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants