Calculating x^y, 2^x, etc.
Code:
Theory:
x^y
If y is an integer, we can repeatedly multiply (or use even faster algorithms that repeatedly square and multiply).
If y is not an integer, we generally make the substitution
x^y = e^(y ln( x )) or x^y = 2^(y log2( x ))
and use some subroutine to calculate ln(x) and e^b -- or, alternatively, calculate log2(x) and 2^b.
(Is there a page somewhere on massmind describing logarithms and exponentials? Move the following text there.)
"Richard Feynman and The Connection Machine" by W. Daniel Hillis 1989 briefly describes Richard Feyman and the algorithm:
An Algorithm For Logarithms
Feynman worked out ... the subroutine for computing logarithms. I mention it here not only because it is a clever algorithm, but also because it is a specific contribution Richard made to the mainstream of computer science. He invented it at Los Alamos.
Consider the problem of finding the logarithm of a fractional number between 1.0 and 2.0 (the algorithm can be generalized without too much difficulty). Feynman observed that any such number can be uniquely represented as a product of numbers of the form 1 + 2-k, where k is an integer. Testing each of these factors in a binary number representation is simply a matter of a shift and a subtraction. Once the factors are determined, the logarithm can be computed by adding together the precomputed logarithms of the factors. The algorithm fit especially well on the Connection Machine.... The entire computation took less time than division.
Scott Dattalo has written a far more detailed explanation of Feynman's log2(x) algorithm, with worked-out examples .
Questions:
| file: /techref/microchip/math/power/index.htm, 4KB, , updated: 2007/5/16 22:40, local time: 2010/3/12 13:25,
38.107.191.113:LOG IN
|
| ©2010 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions? Please DO link to this page! Digg it! <A HREF="http://piclist.com/techref/microchip/math/power/index.htm"> PIC Microcontoller Basic Math Power Methods</A> |
| Did you find what you needed? |
|
Ubicom SX18 thru SX52, PIC 16c5X compatibile, 50 to 75 MIPS microcontrollers! |
Robotics nuts!Check out http://www.verinet.com/~dlc/ email: dlc@verinet.com... This guy ROCKS! He has made (and sells but also releases code, docs, etc...) for a number of cool little robotic modules including whiskers, IR proximity detect and remote control, Sonar proximity detect, PWM, Servo, compass. Most of these use the little PIC 12C508 controller which costs basically nothing and is soooo tiny.The 4 servos, 2400 baud serial servo controller is a wonder of magic and he sells the programmed chip for $8. Wow! |
.