Discuss Scratch

Poemon1_REMIX
New Scratcher
100+ posts

Could we please have an exponents block? ( ) ^ ( )

Poemon1_REMIX wrote:

when GF clicked
set [x v] to [0]
set [y v] to [0]
repeat (y)
set [x v] to ((x) * (y))
end
Try that.
Didn't work, but my new project does!

Scratch cat knows exponents
savaka
Scratcher
1000+ posts

Could we please have an exponents block? ( ) ^ ( )

Poemon1_REMIX wrote:

when GF clicked
set [x v] to [0]
set [y v] to [0]
repeat (y)
set [x v] to ((x) * (y))
end
Try that.
Here it only works if y is a natural number. A block could do anything.

Last edited by savaka (Sept. 21, 2013 18:55:22)

mitchboy
Scratcher
1000+ posts

Could we please have an exponents block? ( ) ^ ( )

savaka wrote:

Poemon1_REMIX wrote:

when GF clicked
set [x v] to [0]
set [y v] to [0]
repeat (y)
set [x v] to ((x) * (y))
end
Try that.
Here it only works if y is a natural number. A block could do anything.
If (y)<(0) then
repeat (y)
set [x v] to ((x) * (y))
_
set [x v] to ((1) / (x))

Boom. It now works for negatives. And you can use (mod (1)) then the log method for non-integers.

Last edited by mitchboy (Sept. 22, 2013 19:57:26)

savaka
Scratcher
1000+ posts

Could we please have an exponents block? ( ) ^ ( )

I understand all these work, but wouldn't a block be way easier for new Scratchers? I have no idea what logarithms are.
Poemon1_REMIX
New Scratcher
100+ posts

Could we please have an exponents block? ( ) ^ ( )

savaka wrote:

I understand all these work, but wouldn't a block be way easier for new Scratchers? I have no idea what logarithms are.
A logarithm is like a reverse power. For example,
How many 5's are in 15?
5 ^ 3 = 15,
LOG(5)15 = 3.

Here's the format:
LOGS:
LOG(base)answer = power

POWERS:
base ^ power = answer.
just switch around power and answer.
savaka
Scratcher
1000+ posts

Could we please have an exponents block? ( ) ^ ( )

Poemon1_REMIX wrote:

savaka wrote:

I understand all these work, but wouldn't a block be way easier for new Scratchers? I have no idea what logarithms are.
A logarithm is like a reverse power. For example,
How many 5's are in 15?
5 ^ 3 = 15,
LOG(5)15 = 3.

Here's the format:
LOGS:
LOG(base)answer = power

POWERS:
base ^ power = answer.
just switch around power and answer.
Log seems to take 2 parameters. Scratch's advanced math block only takes 1!
DadOfMrLog
Scratcher
1000+ posts

Could we please have an exponents block? ( ) ^ ( )

savaka wrote:

Log seems to take 2 parameters. Scratch's advanced math block only takes 1!
Logarithm of a number (say a) for a particular base (say b) is usually written as logb a - where the small b is the closest I could write to a subscript using BBCode.

But there are certain bases for which the notation is normally simplified. One of these is base 10, in which case the subscript b is usually dropped (unless there's a reason to show it explicitly, to avoid confusion, or to make it really clear what is meant). So the “log” in Scratch's drop-down means base 10 logarithm.

The other is the so called "natural logarithm", which is logarithm to base e (=2.718281828459045…)
In that case it is written as “ln” (lower case L and N). This is also in the Scratch maths drop-down.

You can think of “log” as being the ‘opposite’ of “10 to the power” - so that log(10^n)=n.
Similarly, you can think of “ln” as being the ‘opposite’ of "e to the power" - hence ln(e^n)=n.

That's why the two methods for x^y that I outlined earlier using those blocks are identical:
([10^ v] of ((y) * ([log v] of (x))) // =  x^y
([e^ v] of ((y) * ([ln v] of (x))) // = x^y

Hope that make sense!

Last edited by DadOfMrLog (Sept. 23, 2013 13:10:29)

Poemon1_REMIX
New Scratcher
100+ posts

Could we please have an exponents block? ( ) ^ ( )

DadOfMrLog wrote:

savaka wrote:

Log seems to take 2 parameters. Scratch's advanced math block only takes 1!
Logarithm of a number (say a) for a particular base (say b) is usually written as logb a - where the small b is the closest I could write to a subscript using BBCode.

But there are certain bases for which the notation is normally simplified. One of these is base 10, in which case the subscript b is usually dropped (unless there's a reason to show it explicitly, to avoid confusion, or to make it really clear what is meant). So the “log” in Scratch's drop-down means base 10 logarithm.

The other is the so called "natural logarithm", which is logarithm to base e (=2.718281828459045…)
In that case it is written as “ln” (lower case L and N). This is also in the Scratch maths drop-down.

You can think of “log” as being the ‘opposite’ of “10 to the power” - so that log(10^n)=n.
Similarly, you can think of “ln” as being the ‘opposite’ of "e to the power" - hence ln(e^n)=n.

That's why the two methods for x^y that I outlined earlier using those blocks are identical:
([10^ v] of ((y) * ([log v] of (x))) // =  x^y
([e^ v] of ((y) * ([ln v] of (x))) // = x^y

Hope that make sense!
Ya, I was going to get on to that next. Also, LOG(x) is equivalent to LOG(x)^y, right?
SuperNicky
Scratcher
100+ posts

Could we please have an exponents block? ( ) ^ ( )

power what do you mean power?
Poemon1_REMIX
New Scratcher
100+ posts

Could we please have an exponents block? ( ) ^ ( )

SuperNicky wrote:

power what do you mean power?
like 5^2 = 25, or 2^3 = 8.

(^ is power)
savaka
Scratcher
1000+ posts

Could we please have an exponents block? ( ) ^ ( )

DadOfMrLog wrote:

savaka wrote:

Log seems to take 2 parameters. Scratch's advanced math block only takes 1!
Logarithm of a number (say a) for a particular base (say b) is usually written as logb a - where the small b is the closest I could write to a subscript using BBCode.

But there are certain bases for which the notation is normally simplified. One of these is base 10, in which case the subscript b is usually dropped (unless there's a reason to show it explicitly, to avoid confusion, or to make it really clear what is meant). So the “log” in Scratch's drop-down means base 10 logarithm.

The other is the so called "natural logarithm", which is logarithm to base e (=2.718281828459045…)
In that case it is written as “ln” (lower case L and N). This is also in the Scratch maths drop-down.

You can think of “log” as being the ‘opposite’ of “10 to the power” - so that log(10^n)=n.
Similarly, you can think of “ln” as being the ‘opposite’ of "e to the power" - hence ln(e^n)=n.

That's why the two methods for x^y that I outlined earlier using those blocks are identical:
([10^ v] of ((y) * ([log v] of (x))) // =  x^y
([e^ v] of ((y) * ([ln v] of (x))) // = x^y

Hope that make sense!
Oh, I get it. You use multiplication.
DadOfMrLog
Scratcher
1000+ posts

Could we please have an exponents block? ( ) ^ ( )

savaka wrote:

DadOfMrLog wrote:

That's why the two methods for x^y that I outlined earlier using those blocks are identical:
([10^ v] of ((y) * ([log v] of (x))) // =  x^y
([e^ v] of ((y) * ([ln v] of (x))) // = x^y

Hope that make sense!
Oh, I get it. You use multiplication.
Yes, if you know about how things multiply (and divide) within logs, you'll know the following identities:

log(x*y) = log(x) + log(y)
log(x/y) = log(x) - log(y)

Above is true for any base logarithm, but let's just stick with base 10 to avoid having lots of subscripts.

Now, if you take that first one and do something like log(x^4) = log(x*x*x*x), you see it's just log(x)+log(x)+log(x)+log(x), i.e. 4*log(x).

It's not hard to see the generalisation of that: log(x^y) = y*log(x).
That one is particularly important for us here, because it contains the x^y that we want to calculate.

We know that 10^ and log are ‘opposites’, which means that log(10^n) = n.
…and also: 10^(log n) = n.

If you put n = x^y into that, and use the identity above, guess what you find…
firedrake969_test
Scratcher
500+ posts

Could we please have an exponents block? ( ) ^ ( )

The thing is that you can't do negative numbers with logs.
DadOfMrLog
Scratcher
1000+ posts

Could we please have an exponents block? ( ) ^ ( )

firedrake969_test wrote:

The thing is that you can't do negative numbers with logs.
OK, so here's my definitive x^y custom block…
define result = (x) ^ (y)
if <(y) = [0]> then
set [result v] to [1] // yes, we also include 0^0=1 :O
else
if <(x) = [0]> then
set [result v] to [0] // 0^y=0 for any y except zero (see above)
else
set [result v] to ([e^ v] of ((y)*([ln v] of ([abs v] of (x)))) // no negative x for now
if <(x) < [0]> then // now deal with power of negative number
if <(round(y)) = (y)> then // we can do integer powers of negative numbers
if <((y) mod (2)) = [1]> then
set [result v] to ((0) - (result)) // odd powers will be negative
end
else
set [result v] to [NaN] // but let's not go there at this stage...
end
end
if <<(y) > [0]> and <<(round(x)) = (x)> and <(round(y)) = (y)>>> then
set [result v] to (round (result)) // ensure we get exactly an integer if both x & y were ints
end
end
end
That should cover everything but non-integer powers of negative numbers (which gets a bit hairy…), and it makes sure that integer raised to integer gives exactly an integer (in case you assume you'll get an integer, perhaps because you check for equality with an integer at some point).

Hope I've got that all right!

Last edited by DadOfMrLog (Sept. 23, 2013 23:03:00)

Poemon1_REMIX
New Scratcher
100+ posts

Could we please have an exponents block? ( ) ^ ( )

DadOfMrLog wrote:

firedrake969_test wrote:

The thing is that you can't do negative numbers with logs.
OK, so here's my definitive x^y custom block…
define result = (x) ^ (y)
if <(y) = [0]> then
set [result v] to [1] // yes, we also include 0^0=1 :O
else
if <(x) = [0]> then
set [result v] to [0] // 0^y=0 for any y except zero (see above)
else
set [result v] to ([e^ v] of ((y)*([ln v] of ([abs v] of (x)))) // no negative x for now
if <(x) < [0]> then // now deal with power of negative number
if <(round(y)) = (y)> then // we can do integer powers of negative numbers
if <((y) mod (2)) = [1]> then
set [result v] to ((0) - (result)) // odd powers will be negative
end
else
set [result v] to [NaN] // but let's not go there at this stage...
end
end
if <<(y) > [0]> and <<(round(x)) = (x)> and <(round(y)) = (y)>>> then
set [result v] to (round (result)) // ensure we get exactly an integer if both x & y were ints
end
end
end
That should cover everything but non-integer powers of negative numbers (which gets a bit hairy…), and it makes sure that integer raised to integer gives exactly an integer (in case you assume you'll get an integer, perhaps because you check for equality with an integer at some point).

Hope I've got that all right!
Ooh. Pretty big script! Let me rephrase that. really big script!
scratchisthebest
Scratcher
1000+ posts

Could we please have an exponents block? ( ) ^ ( )

Even better than that huge script: this block!

(() ^ ())
1234abcdcba4321
Scratcher
1000+ posts

Could we please have an exponents block? ( ) ^ ( )

The ^: To the power of.

I need this in my current project, and so far, the only way I can do it is by making a different variable just so my value can go up just the right amount.

Also, rename and delete functions for variables? I know delete was in 1.4, why was it deleted?
ErnieParke
Scratcher
1000+ posts

Could we please have an exponents block? ( ) ^ ( )

The delete button wasn't deleted in Scratch 2.0; it's still there. Right click a variable in the block palette, and you can delete it and rename it.

About the ^ power, I'd just like to point out a workaround:

([10 ^ v] of ((power) * ([log v] of (base))))

Showing,

ErnieParke
AonymousGuy
Scratcher
1000+ posts

Could we please have an exponents block? ( ) ^ ( )

I support, because sometimes I have to use it.

Or, if we could get custom reporter blocks, we could just make one of those.
ProdigyZeta7
Scratcher
1000+ posts

Could we please have an exponents block? ( ) ^ ( )

ErnieParke wrote:

([10 ^ v] of ((power) * ([log v] of (base))))
And to prove this works:

10 ^ (b log a) = x >Setting up the equation
log 10 ^ (b log a) = log x >Property of logarithms
(b log a) log 10 = log x >Bringing the exponent down
b log a = log x >log 10 = 1
log (a ^ b) = log x >Bringing the exponent back to the base
a ^ b = x >Getting rid of log

Powered by DjangoBB