Basically, I need to implement a function on an embedded system that only has ADD and SUBTRACT opcodes. The function is: f=440*2^((x-69)/12) (A simple Pitch Space function). I can do multiplication and division using repeated addition and bit shifting, but my power rules are pretty rusty and I cannot get my head around this problem.

Note: I know this is possible because I am using the same Zilog z80 processor as the Ti-83, but stripped down a bit. If anyone knows where to get assembly source code for Ti-83 that has power function in it, that would save me a lot of work. I have searched far and wide with no luck.

## Exponents using only addition and subtraction

**Moderators:** gmalivuk, Moderators General, Prelates

### Re: Exponents using only addition and subtraction

Do you know that

If the exponent is a decimal, you'll have a tricker time, the best I can think of this late at night is to take the exponent

^{(x-26)}/_{12}is an integer? If so, you can implement exponents using repeated multiplication or division (just increment/decrement the index, performing a multiplication/division each time until the exponent is 0).If the exponent is a decimal, you'll have a tricker time, the best I can think of this late at night is to take the exponent

^{p}/_{q}and use the above method to find 2^{p}and then find the qth-root of this number using an approximating technique. Simple (but slow) method is a basic binary search (pick a number, raise to the qth power and test if it is too big or too small, adjust accordingly and test again). You will never get an exact answer unfortunately (if x is 75 then the answer required is 440*sqrt(2), an irrational number) so you'll have to be approximating somewhere.while ((*(iterator++) != (LExpression*)next) && (iterator != m_vector.end()){}

### Re: Exponents using only addition and subtraction

If somebody could refresh me, can I not just pull the 2

Hmm, just realized that still leaves me with a loop of ~1.12246*~1.12246 x times which, with repeating addition, is still REALLY drawn out (Plus another 440 additions after that).... Come to think of it, a frequency table doesn't sound so bad after all...

^{1/12}out and go ahead and solve that leaving me ~1.12246^{x-69}*440? That would simplify things a lot.Hmm, just realized that still leaves me with a loop of ~1.12246*~1.12246 x times which, with repeating addition, is still REALLY drawn out (Plus another 440 additions after that).... Come to think of it, a frequency table doesn't sound so bad after all...

### Re: Exponents using only addition and subtraction

The TI source code is held pretty closely, so it's unlikely you'll be able to locate it anywhere.

That said, the method that TI calculators use to calculate everything that's not addition or subtraction is the CORDIC algorithm. Do you have methods available to store and retrieve data from a table? Addition, subtraction, digit shifts and table lookup is all it uses. With enough cycles, you can get arbitrary precision out the algorithm, if need a real accurate answer.

In any case, I direct you to my professor's webpage, he's got a paper or two on calculator methods. http://www.math.ufl.edu/~be/papers.html

The first paper, How Calculators Calculate ought to cover it.

Hope that's helpful.

That said, the method that TI calculators use to calculate everything that's not addition or subtraction is the CORDIC algorithm. Do you have methods available to store and retrieve data from a table? Addition, subtraction, digit shifts and table lookup is all it uses. With enough cycles, you can get arbitrary precision out the algorithm, if need a real accurate answer.

In any case, I direct you to my professor's webpage, he's got a paper or two on calculator methods. http://www.math.ufl.edu/~be/papers.html

The first paper, How Calculators Calculate ought to cover it.

Hope that's helpful.

- evilbeanfiend
**Posts:**2650**Joined:**Tue Mar 13, 2007 7:05 am UTC**Location:**the old world

### Re: Exponents using only addition and subtraction

how much ram you got free? you can always implement it as a look up table (perhaps with some interpolation between table entries if space is a problem and time isn't)

in ur beanz makin u eveel

- parallax
**Posts:**157**Joined:**Wed Jan 31, 2007 5:06 pm UTC**Location:**The Emergency Intelligence Incinerator

### Re: Exponents using only addition and subtraction

There's always Taylor polynomials. I think most calculators use Taylor polynomials or some variant to calculate trigonometric functions. The Taylor polynomial for that function is 1 + [ln2(x-69)/12]

It should converge rather quickly.

^{1}/2! + [ln2(x-69)/12]^{2}/3! +[ln2(x-69)/12]^{3}/4! + . . .It should converge rather quickly.

Cake and grief counseling will be available at the conclusion of the test.

### Re: Exponents using only addition and subtraction

A few questions:

How big/small is "x" and what sort of accuracy (how many significant figures) do you need for your result? Can "x-69" be negative?

Why can't use use the other instructions in the Z80?

As your always multiplying by powers of two the bitwise shift command, and the ability to grab one bit from a register, would be extreamly useful in this problem. I know the Z80 has the former, does it have the latter?

How big/small is "x" and what sort of accuracy (how many significant figures) do you need for your result? Can "x-69" be negative?

Why can't use use the other instructions in the Z80?

As your always multiplying by powers of two the bitwise shift command, and the ability to grab one bit from a register, would be extreamly useful in this problem. I know the Z80 has the former, does it have the latter?

### Re: Exponents using only addition and subtraction

Seraph wrote:A few questions:

How big/small is "x" and what sort of accuracy (how many significant figures) do you need for your result? Can "x-69" be negative?

Why can't use use the other instructions in the Z80?

As your always multiplying by powers of two the bitwise shift command, and the ability to grab one bit from a register, would be extreamly useful in this problem. I know the Z80 has the former, does it have the latter?

Sorry, I wasn't making myself clear earlier. 0<=x<=127 so it will be negative at times. As for sig figs, what is the human hearing threshold? Like .1? .01 at most.And I can use almost any other function the z80 has. I was just trying to say, "I need to do this w/o multiplication and division." After a lot of thought and writing out some of the code needed for this, I realized it would be a lot simpler to calculate the values at assembly time (Where I have */^) and store them in a table. Time really is the problem. This is going to end up being a Gameboy MIDI player and the timing must be (near) perfect.

### Re: Exponents using only addition and subtraction

The most efficient method would probably use a table which gives f(x) for 12 different values of x, then use bit shifts to calculate f(x) for any other value of x.

### Re: Exponents using only addition and subtraction

For integers...

x^2

1 4 9 16 25 36

3 5 7 9 11

2 2 2 2

x^3

1 8 27 64

7 19 37

12 18

6

x^4

.....

....

...

..

24

etc

This is probably useless to you, but fun nonetheless.

x^2

1 4 9 16 25 36

3 5 7 9 11

2 2 2 2

x^3

1 8 27 64

7 19 37

12 18

6

x^4

.....

....

...

..

24

etc

This is probably useless to you, but fun nonetheless.

### Who is online

Users browsing this forum: No registered users and 6 guests