News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

arithmetic-geometric mean (AGM)

Started by gusto, December 09, 2005, 11:40:11 AM

Previous topic - Next topic

gusto

new project, to find the AGM.. prof. says to use the FPU to compute the AGM... can you get the AGM by not using the FPU? all he spoke about FPU's was a list of calls on the board, he didnt really explain what they do. So far ive started workin on this project, only Floating Point call i've used is the 'fsqrt' function to calculate my second mean (sqrt[a*b])... is this right, or should i be using more FPU calls?

sheep

http://www.website.masmforum.com/tutorials/fptute/index.html

Simply FPU explains each fpu instruction and has very good examples for common things you'll be doing with them.

You'll need to know how to use fadd fdiv and fsqrt to find an AGM.

MichaelW

gusto,

You could calculate the AGM without the FPU, but doing so would turn a relatively simple task into a difficult one.

http://en.wikipedia.org/wiki/Arithmetic-geometric_mean

The AGM is the number that the two sequences (an) and (gn) converge to. The sequences can be calculated in a loop. For each iteration of the loop both calculations, an+1 and gn+1, must use the same input values (for example, if you calculate an+1 first, you cannot use the result in the gn+1 calculation, in the same iteration). You can determine when to exit the loop by comparing the results for one of the calculations with the results from the previous iteration. Exit the loop when the comparison shows the results to be equal.

The loop in my AGM procedure required 17 instructions, 14 of which were FPU instructions.

This page has a table with a few AGM values that you can use to check your results.

http://mathworld.wolfram.com/Arithmetic-GeometricMean.html

I don't know how you are supposed to display the results with the Irvine library. If you cannot do it any other way you can get an integer value, accurate to at least 8 significant digits, by using the FPU to multiply the AGM value by 10,000,000, store the result as an integer, and use available routines to display it. The displayed value will have 7 digits following the (imaginary) the decimal point.

eschew obfuscation

gusto

AGM using FPU simple task? lol u make me feel bad  :(...well this is the code im starting out with...


include \masm615\include\irvine16.inc

.data


a dword 1
b dword 9
a1 dword ?
a2 dword ?
fun1 dword ?
fun2 dword ?
space byte " ",0
root dword ?


.code
main proc
startup


mov eax,a
add eax,b
mov ch,2
div ch
xor edx,edx
mov a1,eax
mov eax,eax
mov fun1,eax
call writedec  ; a1


mov eax,a
imul eax,b
mov fun2,eax
fild fun2
     fsqrt
     fistp root
mov eax,root
mov edx,offset space
call writestring
call writedec    ; a2

exit
main endp
end main


it seems like it does what its supposed to, until i get into imaginary numbers, or decimals... im supposed to calculate the AGM of 1 and sqrt 2... im using 1 and 9 as my test numbers for the moment... so the way im going about it is wrong? i shouldnt be using regular add or mul? how do i load numbers into ST, i took a look @ the page sheep posted but didnt read thorougly because i am at work, im not sure if it says how to... and would i load (a) and (b) as ST(0) and ST(1) ?...

MichaelW

From the Wikipedia page:
Quote
These two sequences converge to the same number, which we call the arithmetic-geometric mean M(x, y) of x and y.

Your code is calculating only the first set of values in the sequences. The values are correct when starting with x=1 and y=9, but because of the integer operations (including the square root being rounded to an integer), none of the other values in the sequence will be correct. And for starting values other than 1 and 9 even the first set of values may be incorrect. Here are the correct sequence values for x = 1 and y = 9:

a = 5.00000000000000  g = 3.00000000000000
a = 4.00000000000000  g = 3.87298334620742
a = 3.93649167310371  g = 3.93597934253086
a = 3.93623550781729  g = 3.93623549948183
a = 3.93623550364956  g = 3.93623550364956


And here is what you code, modified to run as a loop, produces:

5 3
4 4
4 4
4 4
4 4


For x = 1 and y = 9 the correct AGM value, rounded to 15 significant digits, is 3.93623550364956. To get an accurate value you must use the FPU. And because you cannot readily transfer data between the IPU registers and the FPU, it would be most convenient to perform all of the calculations on the FPU. The FP tutorial covers everything that you need to know to do this.

eschew obfuscation