Discuss Scratch
- Discussion Forums
- » Suggestions
- » "To the power of" operation
- elast27
-
New Scratcher
2 posts
"To the power of" operation
PLEASEEEE make a “to the power of” operation (not just 10^ but any power)
This will be super useful for a project i'm working on
(() pwr of ())
This will be super useful for a project i'm working on
- Paddle2See
-
Scratch Team
1000+ posts
"To the power of" operation
Try this:
That should give you x raised to the y power.
([10 ^ v] of (([log v] of (x)) * (y)))
That should give you x raised to the y power.
Last edited by Paddle2See (Dec. 6, 2014 02:50:13)
- nimrocco
-
Scratcher
100+ posts
"To the power of" operation
Try this:([10 ^ v] of (([log v] of (x)) * (y)))
That should give you x raised to the y power.
- little_kitten
-
Scratcher
500+ posts
"To the power of" operation
I think you should do this:
define (one) ^ (two)
repeat (two) ::custom
set [answer v] to ((one) * (one)) ::custom
- VoltageGames
-
Scratcher
1000+ posts
"To the power of" operation
No support…
there's a workaround, I just don't know it…
there's a workaround, I just don't know it…
Last edited by VoltageGames (Dec. 6, 2014 17:47:14)
- TheHockeyist
-
Scratcher
1000+ posts
"To the power of" operation
Hmm… why can't the ST be more direct and include a power block? It would also eliminate “e^” and “10^” from the dropdown, so you lose two things and add one, meaning Scratch is one thing simpler.
- RDoc
-
Scratcher
46 posts
"To the power of" operation
Try this:Only for positive values of X though.([10 ^ v] of (([log v] of (x)) * (y)))
That should give you x raised to the y power.
This has been asked for many times and I don't see why it should be so difficult to implement, but anyway,
I think this is a pretty general solution:
define exp (value, power)
if <<(value) < [0 ]> and << (power) mod [1]> > [0]>> then // negative value and non-integer exponent?
set [answer] to [ ] // imaginary result
else
set [answer] to ([e^ v] of ((power) * ([ln v] of ([abs v] of (value)))) // use abs to handle negative values
if <((value) < [0]) and (((power) mod [2]) = [1])> then // handle odd or even exponents of negative values
set [answer] to ([answer] * [-1])
end
Last edited by RDoc (Dec. 7, 2014 20:08:56)
- elast27
-
New Scratcher
2 posts
"To the power of" operation
Try this:Thank you so much this really helped([10 ^ v] of (([log v] of (x)) * (y)))
That should give you x raised to the y power.
)- lalala3
-
Scratcher
100+ posts
"To the power of" operation
I think you should do this:This doesn't work for fractional powers. See RDoc's solution above.define (one) ^ (two)
repeat (two) ::custom
set [answer v] to ((one) * (one)) ::custom
No support…A workaround was given on the second post, its inability to handle negative bases aside. If you'd bothered to actually read the thread, you should have seen it.
there's a workaround, I just don't know it…
I support this suggestion, despite its workarounds, because the exponent operator is a common and important one, used almost as often as addition, subtraction, multiplication, and division, and requiring users to use a workaround like RDoc's is even more egregious than Cyoce's example of
(((a) + (b)) = ((a) - ((0) - (b))))in almost every respect.
Edit: Link added.
Last edited by lalala3 (Dec. 10, 2014 00:59:56)
- Cyoce
-
Scratcher
500+ posts
"To the power of" operation
Support.
I was about to respond to
I was about to respond to
Try this:Talking about how the ability to compose a function of much more complicated functions does not invalidate said function, but([10 ^ v] of (([log v] of (x)) * (y)))
That should give you x raised to the y power.
I think you should do this:This doesn't work for fractional powers. See RDoc's solution above.define (one) ^ (two)
repeat (two) ::custom
set [answer v] to ((one) * (one)) ::customNo support…A workaround was given on the second post, its inability to handle negative bases aside. If you'd bothered to actually read the thread, you should have seen it.
there's a workaround, I just don't know it…
I support this suggestion, despite its workarounds, because the exponent operator is a common and important one, used almost as often as addition, subtraction, multiplication, and division, and requiring users to use a workaround like RDoc's is even more egregious than Cyoce's example of(((a) + (b)) = ((a) - ((0) - (b))))in almost every respect.
Last edited by Cyoce (Dec. 10, 2014 00:52:42)
- Sillywilly24
-
Scratcher
6 posts
"To the power of" operation
This works 100 percent of the time
if <(x) < [0]> thensets answer to x to the power of y
if <(([floor v] of (y)) mod (2)) = [0]> then
set [answer v] to ([10 ^ v] of (([log v] of ([abs v] of (x))) * (y)))
else
set [ answer v] to ((-1) * ([10 ^ v] of (([log v] of ([abs v] of (x))) * (y))))
end
else
set [answer v] to ([10 ^ v] of (([log v] of (x)) * (y)))
end
- Sillywilly24
-
Scratcher
6 posts
"To the power of" operation
my above solution can be simplified a lot but it works so whatever
- NilsTheBest
-
Scratcher
1000+ posts
"To the power of" operation
This works 100 percent of the timehi, thanks for helping but please don't necropost, this post was made years ago and probably has already been solved. Thanks ^^if <(x) < [0]> thensets answer to x to the power of y
if <(([floor v] of (y)) mod (2)) = [0]> then
set [answer v] to ([10 ^ v] of (([log v] of ([abs v] of (x))) * (y)))
else
set [ answer v] to ((-1) * ([10 ^ v] of (([log v] of ([abs v] of (x))) * (y))))
end
else
set [answer v] to ([10 ^ v] of (([log v] of (x)) * (y)))
end
- Inkulumo
-
Scratcher
500+ posts
"To the power of" operation
If the post is constructive (of which it is) and it is suggesting a workaround, it's allowed.This works 100 percent of the timehi, thanks for helping but please don't necropost, this post was made years ago and probably has already been solved. Thanks ^^if <(x) < [0]> thensets answer to x to the power of y
if <(([floor v] of (y)) mod (2)) = [0]> then
set [answer v] to ([10 ^ v] of (([log v] of ([abs v] of (x))) * (y)))
else
set [ answer v] to ((-1) * ([10 ^ v] of (([log v] of ([abs v] of (x))) * (y))))
end
else
set [answer v] to ([10 ^ v] of (([log v] of (x)) * (y)))
end
- space_elephant
-
Scratcher
500+ posts
"To the power of" operation
Hmm… why can't the ST be more direct and include a power block? It would also eliminate “e^” and “10^” from the dropdown, so you lose two things and add one, meaning Scratch is one thing simpler.It would not remove e because the constant e in still required for scratch projects. It could be changed to
(e::operators)
- bbrainstormer
-
Scratcher
11 posts
"To the power of" operation
This works 100 percent of the timeif <(x) < [0]> thensets answer to x to the power of y
if <(([floor v] of (y)) mod (2)) = [0]> then
set [answer v] to ([10 ^ v] of (([log v] of ([abs v] of (x))) * (y)))
else
set [ answer v] to ((-1) * ([10 ^ v] of (([log v] of ([abs v] of (x))) * (y))))
end
else
set [answer v] to ([10 ^ v] of (([log v] of (x)) * (y)))
end
If the answer is complex it returns the absolute value with the sign of the real part.
- steve3rom
-
Scratcher
31 posts
"To the power of" operation
Try this:([10 ^ v] of (([log v] of (x)) * (y)))
That should give you x raised to the y power.
When I try this, inputting 2 as x and 3 as y I get 7.999999999997. However I should get 8. If I just put a round around the code, will it work? Thanks.
- Maximouse
-
Scratcher
1000+ posts
"To the power of" operation
If you only need integers, you can use round and it will work.Try this:([10 ^ v] of (([log v] of (x)) * (y)))
That should give you x raised to the y power.
When I try this, inputting 2 as x and 3 as y I get 7.999999999997. However I should get 8. If I just put a round around the code, will it work? Thanks.
- E_Equals_EmCeCube3
-
Scratcher
1000+ posts
"To the power of" operation
It looks like this idea has been suggested before in this topic.
Feel free to continue the discussion there so we can keep individual suggestions organized and in one topic.
Feel free to continue the discussion there so we can keep individual suggestions organized and in one topic.

- Super_Scratch_Bros20
-
Scratcher
1000+ posts
"To the power of" operation
Support. I know that there's a workaround, but if you think about it, LOTS of things have workarounds. We don't technically need variables; we can use lists. We don't need the
These features will simplify the amount of work that needs to be done. Again, support.
move (10) stepsblock or the
change x by (10)block; we can just use
go to x: ((x position) + (10)) y: (y position). We don't need backdrops; we can just use sprites that look like backdrops.
These features will simplify the amount of work that needs to be done. Again, support.
- Discussion Forums
- » Suggestions
-
» "To the power of" operation
















