Discuss Scratch

TheLogFather
Scratcher
1000+ posts

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

savaka wrote:

THERE IS NO REAL WORKAROUND
Huh?

…though I still think it'd be quite nice to have the block, despite not being entirely clear where it'd go, since it feels like it should be part of the multichoice maths operators block (the one with the dropdown menu), but cannot go there 'cos it takes two args rather than one…

Last edited by TheLogFather (Aug. 9, 2016 12:54:31)


Siggy the Kumquat slayer:
Main account: DadOfMrLog –– Frameworks for basic pen-rendered 3D in scratch (see studio). Examples:

- - - - 3D Text - - - - - - Simple shapes - - - Controllable structures - - - On the ground - - - - - - In space - - - -

savaka
Scratcher
1000+ posts

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

TheLogFather wrote:

savaka wrote:

THERE IS NO REAL WORKAROUND
Huh?

…though I still think it'd be quite nice to have the block, despite not being entirely clear where it'd go, since it feels like it should be part of the multichoice maths operators block (the one with the dropdown menu), but cannot go there 'cos it takes two args rather than one…

o wow u did it
Gaza101
Scratcher
500+ posts

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

Pretty sure this has been suggested before. It's freaking useful and I'm clueless as to why we don't yet have a block like this. Support.

Gaza101: retired member of the Scratch 2.0 Transition Team


kids112
Scratcher
25 posts

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

kayybee wrote:

ProdigyZeta7 wrote:

Calm down, please. What you ask is already suggested before several times. And don't worry, I'm sure at some point they'll add the exponent block.

The workarounds goes like this:
//short version
x ^ y = 10 ^ (y * log x)

//long version
if y = 0
set x = 1
else
tempx = x
repeat (y - 1)
x = x * tempx
When I tried the short way in Snap!, it did not always say integers. For example, it said 5^2=24.999999999999999999999999999999999999, VERY close to 25.

your “long version” workaround isn't really the same as te short version.

The short version works for all real numbers, the long one doesn't.

Autistc, Aromantic, and Proud
Aspiring filmmaker, object show creator, and conworlder
dvargasews
Scratcher
500+ 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!
You forgot that fractional powers of negative roots are okay unless the denominator is even. Try this:
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
if <((round ((1) / ((1) GCF (y)))) mod (0)) = [0]> then // I won't bother doing a greatest common factor block, but you probably know how to make one.
set [result v] to [NaN] // but let's not go there at this stage...
else //y's denominator is odd.
set [Result v] to ((0) - ([e ^ v] of ((y)*([ln v] of ([abs v] of (x)))))
end
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
By the way, you use 0-x instead of -1*x too? What a coincidence!

Last edited by dvargasews (Jan. 17, 2017 21:21:42)


Please read my signature! It might explain some lingo that I used in my post or comment. However, not all of it shows, so you have to highlight the entire bottom of the post, from “Please read my signature!” to where it says report, and copy and paste in into a new sheet or tab or whatever.
I can't decide my signature between:
1. I'm a grammar stickler, and my pet peeve is when people use good as an adverb.
2. I have lots of [creativity]original ideas[/creativity]. [creativity]This[/creativity] is one of those ideas.
3. If I used this symbol: ⸮ in my comment, please look it up before doing anything else.
4. {Minot, North Dakota/Donald Trump parodies} “WE'RE GONNA BUILD A DAM AND MAKE CANADA PAY FOR IT!” “When Canada sends its water, they’re not sending their best drinking water…They’re bringing sleet. They’re bringing floods. The water's toxic.” (It's just a joke that I came up with.)
5. One should make his/her signature text small; that way (s)he can cram more text into the signature.
6. New [scratchblocks] idea each week! (Thanks to Bright-Idea (Read her signature.) for inspiration/the general idea.) Here's the archive:
Note: I stopped this because of signature character limits.
Week of 1-23-2017: Hat-caps tinyurl.com/htauyu2
Week of 1-16-2017: Country-colored scratchblocks tinyurl.com/z8lz6c4
Week of 1-9-2017: Drop-down booleans tinyurl.com/j5d3k6l
Week of 1-2-2017: Block->string inputs and more truth values tinyurl.com/jqu7avs

DadOfMrLog
Scratcher
1000+ posts

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

dvargasews wrote:

I won't bother doing a greatest common factor block, but you probably know how to make one.
Uhhhrrrmmm… I'd like to see you try to reliably do what you really need to do here (which is to find a fractional representation of y) with all the foibles of decimal numbers represented internally with binary floating-point…





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 -

dvargasews
Scratcher
500+ posts

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

DadOfMrLog wrote:

dvargasews wrote:

I won't bother doing a greatest common factor block, but you probably know how to make one.
Uhhhrrrmmm… I'd like to see you try to reliably do what you really need to do here (which is to find a fractional representation of y) with all the foibles of decimal numbers represented internally with binary floating-point…



Sure, the numerator of y is the LCM of y and 1, and the denominator of y can be calculated by dividing the numerator by y, but it is also the reciprocal of the GCF of 1 and y because for all x and all y, LCM(x, y)*GCF(x, y)=x*y. This returns the numerator and denominator of y in standard form.

Last edited by dvargasews (Dec. 28, 2016 20:59:20)


Please read my signature! It might explain some lingo that I used in my post or comment. However, not all of it shows, so you have to highlight the entire bottom of the post, from “Please read my signature!” to where it says report, and copy and paste in into a new sheet or tab or whatever.
I can't decide my signature between:
1. I'm a grammar stickler, and my pet peeve is when people use good as an adverb.
2. I have lots of [creativity]original ideas[/creativity]. [creativity]This[/creativity] is one of those ideas.
3. If I used this symbol: ⸮ in my comment, please look it up before doing anything else.
4. {Minot, North Dakota/Donald Trump parodies} “WE'RE GONNA BUILD A DAM AND MAKE CANADA PAY FOR IT!” “When Canada sends its water, they’re not sending their best drinking water…They’re bringing sleet. They’re bringing floods. The water's toxic.” (It's just a joke that I came up with.)
5. One should make his/her signature text small; that way (s)he can cram more text into the signature.
6. New [scratchblocks] idea each week! (Thanks to Bright-Idea (Read her signature.) for inspiration/the general idea.) Here's the archive:
Note: I stopped this because of signature character limits.
Week of 1-23-2017: Hat-caps tinyurl.com/htauyu2
Week of 1-16-2017: Country-colored scratchblocks tinyurl.com/z8lz6c4
Week of 1-9-2017: Drop-down booleans tinyurl.com/j5d3k6l
Week of 1-2-2017: Block->string inputs and more truth values tinyurl.com/jqu7avs

JonathanSchaffer
Scratcher
1000+ posts

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

drmcw wrote:

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

([10^ v] of ((y) * ([log v] of (x)))) // =  x^y

club penguin is kil
-stache-
Scratcher
500+ posts

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

JonathanSchaffer wrote:

drmcw wrote:

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

([10^ v] of ((y) * ([log v] of (x)))) // =  x^y
Not always true…


3x3 pb: 13.240
3x3 avg: ~21-26
DadOfMrLog
Scratcher
1000+ posts

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

dvargasews wrote:

Sure, the numerator of y is the LCM of y and 1, and the denominator of y can be calculated by dividing the numerator by y, but it is also the reciprocal of the GCF of 1 and y because for all x and all y, LCM(x, y)*GCF(x, y)=x*y. This returns the numerator and denominator of y in standard form.
I'm a bit confused by what you're trying to do here. Are your x & y integers?

If so, how can LCM(1,y) be anything but y, and LCM(1,y) anything but 1?

If not… well… how are you defining LCM & GCF for non-integers?


But seriously, how are you going to turn an arbitrary floating-point value into a fraction (i.e. a/b where a & b are integers), preferably in its lowest form?

And once you do, and you still find that one of a or b is large (which is very likely), how are you going to raise some other arbitrary number to that power without it overflowing to Infinity, or underflowing to zero, due to double-precision floating-point's min/max bounds? (Guess you'll have to write your own code to do arbitrary-precision multiplication…)

Last edited by DadOfMrLog (Dec. 30, 2016 09:58:53)



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 -

dvargasews
Scratcher
500+ posts

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

DadOfMrLog wrote:

dvargasews wrote:

Sure, the numerator of y is the LCM of y and 1, and the denominator of y can be calculated by dividing the numerator by y, but it is also the reciprocal of the GCF of 1 and y because for all x and all y, LCM(x, y)*GCF(x, y)=x*y. This returns the numerator and denominator of y in standard form.
I'm a bit confused by what you're trying to do here. Are your x & y integers?

If so, how can LCM(1,y) be anything but y, and LCM(1,y) anything but 1?

If not… well… how are you defining LCM & GCF for non-integers?


But seriously, how are you going to turn an arbitrary floating-point value into a fraction (i.e. a/b where a & b are integers), preferably in its lowest form?

And once you do, and you still find that one of a or b is large (which is very likely), how are you going to raise some other arbitrary number to that power without it overflowing to Infinity, or underflowing to zero, due to double-precision floating-point's min/max bounds? (Guess you'll have to write your own code to do arbitrary-precision multiplication…)

LCM and GCF can also work for non-integers because for any x and y, if c is a constant, then LCM(x/c, y/c)=LCM(x, y)/c, and GCF(x/c, y/c)=GCF(x, y)/c.
The numerator of y is LCM(1, y), and the denominator of y is 1/(GCF(1, y)).
That won't be a problem because using the sliders, one can only set x and y to a multiple of 0.01.
Or to find the GCF of a and b for any numbers a and b, you could just do it the old-fashioned way (in terms of finding remainders instead of plugging it in to a calculator, which would be to make a block like this:
if <(a) = (b)> then 
set [GCF v] to (a)
else
if <(a) > (b)> then
find the LCM of (a) and (b) :: custom
else
find the LCM and GCF of (b) and (a) :: custom
end
end
define find the LCM and GCF of (greater number) and (lesser number)
set [a v] to (greater number)//I like to reuse variables if I can, but if you need to save a or b, you may use other variables.
set [b v] to (lesser number)
repeat until <<(a) = [0]> or <(b) = [0]>>
set [a v] to ((a) mod (b))
if <(a) = [0]> then
set [GCF v] to (b)
else
set [b v] to ((b) mod (a))
if <(b) = [0]> then
set [GCF v] to (a)
end
end
end
set [LCM v] to (((greater number)*(lesser number))/(LCM))

Last edited by dvargasews (Jan. 2, 2017 00:29:19)


Please read my signature! It might explain some lingo that I used in my post or comment. However, not all of it shows, so you have to highlight the entire bottom of the post, from “Please read my signature!” to where it says report, and copy and paste in into a new sheet or tab or whatever.
I can't decide my signature between:
1. I'm a grammar stickler, and my pet peeve is when people use good as an adverb.
2. I have lots of [creativity]original ideas[/creativity]. [creativity]This[/creativity] is one of those ideas.
3. If I used this symbol: ⸮ in my comment, please look it up before doing anything else.
4. {Minot, North Dakota/Donald Trump parodies} “WE'RE GONNA BUILD A DAM AND MAKE CANADA PAY FOR IT!” “When Canada sends its water, they’re not sending their best drinking water…They’re bringing sleet. They’re bringing floods. The water's toxic.” (It's just a joke that I came up with.)
5. One should make his/her signature text small; that way (s)he can cram more text into the signature.
6. New [scratchblocks] idea each week! (Thanks to Bright-Idea (Read her signature.) for inspiration/the general idea.) Here's the archive:
Note: I stopped this because of signature character limits.
Week of 1-23-2017: Hat-caps tinyurl.com/htauyu2
Week of 1-16-2017: Country-colored scratchblocks tinyurl.com/z8lz6c4
Week of 1-9-2017: Drop-down booleans tinyurl.com/j5d3k6l
Week of 1-2-2017: Block->string inputs and more truth values tinyurl.com/jqu7avs

IgnaxioScratcher
Scratcher
68 posts

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

savaka wrote:

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.

I quote this, like scratch team says, if a block has a workaround, it dont need reasons for not exist, it can be still used for make scratch easier, but if the workaround is too easy, it will be rejected

Last edited by IgnaxioScratcher (Dec. 31, 2016 00:14:30)


#BRINGITBACK RIP disscus button,

I love collabs! If you have one, please tell!

Please visit my collab Bus Musican Simulator!

Im also part of:
DadOfMrLog
Scratcher
1000+ posts

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

dvargasews wrote:

LCM and GCF can also work for non-integers because for any x and y, if c is a constant, then LCM(x/c, y/c)=LCM(x, y)/c, and GCF(x/c, y/c)=GCF(x, y)/c.
The numerator of y is LCM(1, y), and the denominator of y is 1/(GCF(1, y)).
Sorry, been busy with stuff over New Year, so not got around to replying to this – particularly since there's so much I could say in reply…


But I think I'll just ask if you've actually tried out that script you gave above with non-integer values? (BTW, it primarily calculates GCF, not LCM, so you could swap those variables to make that clear.)

It'll find the GCF for two integers, but I think if you try it with non-integers, and you investigate what's going on, then that will help explain why your argument above doesn't help solve the essential problem here – which is to find a rational representation for any given value, and so use that to perform the exponentiation by raising to the numerator, and taking the root for the denominator.


That won't be a problem because using the sliders, one can only set x and y to a multiple of 0.01.
OK, if you're going to restrict the inputs to only values of the form int/100, then that does make things rather easy…

However, two problems here…

Firstly, I don't see the point of a custom block for x^y that's restricted in such a way – the whole idea of it is to try to make a general power block.
Secondly, it's not necessarily true that a slider is restricted like that – check out the ones in my power project, for example.


I had loads of other things to say about floating-point format in computers, and how they bring about some really interesting twists to all this…

But I've said enough already, so I'll just end by noting…

Happy New Year!

Last edited by DadOfMrLog (Jan. 1, 2017 12:47:03)



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 -

dvargasews
Scratcher
500+ posts

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

DadOfMrLog wrote:

dvargasews wrote:

LCM and GCF can also work for non-integers because for any x and y, if c is a constant, then LCM(x/c, y/c)=LCM(x, y)/c, and GCF(x/c, y/c)=GCF(x, y)/c.
The numerator of y is LCM(1, y), and the denominator of y is 1/(GCF(1, y)).
Sorry, been busy with stuff over New Year, so not got around to replying to this – particularly since there's so much I could say in reply…


But I think I'll just ask if you've actually tried out that script you gave above with non-integer values? (BTW, it primarily calculates GCF, not LCM, so you could swap those variables to make that clear.)

It'll find the GCF for two integers, but I think if you try it with non-integers, and you investigate what's going on, then that will help explain why your argument above doesn't help solve the essential problem here – which is to find a rational representation for any given value, and so use that to perform the exponentiation by raising to the numerator, and taking the root for the denominator.
I didn't test it for non-integers; I'm a theoretical mathematician, not a practical mathematician.
Also, I'm sure that my longer version would work for anything.

dvargasews wrote:

DadOfMrLog wrote:

dvargasews wrote:

Sure, the numerator of y is the LCM of y and 1, and the denominator of y can be calculated by dividing the numerator by y, but it is also the reciprocal of the GCF of 1 and y because for all x and all y, LCM(x, y)*GCF(x, y)=x*y. This returns the numerator and denominator of y in standard form.
I'm a bit confused by what you're trying to do here. Are your x & y integers?

If so, how can LCM(1,y) be anything but y, and LCM(1,y) anything but 1?

If not… well… how are you defining LCM & GCF for non-integers?


But seriously, how are you going to turn an arbitrary floating-point value into a fraction (i.e. a/b where a & b are integers), preferably in its lowest form?

And once you do, and you still find that one of a or b is large (which is very likely), how are you going to raise some other arbitrary number to that power without it overflowing to Infinity, or underflowing to zero, due to double-precision floating-point's min/max bounds? (Guess you'll have to write your own code to do arbitrary-precision multiplication…)

LCM and GCF can also work for non-integers because for any x and y, if c is a constant, then LCM(x/c, y/c)=LCM(x, y)/c, and GCF(x/c, y/c)=GCF(x, y)/c.
The numerator of y is LCM(1, y), and the denominator of y is 1/(GCF(1, y)).
That won't be a problem because using the sliders, one can only set x and y to a multiple of 0.01.
Or to find the GCF of a and b for any numbers a and b, you could just do it the old-fashioned way (in terms of finding remainders instead of plugging it in to a calculator, which would be to make a block like this:
if <(a) = (b)> then 
set [GCF v] to (a)
else
if <(a) > (b)> then
find the LCM of (a) and (b) :: custom
else
find the LCM and GCF of (b) and (a) :: custom
end
end
define find the LCM and GCF of (greater number) and (lesser number)
set [a v] to (greater number)//I like to reuse variables if I can, but if you need to save a or b, you may use other variables.
set [b v] to (lesser number)
repeat until <<(a) = [0]> or <(b) = [0]>>
set [a v] to ((a) mod (b))
if <(a) = [0]> then
set [GCF v] to (b)
else
set [b v] to ((b) mod (a))
if <(b) = [0]> then
set [GCF v] to (a)
end
end
end
set [LCM v] to (((greater number)*(lesser number))/(LCM))



DadOfMrLog wrote:

That won't be a problem because using the sliders, one can only set x and y to a multiple of 0.01.
OK, if you're going to restrict the inputs to only values of the form int/100, then that does make things rather easy…

However, two problems here…

Firstly, I don't see the point of a custom block for x^y that's restricted in such a way – the whole idea of it is to try to make a general power block.
Secondly, it's not necessarily true that a slider is restricted like that – check out the ones in my power project, for example.


I had loads of other things to say about floating-point format in computers, and how they bring about some really interesting twists to all this…

But I've said enough already, so I'll just end by noting…

If you think about it, yours is restricted because it only works with real numbers, unlike mine. (Yes, I know that it's still buggy.)
Also, I'm pretty sure that your sliders are restricted.

DadOfMrLog wrote:

Happy New Year!

http://tinyurl.com/ze7sm2c

Last edited by dvargasews (Jan. 2, 2017 00:28:59)


Please read my signature! It might explain some lingo that I used in my post or comment. However, not all of it shows, so you have to highlight the entire bottom of the post, from “Please read my signature!” to where it says report, and copy and paste in into a new sheet or tab or whatever.
I can't decide my signature between:
1. I'm a grammar stickler, and my pet peeve is when people use good as an adverb.
2. I have lots of [creativity]original ideas[/creativity]. [creativity]This[/creativity] is one of those ideas.
3. If I used this symbol: ⸮ in my comment, please look it up before doing anything else.
4. {Minot, North Dakota/Donald Trump parodies} “WE'RE GONNA BUILD A DAM AND MAKE CANADA PAY FOR IT!” “When Canada sends its water, they’re not sending their best drinking water…They’re bringing sleet. They’re bringing floods. The water's toxic.” (It's just a joke that I came up with.)
5. One should make his/her signature text small; that way (s)he can cram more text into the signature.
6. New [scratchblocks] idea each week! (Thanks to Bright-Idea (Read her signature.) for inspiration/the general idea.) Here's the archive:
Note: I stopped this because of signature character limits.
Week of 1-23-2017: Hat-caps tinyurl.com/htauyu2
Week of 1-16-2017: Country-colored scratchblocks tinyurl.com/z8lz6c4
Week of 1-9-2017: Drop-down booleans tinyurl.com/j5d3k6l
Week of 1-2-2017: Block->string inputs and more truth values tinyurl.com/jqu7avs

DadOfMrLog
Scratcher
1000+ posts

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

dvargasews wrote:

I didn't test it for non-integers; I'm a theoretical mathematician, not a practical mathematician.
Also, I'm sure that my longer version would work for anything.
You should test it. (TBH, though, it doesn't take that much thought to realise that it can't work – if you enter two numbers that are <=1, such as 1 and 0.01, then the “mod” operator can't give anything larger than what you put in – i.e. you certainly can't get an integer >1, which is what you want…)

If you think about it, yours is restricted because it only works with real numbers, unlike mine.

Yup, that's the best way to make it general – create a fully complex-compatible version.

If the user is expecting to work only with real numbers, though, then you'll have to look at the imaginary part and decide if it's ‘close enough’ to zero to return an ‘allowed’ (i.e. real) answer rather than “NaN”…

Last edited by DadOfMrLog (Jan. 2, 2017 00:11:29)



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 -

dvargasews
Scratcher
500+ posts

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

DadOfMrLog wrote:

dvargasews wrote:

I didn't test it for non-integers; I'm a theoretical mathematician, not a practical mathematician.
Also, I'm sure that my longer version would work for anything.
You should test it. (TBH, though, it doesn't take that much thought to realise that it can't work – if you enter two numbers that are <=1, such as 1 and 0.01, then the “mod” operator can't give anything larger than what you put in – i.e. you certainly can't get an integer >1, which is what you want…)
No because it returns the reciprocal of the denominator, which is less than one if the denominator is greater than one.

DadOfMrLog wrote:

If you think about it, yours is restricted because it only works with real numbers, unlike mine.

Yup, that's the best way to make it general – create a fully complex-compatible version.

If the user is expecting to work only with real numbers, though, then you'll have to look at the imaginary part and decide if it's ‘close enough’ to zero to return an ‘allowed’ (i.e. real) answer rather than “NaN”…

Users can just enter 0 for the imaginary parts.

Please read my signature! It might explain some lingo that I used in my post or comment. However, not all of it shows, so you have to highlight the entire bottom of the post, from “Please read my signature!” to where it says report, and copy and paste in into a new sheet or tab or whatever.
I can't decide my signature between:
1. I'm a grammar stickler, and my pet peeve is when people use good as an adverb.
2. I have lots of [creativity]original ideas[/creativity]. [creativity]This[/creativity] is one of those ideas.
3. If I used this symbol: ⸮ in my comment, please look it up before doing anything else.
4. {Minot, North Dakota/Donald Trump parodies} “WE'RE GONNA BUILD A DAM AND MAKE CANADA PAY FOR IT!” “When Canada sends its water, they’re not sending their best drinking water…They’re bringing sleet. They’re bringing floods. The water's toxic.” (It's just a joke that I came up with.)
5. One should make his/her signature text small; that way (s)he can cram more text into the signature.
6. New [scratchblocks] idea each week! (Thanks to Bright-Idea (Read her signature.) for inspiration/the general idea.) Here's the archive:
Note: I stopped this because of signature character limits.
Week of 1-23-2017: Hat-caps tinyurl.com/htauyu2
Week of 1-16-2017: Country-colored scratchblocks tinyurl.com/z8lz6c4
Week of 1-9-2017: Drop-down booleans tinyurl.com/j5d3k6l
Week of 1-2-2017: Block->string inputs and more truth values tinyurl.com/jqu7avs

DadOfMrLog
Scratcher
1000+ posts

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

dvargasews wrote:

No because it returns the reciprocal of the denominator, which is less than one if the denominator is greater than one.
Ah! I see what you're intending to do there now.

Yes, that has the right theory (assuming there is a rational representation). In practice I expect you'll start running into rounding error issues once the values of numerator and denominator become larger than a few hundred or so.

Besides, once the numerator or denominator gets too large, it'll likely overflow or underflow when you try to perform the power calculation by raising to the numerator power, or underflow by trying to take the denominator root.


Anyway, here's my attempt to find a fairly simple rational representation, based on your earlier script:
define Find fraction for (x)
if <((x) mod (1)) = [0]> then // check if it's already an integer
set [Denominator v] to [1]
set [Numerator v] to (x)
stop [this script v]
end
if <(x)<[0]> then // deal with negative case...
Find fraction for (()-(x)) // ...by finding positive fraction instead...
set [Numerator v] to (() - (Numerator)) // ...and negating top of it
stop [this script v]
end
set [tol v] to ((x)*(1e-10)) // tolerance for checking if fraction is (near enough) correct
set [a v] to [1]
set [b v] to ((x) mod (1)) // start with fractional part of x
forever // hopefully will get stopped by tests below...
set [a v] to ( (a) mod (b) )
set [Denominator v] to (round ((1)/(b)))
set [Numerator v] to (round ((x)/(b)))
if <([abs v] of ( (x)-((Numerator)/(Denominator)) )) < (tol)> then
stop [this script v] // the test above seemed to be the most effective way to check we have what we want
end
set [b v] to ( (b) mod (a) )
set [Denominator v] to (round ((1)/(a)))
set [Numerator v] to (round ((x)/(a)))
if <([abs v] of ( (x)-((Numerator)/(Denominator)) )) < (tol)> then
stop [this script v]
end
end

Above works quite nicely for many reasonably simple fractions. But rounding errors cause problems once the rational representation starts to have larger values on top and/or bottom.


Users can just enter 0 for the imaginary parts.
No, I mean for the result, not the operands. I'm thinking of a custom block that only deals with reals here – and it only gives a non-NaN result if the answer is also real(ish).

(In practice, many Scratchers would not have much idea about complex numbers, and would simply want a custom block that has two slots, for two variables/expressions, and so those would be real-valued and they would expect it to give back a real value, if possible.)

Last edited by DadOfMrLog (Jan. 2, 2017 10:46:50)



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 -

dvargasews
Scratcher
500+ posts

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

DadOfMrLog wrote:

dvargasews wrote:

No because it returns the reciprocal of the denominator, which is less than one if the denominator is greater than one.
Ah! I see what you're intending to do there now.

Yes, that has the right theory (assuming there is a rational representation). In practice I expect you'll start running into rounding error issues once the values of numerator and denominator become larger than a few hundred or so.

Besides, once the numerator or denominator gets too large, it'll likely overflow or underflow when you try to perform the power calculation by raising to the numerator power, or underflow by trying to take the denominator root.


Anyway, here's my attempt to find a fairly simple rational representation, based on your earlier script:
define Find fraction for (x)
if <((x) mod (1)) = [0]> then // check if it's already an integer
set [Denominator v] to [1]
set [Numerator v] to (x)
stop [this script v]
end
if <(x)<[0]> then // deal with negative case...
Find fraction for (()-(x)) // ...by finding positive fraction instead...
set [Numerator v] to (() - (Numerator)) // ...and negating top of it
stop [this script v]
end
set [tol v] to ((x)*(1e-10)) // tolerance for checking if fraction is (near enough) correct
set [a v] to [1]
set [b v] to ((x) mod (1)) // start with fractional part of x
forever // hopefully will get stopped by tests below...
set [a v] to ( (a) mod (b) )
set [Denominator v] to (round ((1)/(b)))
set [Numerator v] to (round ((x)/(b)))
if <([abs v] of ( (x)-((Numerator)/(Denominator)) )) < (tol)> then
stop [this script v] // the test above seemed to be the most effective way to check we have what we want
end
set [b v] to ( (b) mod (a) )
set [Denominator v] to (round ((1)/(a)))
set [Numerator v] to (round ((x)/(a)))
if <([abs v] of ( (x)-((Numerator)/(Denominator)) )) < (tol)> then
stop [this script v]
end
end

Above works quite nicely for many reasonably simple fractions. But rounding errors cause problems once the rational representation starts to have larger values on top and/or bottom.


Users can just enter 0 for the imaginary parts.
No, I mean for the result, not the operands. I'm thinking of a custom block that only deals with reals here – and it only gives a non-NaN result if the answer is also real(ish).

(In practice, many Scratchers would not have much idea about complex numbers, and would simply want a custom block that has two slots, for two variables/expressions, and so those would be real-valued and they would expect it to give back a real value, if possible.)

You do realize that rational representation is only a way to get to exponential representation. There are others.

Please read my signature! It might explain some lingo that I used in my post or comment. However, not all of it shows, so you have to highlight the entire bottom of the post, from “Please read my signature!” to where it says report, and copy and paste in into a new sheet or tab or whatever.
I can't decide my signature between:
1. I'm a grammar stickler, and my pet peeve is when people use good as an adverb.
2. I have lots of [creativity]original ideas[/creativity]. [creativity]This[/creativity] is one of those ideas.
3. If I used this symbol: ⸮ in my comment, please look it up before doing anything else.
4. {Minot, North Dakota/Donald Trump parodies} “WE'RE GONNA BUILD A DAM AND MAKE CANADA PAY FOR IT!” “When Canada sends its water, they’re not sending their best drinking water…They’re bringing sleet. They’re bringing floods. The water's toxic.” (It's just a joke that I came up with.)
5. One should make his/her signature text small; that way (s)he can cram more text into the signature.
6. New [scratchblocks] idea each week! (Thanks to Bright-Idea (Read her signature.) for inspiration/the general idea.) Here's the archive:
Note: I stopped this because of signature character limits.
Week of 1-23-2017: Hat-caps tinyurl.com/htauyu2
Week of 1-16-2017: Country-colored scratchblocks tinyurl.com/z8lz6c4
Week of 1-9-2017: Drop-down booleans tinyurl.com/j5d3k6l
Week of 1-2-2017: Block->string inputs and more truth values tinyurl.com/jqu7avs

Sigton
Scratcher
1000+ posts

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

^
Maybe you could move the discussion about imaginary numbers off of this topic since it's getting a bit off-topic now?

Sigton


dvargasews
Scratcher
500+ posts

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

Sigton wrote:

^
Maybe you could move the discussion about imaginary numbers off of this topic since it's getting a bit off-topic now?

Sigton
This discussion is about powers. I was talking about powers of imaginary numbers. If you feel that what I was talking about was off-topic, then feel free to report this conversation for being off-topic, but I doubt that Paddle2See will agree with you (He seems to be the most active moderator on the forums.).

Please read my signature! It might explain some lingo that I used in my post or comment. However, not all of it shows, so you have to highlight the entire bottom of the post, from “Please read my signature!” to where it says report, and copy and paste in into a new sheet or tab or whatever.
I can't decide my signature between:
1. I'm a grammar stickler, and my pet peeve is when people use good as an adverb.
2. I have lots of [creativity]original ideas[/creativity]. [creativity]This[/creativity] is one of those ideas.
3. If I used this symbol: ⸮ in my comment, please look it up before doing anything else.
4. {Minot, North Dakota/Donald Trump parodies} “WE'RE GONNA BUILD A DAM AND MAKE CANADA PAY FOR IT!” “When Canada sends its water, they’re not sending their best drinking water…They’re bringing sleet. They’re bringing floods. The water's toxic.” (It's just a joke that I came up with.)
5. One should make his/her signature text small; that way (s)he can cram more text into the signature.
6. New [scratchblocks] idea each week! (Thanks to Bright-Idea (Read her signature.) for inspiration/the general idea.) Here's the archive:
Note: I stopped this because of signature character limits.
Week of 1-23-2017: Hat-caps tinyurl.com/htauyu2
Week of 1-16-2017: Country-colored scratchblocks tinyurl.com/z8lz6c4
Week of 1-9-2017: Drop-down booleans tinyurl.com/j5d3k6l
Week of 1-2-2017: Block->string inputs and more truth values tinyurl.com/jqu7avs

Powered by DjangoBB