Discuss Scratch

BzarrTehAxolotl
New Scratcher
4 posts

Is it a string? Or a number?

Hi,

So I was thinking of a block idea that could be useful, the string or number block!

isastringreturns true if it's a string, false if notisanumberreturns true if a number, false if not

I know that there is a workaround using like division or something, but it is very confusing and this could help be more accessible

var/1=var

Let me know what you think
-Bzarr
BzarrTehAxolotl
New Scratcher
4 posts

Is it a string? Or a number?

Sorry scratchblock fail

isastring?isanumber?isvaranumber?

repeatuntilisansweranumber?askGive me a year!andwait

Last edited by BzarrTehAxolotl (July 30, 2022 00:56:05)

Za-Chary
Scratcher
1000+ posts

Is it a string? Or a number?

Is “12345” a string?

The answer is “it depends.” By running a block like this, Scratch will interpret “12345” as a number:

12345+10 returns "12355"

But by running a block like this, Scratch will interpret “12345” as a string:

join12345apple returns "12345apple"

So, how will it tell whether or not what the intent is? Or are you more concerned with whether or not the input is a number? If so, then I think "is [ ] a number" would be the best and most unambiguous block for your suggestion.
BzarrTehAxolotl
New Scratcher
4 posts

Is it a string? Or a number?

Yeah, I think the ‘is a number’ makes the most sense, since you could just stick a ‘not’ in front of it.

notisanumber?functions pretty similarly
banana439monkey
Scratcher
1000+ posts

Is it a string? Or a number?

i'm worried…
scratch's typing is just reckless in the first place
like why do variables get assigned the string type but then you can increase the string by 1 and it works like an integer???
even javascript wouldn't do that!
in theory we could have a typeof () block but that's rather ambiguous, is 1 a string or an integer? sure, in () it's an integer but you can technically put arbitrary strings into integer-only inputs. it becomes all over the place

Banana
-iviedwall-
Scratcher
500+ posts

Is it a string? Or a number?

Za-Chary wrote:

Is “12345” a string?

The answer is “it depends.” By running a block like this, Scratch will interpret “12345” as a number:

12345+10 returns "12355"

But by running a block like this, Scratch will interpret “12345” as a string:

join12345apple returns "12345apple"

So, how will it tell whether or not what the intent is? Or are you more concerned with whether or not the input is a number? If so, then I think "is [ ] a number" would be the best and most unambiguous block for your suggestion.
The join block converts numbers into strings, so it will be possible to join them.
ScolderCreations
Scratcher
1000+ posts

Is it a string? Or a number?

This would be incredibly confusing because the type of a variable in Scratch is very fluid, even more fluid than the language it's based in, JavaScript. This means the type will usually change depending on the situation is used in, and while I can see how this would be implemented, I can see ways it wouldn't be better than the workaround, and would probably be a very confusing concept. Definitely not how I'd like people to be introduced to types, since in languages like python or cpp, typing is not as fluid.

Last edited by ScolderCreations (July 30, 2022 05:28:24)

Maximouse
Scratcher
1000+ posts

Is it a string? Or a number?

I think this could be useful if it checked if something is a valid number, not if it is stored as a JavaScript number internally.
SavetheAtlantic
Scratcher
1000+ posts

Is it a string? Or a number?

Here's a question: is Infinity a number? I, and most people well-versed in math, would say that it is not, it is a concept. However, if you ask Scratch, then it would say no. Scratch– and the language it's based on, Javascript– only use 64-bit double-precision floating point numbers, and due to quirks of floats, it is in fact a number. 7FF0 0000 0000 0000 in hex, to be precise. Not only does Scratch think infinity is a number, it can also do arithmetic with it! Dividing by 0, which does not give a result (see here) is instead equal to infinity. With this infinity value, lots of weird stuff happens. For example, Infinity * 0 = NaN; Infinity * 1 = Infinity; Infinity / Infinity = NaN; Infinity - Infinity = NaN; and Infinity < NaN.

This goes to show that Scratch thinks Infinity is a number that can be used in mathematical operations, when clearly it has no real purpose to be used in said operations. So how should Scratch handle it? The unintuitive, technically correct way of saying it is a number, or the incorrect but logical way of saying it is a string. After all, Scratch claims that Infinity the number is equal to the string Infinity. You could also say Infinity is neither, but that's really bizarre.

Come to think of it, what about NaN? It, by definition, is Not a Number, but it's also not a string and it is technically a valid float value.
SavetheAtlantic
Scratcher
1000+ posts

Is it a string? Or a number?

Also, what about undocumented things like Scratch accepting hexadecimal, binary, or octal? If you have a number in any of those systems prefixed with the string “0x”, “0b”, or “0o” respectively, and try to do math operations on it, Scratch will convert that string into decimal. So, 0x1A + 0b100 is converted to 26 + 4, which equals 30. Are those strings or numbers?
yavuz61035
Scratcher
500+ posts

Is it a string? Or a number?

SavetheAtlantic wrote:

Here's a question: is Infinity a number? I, and most people well-versed in math, would say that it is not, it is a concept. However, if you ask Scratch, then it would say no. Scratch– and the language it's based on, Javascript– only use 64-bit double-precision floating point numbers, and due to quirks of floats, it is in fact a number. 7FF0 0000 0000 0000 in hex, to be precise. Not only does Scratch think infinity is a number, it can also do arithmetic with it! Dividing by 0, which does not give a result (see here) is instead equal to infinity. With this infinity value, lots of weird stuff happens. For example, Infinity * 0 = NaN; Infinity * 1 = Infinity; Infinity / Infinity = NaN; Infinity - Infinity = NaN; and Infinity < NaN.

This goes to show that Scratch thinks Infinity is a number that can be used in mathematical operations, when clearly it has no real purpose to be used in said operations. So how should Scratch handle it? The unintuitive, technically correct way of saying it is a number, or the incorrect but logical way of saying it is a string. After all, Scratch claims that Infinity the number is equal to the string Infinity. You could also say Infinity is neither, but that's really bizarre.

Come to think of it, what about NaN? It, by definition, is Not a Number, but it's also not a string and it is technically a valid float value.

btw i made it small to save space
floating point is all about estimation; a number as floating point is defined as the number which is closer than that floating point number than any else
also, infinity is defined as something so large it cannot be represented as floating point
so that explains why 1 / 0 is Infinity in floating point

anyways, let's talk about the op

i think that the “is number” block should only return true if it detects -, 0-9, ., e and + (a large number is represented as ae+b and a small one is ae-b)
SavetheAtlantic
Scratcher
1000+ posts

Is it a string? Or a number?

yavuz61035 wrote:

floating point is all about estimation; a number as floating point is defined as the number which is closer than that floating point number than any else
also, infinity is defined as something so large it cannot be represented as floating point
so that explains why 1 / 0 is Infinity in floating point

anyways, let's talk about the op

i think that the “is number” block should only return true if it detects -, 0-9, ., e and + (a large number is represented as ae+b and a small one is ae-b)
I am aware of that. Infinity, however, is a valid floating point value as much as any other number.
banana439monkey
Scratcher
1000+ posts

Is it a string? Or a number?

-iviedwall- wrote:

Za-Chary wrote:

Is “12345” a string?

The answer is “it depends.” By running a block like this, Scratch will interpret “12345” as a number:

12345+10 returns "12355"

But by running a block like this, Scratch will interpret “12345” as a string:

join12345apple returns "12345apple"

So, how will it tell whether or not what the intent is? Or are you more concerned with whether or not the input is a number? If so, then I think "is [ ] a number" would be the best and most unambiguous block for your suggestion.
The join block converts numbers into strings, so it will be possible to join them.
question is, what about:

12345+str
where str is a string
for example, let str = “apple”

after some testing it appears to be “12345”

that said, we still don't know whether 12345 is to be used as a string or an integer, and i don't even know if the st cares about types at all anymore

Banana

Last edited by banana439monkey (Aug. 1, 2022 15:38:55)

Powered by DjangoBB