Calculating exponentials using only addition?
9 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
As a side project, I want to write a function that calculates exponentials without using multiplication or the ^ operator. So far I have it working to find the square. I have a picture of the code (you can't see it but the inputs are base (the number being raised to a power) and power (the power the base is being raised to). I've done some thinking and testing but I can't get it to work for any power higher than 2 while still working for 1 and 2. Any help? That comment at the bottom is just some guesswork by the way, not sure if it's even remotely correct.

1 Commento
David Goodmanson
il 17 Ott 2022
Modificato: David Goodmanson
il 17 Ott 2022
Hi Josiah,
If you are talking about raising numbers to integral powers, this is certainly doable. As for praticality, as long as you recognize that it's not going to be done for practical reasons then what the rest of the world thinks doesn't matter, and you might find some interesting things along the way. As Matt has mentioned, you can make a function that does multiplication of a and b, by adding a to itself b times (b an integer). If a is an integer, then f(a,a) gives a^2. With a^2 and a you can find a^3, and so forth as he also mentioned.
If you want to allow yourself a little more freedom, you could allow moving the decimal point, so effectively multiplying and dividing by 10. Then you can multiply e.g 1.23 * 4.56 = (123*456) / 1e4 which is going to take a lot of additions, but it works.
Risposte (2)
John D'Errico
il 17 Ott 2022
Modificato: John D'Errico
il 17 Ott 2022
Why in the name of the gods of computing would you possibly want to do this? Seriously? Not homework? A simple waste of CPU time. (I'm sorry, but it is.) Why not just set your computer to compiling random lists of characters, and look for the possibility that you get a Pulitzer prize winning play out the end? At last then, if it works, you get something concrete, and few would sneer at a Pulitzer prize.
Anyway, the square of a number is NOT an exponential. Just the square. So are you really asking to compute something like exp(x)? Or are you wanting to raise a number to some (INTEGER) power? There is an immense difference.
As you have observed, adding some number x to itself x times, this will form the square of x. Of course, in order to then use a loop, you need to be woring with only integers. Why in the name of god?
You could even compute the cube of a number, but that would require a double loop, and again, only for integers would it apply anyway. A 10'th power would take multiply nested loops, althought you can get tricky there. For example, using the binary expansion of 10, you would see that 10 = 8 + 2. So you could compute the square of x, save the number, then cube the square. Finally, multiply the square by the 8th power to get the 10'th power. This would effectively require far fewer loops, far less deeply nested. All it requires is the binary expansion of the desired power. (This is the trick used by powermod computations you might find for working with the moduli of powers of hugely large integers.)
If you wanted to compute a true exponential, then you would need to use a series. For example, the classical (truncated) Taylor series for exp(x)...
syms x
taylor(exp(x),'order',10)
Of course, this is merely an approximation. But you should see that it will take nested for loops, as you need to compute powers of x. And, again, that ONLY works for integers, because in order to raise x to any power of itself using only adds, you will need to use an integer number of adds. Why, oh why?
And if you are trying to do this, then really, you just need to spend some time learning numerical analysis, as then you need to learn what it will take to make that thing converge. There are, for example, tricks to make such a series converge more rapidly. But I'm sorry, this is mainly a waste of effort. It feels like you are trying to learn to do something the obscenely hard way, for absolutely no good reason. As I said, spend the time doing some reading, or even taking a class in the appropriate subject.
Do you see what I am trying to say here? It would be one thing if computers actually raised things to powers using addition as you seem to want to do. But they don't.
Finally, in the future, please don't post pictures of code. They are useless for anypne who wants to help you, as then they need to retype your code from scratch. Just paste in text.
0 Commenti
Vedere anche
Categorie
Scopri di più su Matrix Indexing in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!