Discuss Scratch

savaka
Scratcher
1000+ posts

Power block

(() ^ () :: 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.

Last edited by savaka (Aug. 9, 2016 14:44:58)

drmcw
Scratcher
1000+ posts

Power block

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

Last edited by drmcw (Sept. 11, 2013 12:22:57)


10 !
ScratchVaders or Galaga?
Maybe Eliza can help you decide?
xlk
Scratcher
100+ posts

Power block

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
Scratcher
1000+ posts

Power block

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

I've reached the 1000+ post rank on wednesday, 11th Sept., 2013 18:29 (German time) / 12:29 (Eastern Standard Time)
Hi! I Iike helping others. So feel free to leave a question at my profile!

drmcw
Scratcher
1000+ posts

Power block

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?

10 !
ScratchVaders or Galaga?
Maybe Eliza can help you decide?
jvvg
Scratcher
1000+ posts

Power block

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.

Last edited by jvvg (Sept. 11, 2013 17:39:29)


jvvg
Administrator and backend engineer on the Scratch Wiki
Executive director of FutureSight Technologies
savaka
Scratcher
1000+ posts

Power block

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
Scratcher
1000+ posts

Power block

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
Scratcher
1000+ posts

Power block

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.

Last edited by mitchboy (Sept. 11, 2013 21:42:01)


Capsicum annuum.
drmcw
Scratcher
1000+ posts

Power block

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.

10 !
ScratchVaders or Galaga?
Maybe Eliza can help you decide?
DadOfMrLog
Scratcher
1000+ posts

Power block

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…

Last edited by DadOfMrLog (Sept. 12, 2013 16:27:02)



Alternate account: TheLogFather –– HowTos and useful custom blocks (see studio). Examples below…


- String manipulation - - - X to power of Y - - - Clone point to clone - Detect New Scratcher - Speed tests studio -

DadOfMrLog
Scratcher
1000+ posts

Power block

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…)

Last edited by DadOfMrLog (Sept. 12, 2013 16:47:58)



Alternate account: TheLogFather –– HowTos and useful custom blocks (see studio). Examples below…


- String manipulation - - - X to power of Y - - - Clone point to clone - Detect New Scratcher - Speed tests studio -

Lilifant7253
Scratcher
1000+ posts

Power block

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.

Last edited by Lilifant7253 (Sept. 14, 2013 16:10:19)


I've reached the 1000+ post rank on wednesday, 11th Sept., 2013 18:29 (German time) / 12:29 (Eastern Standard Time)
Hi! I Iike helping others. So feel free to leave a question at my profile!

A-no-meep
Scratcher
100+ posts

Power block

This is a good idea.

[ v]
A dropdown to nowhere
mathfreak231
Scratcher
1000+ posts

Power block

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

I remember adding that thread as a citation on Solving Exponents

Last edited by mathfreak231 (Sept. 16, 2013 21:50:54)


3DS FC: 3239-6065-2543 (Ask me before adding) | AS: Alex~ | X: Alex (DWS)
Ask me on my profile about XYORAS breeding requests!

My browser / operating system: MacOS Macintosh X 10.11.3, Safari 9.0.3, Flash 20.0 (release 0)
firedrake969_test
Scratcher
500+ posts

Power block

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.

Alt account of Firedrake969.

Rocket II: A black and white bitmap space game!

I seek not fame, but education.

;
mathfreak231
Scratcher
1000+ posts

Power block

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.

3DS FC: 3239-6065-2543 (Ask me before adding) | AS: Alex~ | X: Alex (DWS)
Ask me on my profile about XYORAS breeding requests!

My browser / operating system: MacOS Macintosh X 10.11.3, Safari 9.0.3, Flash 20.0 (release 0)
savaka
Scratcher
1000+ posts

Power block

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.

Last edited by savaka (Sept. 21, 2013 11:14:27)

scratchisthebest
Scratcher
1000+ posts

Power block

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

I am a Lava Expert
xlk
Scratcher
100+ posts

Power block

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?

Powered by DjangoBB

Standard | Mobile