Pages: 1
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)
Offline
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)
Offline
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?
Offline
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)
Offline
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.
Offline
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)
Offline
thanks for this. Sadly, I forgot what project it was that needed this to work...
Offline
Pages: 1