savaka

(() ^ () :: operators)

This is necessary because there is no real workaround (AFAIK) that will work for all power calculations in Scratch.
Oh wait there is
(credit to DadOfMrLog) but this workaround is huge and no reason to not just have the block

It also isn't too complicated for young people who may not have learned this math yet because this block already has more complicated math operations:
([sqrt v] of (9))
The block could be positioned right above that block so both blocks with somewhat complex math are together.

drmcw

You can use logarithms to do that.
([10^ v] of ((y) * ([log v] of (x))) // =  x^y

xlk

yes, but same can be said of the change x/y by and go to blocks… They aren't necessary, but for new scratchers are a great help.

Lilifant7253

You can do it like this too:
when gf clicked
set [variable1 v] to (1)
repeat (y)
set [variable1 v] to ((variable1) * (x))

drmcw

xlk wrote:

yes, but same can be said of the change x/y by and go to blocks… They aren't necessary, but for new scratchers are a great help.
Yes, but how many projects need a x^y block?

jvvg

Lilifant7253 wrote:

You can do it like this too:
when gf clicked
set [variable1 v] to (1)
repeat (y)
set [variable1 v] to ((variable1) * (x))
That doesn't work for negative or non-integer powers, though.

savaka

drmcw wrote:

You can use logarithms to do that.
([10^ v] of ((y) * ([log v] of (x))) // =  x^y
Does this work for all negatives and decimals?

savaka

drmcw wrote:

xlk wrote:

yes, but same can be said of the change x/y by and go to blocks… They aren't necessary, but for new scratchers are a great help.
Yes, but how many projects need a x^y block?
I was thinking of making a calculator project.

mitchboy

jvvg wrote:

Lilifant7253 wrote:

You can do it like this too:
when gf clicked
set [variable1 v] to (1)
repeat (y)
set [variable1 v] to ((variable1) * (x))
That doesn't work for negative or non-integer powers, though.
It actually does. Check out my project to test non-integer numbers. I don't think it works with negatives.

drmcw

savaka wrote:

drmcw wrote:

You can use logarithms to do that.
([10^ v] of ((y) * ([log v] of (x))) // =  x^y
Does this work for all negatives and decimals?
You can't make x negative.

DadOfMrLog

mitchboy wrote:

jvvg wrote:

Lilifant7253 wrote:

You can do it like this too:
when gf clicked
set [variable1 v] to (1)
repeat (y)
set [variable1 v] to ((variable1) * (x))
That doesn't work for negative or non-integer powers, though.
It actually does. Check out my project to test non-integer numbers. I don't think it works with negatives.

It works fine for non-integer x, but as jvvg wrote, it can't work with non-integer *powers* (i.e. where y is not a whole number).
It will give an answer, because it will repeat a certain whole number of times [round(y) rather than floor(y), interestingly], but it will be wrong…

@savaka:
Note that using:

([10^ v] of ((y) * ([log v] of (x))) // =  x^y
or
([e^ v] of ((y) * ([ln v] of (x))) // =  x^y
may give you a non-integer result even though both x and y are integers - because the log(x) [or ln(x)] won't be an integer, so there can be rounding errors when it then does 10 to the power [or e to the power] of that.
If you want to ensure you get exactly an integer (maybe for testing if it is less/greater/exactly some integer value) then you should round it.


Personally, I think it'd be nice to see this added somewhere - though it would unfortunately not be able to go into the current drop-down with the other less well-known maths operators (sqrt,abs,etc.) because it has two ‘slots’ rather than just the one. It would need to be on its own like ‘mod’. Or else that block could also become a drop-down, containing “mod” and ^. (But I haven't checked how that affects certain things in the way script is encoded…)

Unfortunately, those are the only really useful two-argument operators that I can think of (apart from the obvious ones like +, -, *, /, already elsewhere) - others start to become really rather obscure, and not so useful…

DadOfMrLog

drmcw wrote:

savaka wrote:

drmcw wrote:

You can use logarithms to do that.
([10^ v] of ((y) * ([log v] of (x))) // =  x^y
Does this work for all negatives and decimals?
You can't make x negative.

You need to check for zeros, though, because x^0=1 and 0^y = 0 unless x=y=0, in which case you should have 0^0=1 (surprisingly enough…)
So you can do the check for x being zero first, and make the result equal to one straight away if it is, otherwise then check if y=0, and if so set the result to zero straight away.

But dealing with negative x is possible under certain circumstances - if y is an integer then it's just a case of checking if y is even or odd, and using abs(x)^y - you negate the result if y is odd.

Fractional powers of negative numbers are rather more tricky to do if you're only able to deal with real numbers (I'd assume you don't want to get into the realm of complex numbers…)

Lilifant7253

jvvg wrote:

Lilifant7253 wrote:

You can do it like this too:
when gf clicked
set [variable1 v] to (1)
repeat (y)
set [variable1 v] to ((variable1) * (x))
That doesn't work for negative or non-integer powers, though.
Yes, you're right you've to do it like this. (but y has to be integer):
when gf clicked
set [variable1 v] to (1)
repeat ([amount v] of (y))
set [variable1 v] to ((variable1) * (x))
if <(y) < (0)>
set [variable1 v] to ((1) / (variable1))
Of course there shouldn't be the leak between the repeat and the if block.

A-no-meep

This is a good idea.

mathfreak231

Sorry, but http://scratch.mit.edu/discuss/topic/9907/ .

I remember adding that thread as a citation on Solving Exponents

firedrake969_test

Would adding this make it easier for new Scratchers? Yup.

And why would you add it as a citation? It doesn't accept OR deny the block.

mathfreak231

firedrake969_test wrote:

Would adding this make it easier for new Scratchers? Yup.

And why would you add it as a citation? It doesn't accept OR deny the block.
It shows that at least 1 scratcher supports it.
And I meant it as something that has already been suggested.

savaka

mathfreak231 wrote:

Sorry, but http://scratch.mit.edu/discuss/topic/9907/ .

I remember adding that thread as a citation on Solving Exponents

firedrake969_test wrote:

Would adding this make it easier for new Scratchers? Yup.

And why would you add it as a citation? It doesn't accept OR deny the block.

mathfreak231 wrote:

firedrake969_test wrote:

Would adding this make it easier for new Scratchers? Yup.

And why would you add it as a citation? It doesn't accept OR deny the block.
It shows that at least 1 scratcher supports it.
And I meant it as something that has already been suggested.
I don't understand what you're talking about.

scratchisthebest

by this logic, we can remove “move (10) steps”

why not just use
go to x: ((x position) + ([cos v] of ((direction)*(amt)))) y ((x position) + ([sin v] of ((direction)*(amt))))
i mean jeez so simple

xlk

and while we are at it, the “wait” block is also unnecessary, as you can use the timer for it.
BTW, I remember reading somewhere that scratch's blocks are faster than a series of blocks doing the same, so (()^()) would be technically speaking faster than doing logarithms. I mean, is it that hard to add scratchteam?