This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.
  • Index
  •  » Advanced Topics
  •  » Easy way to do exponents - No repeats, and supports decimal exponents!

#1 2010-03-08 00:38:42

billyedward
Scratcher
Registered: 2008-01-03
Posts: 500+

Easy way to do exponents - No repeats, and supports decimal exponents!

Sound too good to be true?
Well, read on:
One of the troubles that has plagued scratch users from the 0.1 days is the lack of exponents.
Such as 2^4 should return 16, 3^3 should be 27, etc.
recognising this mathematical devoision I added this block to streak, however due to compatibility this really is an unfit solution.

There is the really obvious answer: to use a repeat loop to repeatedly multiply the number by the exponent times, however this is slow, tedious, and only supports positive integer, non- zero exponents. (I know, I know, you can modify the code to support negatives and zero, too, but this makes it 200% more tedious.)

So recently having the same problem using a pascal compiler which didn't support this either, I did some digging on the logarithmic functions which come with scratch, and came up with a surprisingly convenient answer.

We simply need to combine two rarely-used functions to get it to work...
10^ and log.

All you have to do is use (10^ of (exponent * (log(number) ) ) ), to raise a number to any power. To get the nth root of a number, use the same, except instead of exponent use (1/root), because a number to the power of reciprocal of a number will give the numberBth root of numberA.

Given this, then, 27^(1/3) = 3.

See this project to see this in action, and to see a screenshot.

Last edited by billyedward (2010-03-08 21:49:56)


"I'd love to change the world, but they haven't released the source code yet."
Check out the latest version of Streak --> http://billy.scienceontheweb.net/Streak

Offline

 

#2 2010-03-08 16:20:29

nXIII
Community Moderator
Registered: 2009-04-21
Posts: 1000+

Re: Easy way to do exponents - No repeats, and supports decimal exponents!

Technically, this is wrong, if given x^y it is not [/blocks]10^(y * ln(x) ), but rather 10^(y * log(x) )

Ln is the natural logarithm, base e.
Log is the base-ten logarithm, the one you want here because you are raising 10 to the power.

Wow, you should have seen what BBCode did to that before I put a space between double-parentheses.

Last edited by nXIII (2010-03-08 16:31:54)


nXIII

Offline

 

#3 2010-03-08 21:43:16

MathWizz
Scratcher
Registered: 2009-08-31
Posts: 1000+

Re: Easy way to do exponents - No repeats, and supports decimal exponents!

nXIII wrote:

Technically, this is wrong, if given x^y it is not [/blocks]10^(y * ln(x) ), but rather 10^(y * log(x) )

Ln is the natural logarithm, base e.
Log is the base-ten logarithm, the one you want here because you are raising 10 to the power.

Wow, you should have seen what BBCode did to that before I put a space between double-parentheses.

Awesome!

What DID it do?


http://block.site90.net/scratch.mit/text.php?size=30&text=%20A%20signature!&color=333333

Offline

 

#4 2010-03-08 21:49:09

billyedward
Scratcher
Registered: 2008-01-03
Posts: 500+

Re: Easy way to do exponents - No repeats, and supports decimal exponents!

nXIII wrote:

Technically, this is wrong, if given x^y it is not [/blocks]10^(y * ln(x) ), but rather 10^(y * log(x) )

Ln is the natural logarithm, base e.
Log is the base-ten logarithm, the one you want here because you are raising 10 to the power.

Wow, you should have seen what BBCode did to that before I put a space between double-parentheses.

Oh, I'm sorry, I meant log.
The thing is, pascal doesn't have 10^, so I must use exp(n*ln(m) ), which also works.
I originally wrote it for this, but upon finding that the e^|log method was faster, I switched it to that, but I guess I overlooked that one ln.

Thank you for pointing this out, it has now been changed.

Last edited by billyedward (2010-03-08 21:49:24)


"I'd love to change the world, but they haven't released the source code yet."
Check out the latest version of Streak --> http://billy.scienceontheweb.net/Streak

Offline

 

#5 2010-03-09 08:17:38

filo5
Scratcher
Registered: 2008-01-08
Posts: 1000+

Re: Easy way to do exponents - No repeats, and supports decimal exponents!

nXIII wrote:

Technically, this is wrong, if given x^y it is not [/blocks]10^(y * ln(x) ), but rather 10^(y * log(x) )

Ln is the natural logarithm, base e.
Log is the base-ten logarithm, the one you want here because you are raising 10 to the power.

Wow, you should have seen what BBCode did to that before I put a space between double-parentheses.

Yeah. )). Scratch blocks gone mad.


Converting my Scratch projects to Python!

Offline

 

#6 2010-03-09 14:52:08

markyparky56
Scratcher
Registered: 2008-03-20
Posts: 1000+

Re: Easy way to do exponents - No repeats, and supports decimal exponents!

filo5 wrote:

nXIII wrote:

Technically, this is wrong, if given x^y it is not [/blocks]10^(y * ln(x) ), but rather 10^(y * log(x) )

Ln is the natural logarithm, base e.
Log is the base-ten logarithm, the one you want here because you are raising 10 to the power.

Wow, you should have seen what BBCode did to that before I put a space between double-parentheses.

Yeah. [blocks]))[/blocks]. Scratch blocks gone mad.

You sure its classed as BBcode? i would have thought that they'd have used the smiley plugin for it. It would make sense.

[blocks](( ))[/blocks]

Last edited by markyparky56 (2010-03-09 14:52:50)


http://j.mp/jgVnTq
Check out my game engine development site: NewDawn I'm a Level 171 Scratcher.I am http://bit.ly/nkvLNT

Offline

 

#7 2010-03-09 15:08:03

juststickman
Scratcher
Registered: 2009-05-31
Posts: 1000+

Re: Easy way to do exponents - No repeats, and supports decimal exponents!

big_smile  thanks for this. Sadly, I forgot what project it was that needed this to work...


http://is.gd/iBQi2 Add grob to your sig and help with world dominiation!http://is.gd/iBQ9Q                                                             Hey guys, we're seriously naming our team bob?

Offline

 

#8 2010-04-24 19:08:47

roger-
Scratcher
Registered: 2010-02-13
Posts: 100+

Re: Easy way to do exponents - No repeats, and supports decimal exponents!

Code:

exp: t1 raisedTo: t2 
    | t3 |
    t3 _ t1 raisedTo: t2.
    ^ t3

What about this?

Last edited by roger- (2010-04-25 10:53:46)


Food is Fat. Fat is power.
        I am almighty.

Offline

 
  • Index
  •  » Advanced Topics
  •  » Easy way to do exponents - No repeats, and supports decimal exponents!

Board footer