Mathematic Calculation of Square Root

Thread: Mathematic Calculation of Square Root

1. Junior Member
Join Date
Dec 1969
Posts
2

Mathematic Calculation of Square Root

How does one calculate a Square Root of a variable in ASP?

2. Senior Member
Join Date
Dec 1969
Posts
10,852

RE: Mathematic Calculation of Square Root

sqr(Variable)

3. Senior Member
Join Date
Dec 1969
Posts
96,118

That's no fun!

You should write your own implementation! Maybe a Newton-Raphson iterative technique?<BR><BR>&#060;major grin/&#062;<BR><BR>

4. Senior Member
Join Date
Dec 1969
Posts
2,930

speaking of writing own implementations

i wrote a MOD function, but didn&#039;t know if it was necessary... is there a different mod function in vbscript that can work with numbers bigger than the max value that the original mod function that we all know and love can work with?

5. Senior Member
Join Date
Dec 1969
Posts
946

We love you Bill!!! =)

.

6. Senior Member
Join Date
Dec 1969
Posts
96,118

If you mean built-in...

...no.<BR><BR>How did you write yours?<BR><BR>Function FMod( n, m )<BR>&nbsp; &nbsp; FMod = n - m * Int( n/m )<BR>End Function<BR><BR>Probabaly not worth even creating a function for that, is it?<BR><BR>Just do it inline?<BR><BR><BR>

7. Senior Member
Join Date
Dec 1969
Posts
2,930

no

if i knew that trick, i would have ;-) mine was superiorly inefficient, but correct nonetheless.

8. Senior Member
Join Date
Dec 1969
Posts
10,852

LOL....Well...

If I had time, I&#039;d love to. Maybe this evening. ;-)

9. Senior Member
Join Date
Dec 1969
Posts
96,118

That's from the definition of MOD

That is, MOD is the remainder after division.<BR><BR>Which is, of course, the fractional part of the quotient multiplied by the divisor.<BR><BR>With a FRACT you would do:<BR><BR>Function FMod( n, m )<BR>&nbsp; &nbsp; FMod = m * FRACT( n/m )<BR>End Function<BR><BR>But VBS doesn&#039;t have a FRACT, so...<BR><BR>So we write our own FRACT thus:<BR><BR>Function FRACT( n )<BR>&nbsp; &nbsp; FRACT = n - Int(n)<BR>End Function<BR><BR>And then we substitute the FRACT function inline in FMOD to get this:<BR><BR>Function FMod( n, m )<BR>&nbsp; &nbsp; FMod = m * ( n/m - Int(n/m) )<BR>End Function<BR><BR>But that has the disadvantage the you might lose precision in the floating point math. So you simply "distribute" the multiplier (m) to each of the terms inside the parens and get<BR><BR>Function FMod( n, m )<BR>&nbsp; &nbsp; FMod = n - m * Int(n/m)<BR>End Function<BR><BR>And now there&#039;s no loss of accuracy from floating point foibles.<BR><BR>

10. Junior Member
Join Date
Dec 1969
Posts
2

RE: Mathematic Calculation of Square Root

You rock.. Thanks for the amazingly quick reply.

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•