square root function in Netlinx?
MorgoZ
Junior Member
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!!
Comments
-
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?
-
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.
http://www.amxforums.com/showthread.php?t=3657 -
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.
-
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 -
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 -
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
Categories
- All Categories
- 2.5K AMX General Discussion
- 922 AMX Technical Discussion
- 514 AMX Hardware
- 502 AMX Control Products
- 3 AMX Video Distribution Products
- 9 AMX Networked AV (SVSI) Products
- AMX Workspace & Collaboration Products
- 3.4K AMX Software
- 151 AMX Resource Management Suite Software
- 386 AMX Design Tools
- 2.4K NetLinx Studio
- 135 Duet/Cafe Duet
- 248 NetLinx Modules & Duet Modules
- 57 AMX RPM Forum
- 228 MODPEDIA - The Public Repository of Modules for Everyone
- 943 AMX Specialty Forums
- 2.6K AMXForums Archive
- 2.6K AMXForums Archive Threads
- 1.5K AMX Hardware
- 432 AMX Applications and Solutions
- 249 Residential Forum
- 182 Tips and Tricks
- 146 AMX Website/Forums
