# square root function in Netlinx?

MorgoZ
Junior MemberPosts:

**116**
Hello!

The question is simple, i just need to know if there is a square root function or similar in Netlinx (like the "sqrt" in C).

Thanks to all!!

The question is simple, i just need to know if there is a square root function or similar in Netlinx (like the "sqrt" in C).

Thanks to all!!

0

## Comments

3201,917Jeff

Here is the thread that has the math program.... unfortunately, it doesn't seem to have sqrt. Should be easy enough to add though if cafe duet has a sqrt root function.

http://www.amxforums.com/showthread.php?t=3657

4,177966Eeek... that would kill your code performance. Duet will do what you need. I'm out on site at the moment, but I'm pretty sure I've got a Duet math module sitting back at the office. I'll have a look if I get a chance.

1,619If you can translate this into Netlinx it should be a good approximation of √ .

320159Try this

//subroutine to find square root

define_function float SquareRoot (integer intNumber)

stack_var integer x

stack_var float fltResult

stack_var float fltSqrIndex

stack_var float fltTrash

{

for(x = 1; x <= intNumber; x++)

{

if((x*x) > intNumber)

{

fltSqrIndex = x

break

}

else if((x*x) = intNumber)

{

fltResult = x

break

}

}

if(fltSqrIndex)

{

for(x = 1;x <= 10; x++)

{

fltTrash = (intNumber/fltSqrIndex)

fltSqrIndex = ((fltSqrIndex + fltTrash)/2)

}

fltResult = fltSqrIndex

}

return fltResult

}

4,5841,619What makes you say that? I haven't found this to be the case. I have some math routines that convert x,y coordinates to polar coordinates and Netlinx has no problem with it even if you are moving your finger on the panel as fast as you can.

Paul

4,584Some calculations are intrinsically easier to make than others. Look at the above examples for finding a square root, as opposed to a built-in function. Not long ago, I needed to calculate an nth root, and that was a horror that locked the processor up until I dramatically scavenged the resolution of the result. Sure,

somemath calculations are no problem, but others are more than a major pain; in Java, it's simply including the proper library. Read my posts on Duet, and you will see I'm no great fan, but this is one area where it vastly outstrips basic NetLinx. Fortunately, most applications don't need advanced math.1,619I am only using the built in operators */+- with floats/doubles since that is all I have at my disposal in Netlinx. The JVM usually uses native libraries for the math functions if they are available and I can't confirm that FP math is done in hardware on an NI. I would guess that the JVM has to use numerical methods the same way we would, and would end up being no faster in Duet than a comparable method using Netlinx. I haven't tested it, but I haven't found any reason to need any more speed when doing math to justify spending the time to look into it further. If you have an exponent function that runs fast I don't understand why a nth root function would run much slower. I haven't found FP math to be unbearably slow on the AMX processor thankfully.

Paul

4,584Because an nth root function is a brute force method with multiple recursive routines. Perhaps the one experience has soured me and it's not representative.

116By now it is not very important to use the square function, but i?ll try to use the code of Bano and, if there is any problem, to make it in Duet.

Thanks for the help and the code