Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » Calculating variable roots
- Whitepatchwastaken
-
Scratcher
100+ posts
Calculating variable roots
X*X is simple enough for square roots (x^2), and I can do X*X*X for cube roots (x^3), etc. However, if I wanted to do, say, x^24, I'd have to do X*X*X*X*X*X… and I don't want to do all that. It especially gets bad if I want the root to be a variable.
I tried using repeat blocks, and they worked well for non-decimals.
code, for the curious:
I tried using repeat blocks, and they worked well for non-decimals.
code, for the curious:
set [originalX v] to (X)However, when Y ends with a decimal, the repeat does not register it. Is there any workaround to this that doesn't involve the repeat block?
repeat (y)
set [X v] to ((X) * (originalX))
end
- deck26
-
Scratcher
1000+ posts
Calculating variable roots
Your post if a bit confusing X*X is the square of X and X is the square root of X*X. So you're saying your method works for squares, cubes etc.
Using logarithms is potentially the answer to your question. log(x) + log(x) = 2 log(x) = log (x^2)
So n log (x) gives you the log of x^n - call it Y. Then 10^Y is x^n. In programming this will always be approximate as we're dealing with real numbers which will mostly not be exact.
It doesn't matter what the base is for the logarithm you use so you can use log (base 10) and 10^Y or you can use ln (natural log, base e) and e^Z where Z = n ln (x). You just need to be consistent - don't mix ln and log and expect it to work.
Using logarithms is potentially the answer to your question. log(x) + log(x) = 2 log(x) = log (x^2)
So n log (x) gives you the log of x^n - call it Y. Then 10^Y is x^n. In programming this will always be approximate as we're dealing with real numbers which will mostly not be exact.
It doesn't matter what the base is for the logarithm you use so you can use log (base 10) and 10^Y or you can use ln (natural log, base e) and e^Z where Z = n ln (x). You just need to be consistent - don't mix ln and log and expect it to work.
- ducdat0507
-
Scratcher
5 posts
Calculating variable roots
Do you mean n-th roots (√x, ∛x, etc.) or exponentiation (x^2, x^3, etc.)?
Anyways, like how deck26 said, you should be able to use the “() of ()” operator block and a bit of logarithm math, like this:
Anyways, like how deck26 said, you should be able to use the “() of ()” operator block and a bit of logarithm math, like this:
([10 ^ v] of (([log v] of (x)) * (y))) // this calculates x to the power of yTo calculate the y-th root of x, just swap the multiplication block with the division block.
- Whitepatchwastaken
-
Scratcher
100+ posts
Calculating variable roots
Thanks for the responses!

Do you mean n-th roots (√x, ∛x, etc.) or exponentiation (x^2, x^3, etc.)?Oops! I meant exponentation. My brain likes to switch what it thinks a word means with a similar word all the time

Using logarithms is potentially the answer to your question. log(x) + log(x) = 2 log(x) = log (x^2)This is a little confusing (probably because I don't know what a logarithm is.)
So n log (x) gives you the log of x^n - call it Y. Then 10^Y is x^n.
Anyways, like how deck26 said, you should be able to use the “() of ()” operator block and a bit of logarithm math, like this:This works really well! Oddly, it seems to always be sliiiightly off on multiples of 3 (i.e. 5.00000001 for the cube root of 125) but that's probably just a quirk of how computers handle numbers like deck26 mentioned.([10 ^ v] of (([log v] of (x)) * (y))) // this calculates x to the power of y
- Discussion Forums
- » Help with Scripts
-
» Calculating variable roots


