I have a routine for calculate the x^y with C calls. It works fine with some numbers but with another ones it returns bad values. Some friend has said me that the problems is the range of one instruction (mybe log2?). Anyone can explain me how I can solve the problem? I have search in the forums and everywhere I find the same method to calculate x^y that I use.

Push BP
Mov BP, SP


Fyl2x ; st(1) * log2 st(0)
F2xm1 ; (2^x) -1
Fld1 ; Load +1.0

Pop BP

Thanx :)
Posted on 2002-12-09 09:56:45 by wildark
i think there's nothing wrong with the algorithm, but it seems that you should fld and instead of and
Posted on 2002-12-09 10:21:50 by longer
Obviously, your code would not work when the procedure is called because of your wrong references to the stack address. You probably tested it without calling it. Then it may work for source numbers between 0 and 2 exclusive regardless of the exponent, or for source numbers higher than 2 depending on the exponent.

First, you cannot use negative numbers as a source number. If you need to raise a negative number by an exponent, do it on the absolute value and adjust the result as required. The FPU will also dislike an input of 0 for the source number. you should avoid that since 0 to any power is always 0.

Following is my code for x^y (which you will find soon in the upcoming Fpu.lib library of functions for the FPU), after loading the exponent followed by loading the source number:

fyl2x ;->log2(Src1)*exponent

;the FPU can compute the antilog only with the mantissa of the logarithm
;the characteristic of the logarithm must thus be removed

fld st(0) ;copy the logarithm
frndint ;keep only the characteristic
fsub st(1),st ;keeps only the mantissa
fxch ;get the mantissa on top
f2xm1 ;->2^(mantissa)-1
fadd ;add 1 back

;the number must now be readjusted for the characteristic of the logarithm

fscale ;scale it with the characteristic

;the characteristic is still on the FPU and must be removed

fxch ;get the characteristic on top
fstp st(0) ;"pop" it

;the result is now on top of the FPU stack ready for the next operation

Posted on 2002-12-10 12:45:12 by Raymond