Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Interesting Challenge
Message
De
31/08/1999 11:13:03
 
 
À
30/08/1999 15:06:10
Information générale
Forum:
Visual FoxPro
Catégorie:
Autre
Divers
Thread ID:
00259328
Message ID:
00259681
Vues:
15
>I recently came across the need to do a financial calculation that cannot be handled Algebraically. IOW, the only way to find the correct number is to guess the answer and work backwards. The math is easy. But the "guessing" algorithm turned out to be be a very interesting piece of code, and I am curious if my solution was the standard way of handling things. I am also interested to see how you might have handled it.
>
>My particular problem was far too complex to make a challenge of, but I can give a very simple riddle whose solution would need to use the same principles...
>
>Write a function to calculate the square root of a number to a given number of decimal places. (Yes. I know, VFP has the SQRT function, but what would you do without it?)

Alright- here was mine.

FUNCTION FindRoot
LPARA tnSquare, tnPrecision
lnTry = tnSquare
lnIncrement = tnSquare/2
lnSquare = tnSquare
lnPrecision = IIF(!EMPTY(tnPrecision), tnPrecision, SET("DECIMAL"))
DO WHILE ROUND(lnTRY^2, lnPrecision) <> lnSquare
lntry = lnTry + lnIncrement
?lnTry, lnIncrement, ROUND(lnTRY^2, lnPrecision)
IF (lnIncrement > 0 AND lnSquare < lnTry^2) OR (lnIncrement < 0 AND lnTry^2 < lnSquare)
lnIncrement = lnIncrement /-2
ENDIF
ENDDO
RETURN lnTry

This was a very interesting excercise indeed. Only one of your solutions even used the same principles as I did. Mike and Christof circumvented the problem altogether, and Bruce- I have to admit I have no idea what your code is doing, though I am sure it is the correct mathematical way to handle the problem. Thanks for your inpput everybody...
Erik Moore
Clientelligence
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform