Skip to content
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

Make mathematical operators and functions automatically cast string operands/arguments to numbers #45

Open
ghost opened this issue Apr 29, 2023 · 5 comments
Labels
bug Something isn't working good first issue Good for newcomers

Comments

@ghost
Copy link

ghost commented Apr 29, 2023

For some reason I can't do arithmetic operations with array elements.
(Type conversion error)
Example: dim array:push 1,array:print array:print array[0]+1

@James-Livesey
Copy link
Member

That's strange... It seems to work as expected for me!

image

Have you got any errors in DevTools about it? Do you know if the type conversion error occurs for print array or just print array[0]+1?

@ghost
Copy link
Author

ghost commented Apr 29, 2023

I forgot to mention, yes, the error only occurs at array[0]+1. What is weird, is that it works on the phone, but both devices keep atto up-to-date. I can't check it on the PC right now, but when I can, I will write more info.

@ghost
Copy link
Author

ghost commented Apr 29, 2023

What is even weirder is that it still works on my phone, but when it's in this code (it's on the bottom of the comment), it doesn't work on both devices (coord2Array[0] is a normal number). I tried running this code (on the phone) and tried these commands:
dim abc:push 23,abc:print abc[0]+2 (returns 25, works ok)
print coord2Array[0]+2 (Type conversion error)

Basically, it's like this:

  • Works on PC? No
  • Works on mobile? Yes
  • Works on PC in this code? No
  • Works on mobile in this code? No

Even if I am doing something wrong, the simple dim x:push 1,x:print x+1 command returns different results on different devices. I don't think it should have problems converting 2 into a number though.

Also, "this" code:
https://atto.devicefuture.org/?code=1%20rem%20%7Bredacted%7D%0A2%20rem%20%7Bredacted%7D%0A10%20rem%20%5B%5D%0A20%20boardX%25%3D19%0A30%20boardY%25%3D14%0A40%20graphSize%25%3D16%0A50%20arrows%3Dsplit(%22wdsa%22)%20rem%20yes...w%20d%20s%20a.%0A60%20rem%20%5BGenerate%20free%20space%5D%0A70%20dim%20free%0A80%20for%20freeY%25%3D0%20to%20boardY%25%0A90%20for%20freeX%25%3D0%20to%20boardX%25%0A100%20push%20freeX%25%3B%22%3B%22%3BfreeY%25%2Cfree%20%0A110%20next%0A120%20next%0A130%20rem%20%5BGenerate%20the%20snake%5D%0A140%20dim%20snake%0A150%20for%20snakePart%25%3D0%20to%204%0A160%20snakePartCell%24%3D1%2BSnakePart%25%3B%22%3B%22%3B1%0A170%20push%20snakePartCell%24%2Csnake%0A180%20remove%20free%2Cfind(snakePartCell%24%2Cfree)%0A190%20next%0A200%20rem%20%5B%5D%0A210%20goAngle%25%3D1%0A220%20gosub%20540%0A230%20rem%20%5BGame%20loop%5D%0A240%20rem%20%5BRead%20key%20data%5D%0A250%20input%20ctrl%24%20rem%20Mobile-friendly.%0A260%20if%20find(ctrl%24%2Carrows)!%3D-1%0A270%20goAngle%25%3Dfind(ctrl%24%2Carrows)*90%0A280%20end%0A290%20rem%20%5BMove%20the%20snake%5D%0A300%20coord2Array%3Dsplit(last(snake)%2C%22%3B%22)%0A310%20push%20coord2Array%5B0%5D%2Bsin(goAngle%25)%3B%22%3B%22%3Bcoord2Array%5B1%5D%2Bcos(goAngle%25)%2Csnake%0A320%20rem%20%5BCheck%20apple%5D%0A330%20if%20last(snake)%3Dapple%24%0A340%20gosub%20540%0A350%20apples%25%3Dapples%25%2B1%0A360%20else%0A370%20remove%20snake%2C0%0A380%20end%0A390%20rem%20%5BCheck%20loss%5D%0A400%20if%20find(last(snake)%2Cfree)%3D-1%0A410%20print%20%22You%20lost!%20Score%20(apples)%3A%20%22%3Bapples%25%0A420%20stop%0A430%20end%0A440%20rem%20%5BRendering%5D%0A450%20cls%0A460%20graphX%25%3DgraphSize%25*boardX%25%0A470%20graphY%25%3DgraphSize%25*boardY%25%0A480%20rem%20%5BRendering%3A%20Background%5D%3A%0A490%20rem%20%5BRendering%3A%20Snake%5D%0A495%20fg%20%22rgb%22%2C0%2C128%2C0%3A%0A500%20rem%20%5BRendering%3A%20Snake%20head%5D%0A505%20fg%20%22rgb%22%2C0%2C255%2C0%3A%0A510%20rem%20%5BRendering%3A%20Apple%5D%0A515%20fg%20%22rgb%22%2C255%2C0%2C0%3A%0A520%20goto%20240%0A530%20rem%20%5BApple%5D%0A540%20apple%24%3Dfree%5Bfloor(random*len(free))%5D%0A550%20return
I originally made it in a simple text editor and still debugging it.

@ghost
Copy link
Author

ghost commented May 3, 2023

Weirdly, it works on PC now, but not in the code. Here's a DevTools screenshot of running this code on PC. There are no new error messages after the conversion error.
atto devtools 9

@James-Livesey
Copy link
Member

It might not seem immediately obvious, but coord2Array items appear to be strings, which won't work with the + operator! To get it to work, you'll need to modify your program to be:

304 value0=coord2Array[0]:value1=coord2Array[1]
305 push value0%+sin(goAngle%);";";value1%+cos(goAngle%),snake

At the moment, list items can't be casted to a number like coord2Array[0]%, so the items must be stored in variables that can then be subsequently casted. It might be worth modifying atto and making the + operator automatically cast strings to numbers (just like how ; basically casts numbers to strings when used) to prevent the confusing type conversion error, so I'll keep the issue open for that.

@James-Livesey James-Livesey changed the title Can't use array elements in math Make + operator automatically cast string operands to numbers May 3, 2023
@James-Livesey James-Livesey added bug Something isn't working good first issue Good for newcomers labels May 3, 2023
@James-Livesey James-Livesey changed the title Make + operator automatically cast string operands to numbers Make mathematical operators and functions automatically cast string operands/arguments to numbers May 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

1 participant