Discuss Scratch

BEncode314
Scratcher
21 posts

Cube Roots

I'm doing a project where I need to set a variable to the cube root of a different variable. It would also be very helpful if it could expand to rt 4, rt 5, and so on. thanks!
awesome-llama
Scratcher
1000+ posts

Cube Roots

You can combine the e^ and ln math operators to achieve exponentiation (or to find the nth root).

Just some examples, feel free to pick or modify whatever suits your needs…

([e ^ v] of ((2) * ([ln v] of (3)))) // 3^2 = 9

([e ^ v] of ((0.5) * ([ln v] of (9)))) // 9^0.5 = sqrt(9) = 3

([e ^ v] of (([ln v] of (9)) / (2))) // 9^(1/2) = sqrt(9) = 3

BEncode314
Scratcher
21 posts

Cube Roots

In example #2, does the 0.5 represent 1/2 or 2nd root? Can I use 0.333333 to represent 1/3 or 3rd root? Thanks!
MathPuppy314
Scratcher
500+ posts

Cube Roots

Yes; a cube root is the same as raising to an exponent of 1/3.
This is the same with any other root.
BEncode314
Scratcher
21 posts

Cube Roots

I tried it and It worked! thanks so much!
qwerty_wasd_gone
Scratcher
1000+ posts

Cube Roots

awesome-llama wrote:

You can combine the e^ and ln math operators to achieve exponentiation (or to find the nth root).

Just some examples, feel free to pick or modify whatever suits your needs…

([e ^ v] of ((2) * ([ln v] of (3)))) // 3^2 = 9

([e ^ v] of ((0.5) * ([ln v] of (9)))) // 9^0.5 = sqrt(9) = 3

([e ^ v] of (([ln v] of (9)) / (2))) // 9^(1/2) = sqrt(9) = 3

It doesn't have to be that, it can also be base 10 logarithms.
([10^ v] of (([log v] of (...)) / (...))::operators)
awesome-llama
Scratcher
1000+ posts

Cube Roots

qwerty_wasd_gone wrote:

It doesn't have to be that, it can also be base 10 logarithms.
([10^ v] of (([log v] of (...)) / (...))::operators)
Correct, but I don't recommend using them because they are slower.

https://github.com/scratchfoundation/scratch-vm/blob/65f22c15f3060b1faea8871176f7849bb3808883/src/blocks/scratch3_operators.js#L145-L148
case 'ln': return Math.log(n);
case 'log': return Math.log(n) / Math.LN10;
case 'e ^': return Math.exp(n);
case '10 ^': return Math.pow(10, n);

Powered by DjangoBB