# square root function in Netlinx?

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!!

• I'm not aware of one. You could use Newton-Raphson, a lookup table, or maybe a Taylor series depending on the range of values needed. What's the application?
• Formerly Caffeinated Programmer Posts: 1,917
If I had time, I would try it myself, but I am wondering if Cafe Duet has access to a square root function (I would guess they do). If they do, it should be easy to whip up a quick square root module that either lets you pass in a value on level 1 and sends the answer to level 2, or let's you send a command and returns a string with the answer. In fact, I think someone may have already created a math function duet module .... I'll see if I can find it.

Jeff

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.

• Senior Member - 4000+ posts Posts: 4,177
Perhaps it's a little Klugee... But you could hit one of those websites that have calculators and scrape the results.
• ericmedley wrote: »
Perhaps it's a little Klugee... But you could hit one of those websites that have calculators and scrape the results.

Eeek... 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.
• ```float SquareRootFloat(float number)
{
long i;
float x, y;
const float f = 1.5F;

x = number * 0.5F;
y  = number;
i  = * ( long * ) &y;
i  = 0x5f3759df - ( i >> 1 );
y  = * ( float * ) &i;
y  = y * ( f - ( x * y * y ) );
y  = y * ( f - ( x * y * y ) );
return number * y;
}
```

If you can translate this into Netlinx it should be a good approximation of √ .
• Good luck casting your float to a long... there's no way to get the "bits" out of a float value in NetLinx, that's been discussed before.
• MorgoZ wrote: »
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!!

Try 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
}
• NetLinx is weak on math; if you are going to do anything more than a little bit of it, you want Duet. Not only does it not have the libraries, but the workarounds can eat up a lot of processor.
• DHawthorne wrote: »
NetLinx is weak on math; if you are going to do anything more than a little bit of it, you want Duet. Not only does it not have the libraries, but the workarounds can eat up a lot of processor.

What 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
• a_riot42 wrote: »
What 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

Some 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, some math 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.
• DHawthorne wrote: »
Some 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, some math 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.

I 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
• a_riot42 wrote: »
I 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

Because 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.
• Many thanks to all!

By 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 