How do you do exponential math on the Propeller?
Don M
Posts: 1,652
Lets say if:
x := 3
y := 5
z := answer
How would you do z := x^y in spin? (3^5 = 243)
x := 3
y := 5
z := answer
How would you do z := x^y in spin? (3^5 = 243)
Comments
But it doesn't work. I get Z = 729
This works.
Is this the only way to do this type of math on the Prop?
If you don't have large exponents, this is probably the easiest way to go.
If you use F32, you can do it like this:
log z = y * log x
z = log-1(y * log x)
-Phil
How about some examples of using the built in LOG ANTILOG tables in spin?
I would really like to convert the procedures into a FemtoBasie function.
Duane J
The Propeller Manual appendix B discusses this in PASM but I didn't get very far.
Duane J
-Phil
Not my strong suit, else I would have done it. Perhaps Tracy Allen -- who is in fact a math wizard -- will chime in with something neat.
Marty
Note, if you expect the exponent to be very large, something like this can speed it up: The break-even point is somewhere around 8, I think, so probably not generally that useful. [8^)
Jonathan
Nope.
(AnyNumber)0 = 1
Try it on a scientific calculator. Even 00, -00, 10, -10000 all = 1
Duane J
There are a couple of stumbling blocks mortals encounter in a direct attack on the HUB tables for exp and log The tables are in base 2, but problems are often framed or answered in a different base. That is handled by multiplying by a fractional constant at the right point in the calculation. If doing it in integer math, one has to be familiar with how to do fractional multiplication with something like the ** operator. Second, the HUB tables cover only one octave (power of 2) range. The range is input 0–1 and output 1–2 for exp2, and vice versa for log2, they are inverse functions. Not to worry, the functions are multiplicative by octaves, so larger inputs and outputs only need to be scaled by factors of two in a wrapper. Not only that, the values from 0–1 are represented as implied fractions, 0/2048 to 2047/2048 for the inputs, 11 bits as the input address. It is the numerator you are reading out of the table. Outputs are implied 16 bit fractions from 0/65536 to 65535/65536.
Attached is a simple demo that uses the HUB tables for exp2 and log2, over the restricted 0-2 range.
Methods using log and exp are not so great for integer exponentiation. There will be errors that accumulate. Better just multiply it out, up to a point.
In floating point, all that scaling, interpolation, and base conversion is taken care of behind the scenes. The wizardly cordic-like algorithm Lawson used for Flat Math Extended (Spin) is much more accurate than the linear HUB table lookup. The same notions apply though, about the range reduction and scaling.
Jonathan
Yep, because the button on my trusty HP 20S calculator is marked yx
Zero multiplied by itself zero times. Can anyone explay why that equals 1? I too have been taught the answer is 1, but I never understood why that is so. My calculator (RealCalc App) actually returns "Error" when I do this calculation.
Erlend
I thought it was undefined because there are two answers:
x ^ 0 = 1
0 ^ x = 0
So 0 ^ 0 could be either 0 or 1.
limx->0+(0x) = 0
I think it's just a matter of convention that the first is the one that's accepted for interpreting 00, as either could be viewed as correct.
To illustrate further the nastiness that occurs right at zero, try taking the log of 00. You get zero times negative infinity.
-Phil
I completely forgot about negative exponents which just makes things worse.
0 ^ x where x < 0 is essentially 1 / (0 ^ x) which is division by zero for any non-zero x. So shouldn't 0 ^ x be undefined for x < 0?
Which brings us back to 0 ^ 0. I thought one of the many reasons why division by zero is undefined is that it approaches two separate limits (+- infinity) when approached from opposite side of the x axis). So 0 ^ x also has two different values when approached from opposite sides as well. That seems to imply to me that 0 ^ 0 is also undefined.
Update: Holy smokes, we're not the only ones confused. Here are two different answers to the same question.
http://www.math.hmc.edu/funfacts/ffiles/10005.3-5.shtml
http://www.math.utah.edu/~pa/math/0to0.html
I am close to illiterate wrt math. I hate that. I regret I did not realise that math is the programming language until it was so late in life that it is very hard to learn it. I keep reading the Feinman Lectures, but lots of it stops at almost-grasping level. Frustrating..
Erlend
- 1^any power = 1
- any^0 = 1
- 0^any power = 0
- any ^ negative number = 0
- compute
The rules happen basically in order, so 0^0 will return 1 (on my TI-85 it returns 'ERROR 04 DOMAIN' [8^) Here is the code: The simple version just loops b times...the runtime is O( b ).The 'a2b_high_bit' version optimizes the high bit of b, and once the power is 8 or higher it is faster than the simple case (about 2x faster on average). It is extremely quick when the power b is actually a power of 2 (faster than the log version).
The 'a2b_log_time' version is O( log2 N ), but has more overhead. Once power is > 12 it is faster than the simple version. And on average it is also much faster than the high-bit version. If I was coding Exp in PASM, this is the version I would choose.
Jonathan
It's quite possible to set up a systems of mathematical axioms that do not describe any reality we are aware of but self-consistent none the less. I don't know whose definition you are referring to but I'll go for that. I see that Maths happens in the brains of mathematicians, that is humans, and humans are made of the stuff of the real world. Ergo Maths is inside reality, so to speak. There are some problems with this idea. As far as I can tell maths is quite capable of describing things that do not or cannot exist.
As a simple case try drawing a graph of y = 1/x.
For all the positive numbers that goes nicely, as x gets bigger and bigger y gets smaller and smaller.
For all the negative numbers it goes well. as x gets more and more negative y gets smaller and smaller but is negative.
But as you approach zero from the positive side things run away to plus infinity. As you approach zero from the negative side things run away to minus infinity. So what do we say in the middle there at y = 1 / 0 ? Is it positive or negative? And what is this infinity anyway? We have mathematically constructed a thing that cannot be, physically or logically by the looks of it.
In physics we have things like gravitational force between two objects: F = G * m1 * m2 / (1 / r^2)
Sort of works, since the days of Newton. And looks a bit like our simple 1 / x thing above. But what does it mean when the distance "r=0"? An infinite amount of force holding two together? A black hole? Can we even have two masses at the same point in space? Does it ultimately make any sense to speak of a mass being "at a point"?
And what about negative values of G or m? Does that make any sense?
This simple mathematical model clearly tries to describe a lot more than the "reality" we see out there.
So what about that 0^0 thing. Seems we cannot look to "reality" to tell us what it should be. We have to decide for ourselves: http://www.askamathematician.com/2010/12/q-what-does-00-zero-raised-to-the-zeroth-power-equal-why-do-mathematicians-and-high-school-teachers-disagree/
This subject fascinates me, but I do not pretend to understand half of it. I regularily google - or read a book on - math/reality/quantum/etc, because I enjoy when my brain has to strain to understand. So, I do know that Plato and all the others are not agreeing on all the 'truth'.
Often I get a sort of I-almost-understand-it-now experience, but strive (forever) to get the final Eureka. One example of this is the Goedel theorem which aims to prove that a computer by principle cannot become creative. I feel I understand the reasoning enough to agree, but not enough to explain it to others. (-and what it therefore proves too, is that the brain is not a compuing device only, but that there is something more, something unknown).
There is also a 'proof' that all math must be reflected in reality, I cannot recall by whom, and it is hard to understand, and it by no means reflects a consensous, but it sort of feels right to me.
Maybe I suffer from stack overflow?
Erlend
I do sympathize. I spent fours years studying Physics. It was kind of a shock to find that it was wall to wall mathematics everyday with the odd break for an actual lab experiment or project. Very different from school time science class. Most of it went over my head, a few gold nuggets hit home. Most of that has been forgotten since.
Anyway we are in good company because "Mathematicians are chronically lost and confused", or at least according to this one http://j2kun.svbtle.com/mathematicians-are-chronically-lost-and-confused
P.S. Re: Goedel ... therefore proves too, is that the brain is not a compuing device only, but that there is something more, something unknown.
There are many that argue against this interpretation of Goedel's proofs.
P.P.S. Do check out "Numberphile"http://www.numberphile.com/ I think you'll love it.
As regards the brain, I firmly believe that Nature has a few more tricks up her sleve than von Neumann. In general, science people should be more humble to the fact that there is lots and lots more to learn - and unlearn. Bodes well for an exciting future, I think.
Erlend
In my experience of "science people" they are very aware of the fact that the human race is only just lifting the corner of the rug when it comes to understanding the universe. They will say that making some discovery or finding an answer to a question always makes more questions and exposes more of what we don't know. They are very humble that way. See for example that article on the confused mathematicians above.
It's the case that "The more you know, the more you know that there is more you don't know"
Apologize - I stepped into the generalising trap - my comment is only relevant to the (probably relativily few) scientists who loudly deny any questions to established truth. I am not part of the crack-pot community (I hope), but it so saddens me when someone claims e.g. that the Grand Unified Theory is just around the corner, and it will be the final one. Unfortunately the crack-pots/hyper-religous/alternative-energy people do serious damage by derailing what could otherwise have been an openminded and constructive discussion.
I am not a scientist, but my job is working with advanced knowledge, and I have seen so many times how important it is to allow for asking questions which goes across disciplines and knowledge levels, even if they may appear disrespectful or stupid.
Erlend