Xmlspy Posted August 23, 2014 Share Posted August 23, 2014 Does anyone know if Leadwerks has a square root function for LUA? EDIT: sqrt is highlighted in blue, but it doesn't work. The reason I want a square root function is to determine the distance between two points using xy positions and the good ol distance formula sqrt( (x2-x1)^2 + (y2-y1)^2 ). Since there is no square root function (that I have found) one workaround is to create two pivot points, give them the x and y positions of each point, then use pivotOne:GetDistance(pivotTwo). Quote Link to comment Share on other sites More sharing options...
Rick Posted August 23, 2014 Share Posted August 23, 2014 http://lua-users.org/wiki/MathLibraryTutorial Quote Link to comment Share on other sites More sharing options...
Xmlspy Posted August 23, 2014 Author Share Posted August 23, 2014 I'm still getting used to LUA, but I find this to be a bit weird because I really want to write Math:Sqrt because many functions are written in that way. I guess it will be math.sqrt for now. Thanks. Quote Link to comment Share on other sites More sharing options...
Rick Posted August 23, 2014 Share Posted August 23, 2014 The functions in the link I provided are part of the lua language. The ones you see like, Math:Sqrt, are part of the Leadwerks library. Every language has some base functions/features they provide but then people can make libraries using that language. I'm not 100% sure why Josh felt the need to duplicate some of these built in functions that Lua already had. Quote Link to comment Share on other sites More sharing options...
Naughty Alien Posted August 23, 2014 Share Posted August 23, 2014 ...i think you should avoid that particular function, if purpose is to get distance for comparison.. then use of ((x2-x1)^2 + (y2-y1)^2) without Sqrt is as good as Sqrt, but much faster, especially because you using Lua.. 1 Quote Link to comment Share on other sites More sharing options...
nick.ace Posted August 23, 2014 Share Posted August 23, 2014 That's a good point Naughty Alien, because from a machine standpoint, a Taylor series is usually used to compute more complex mathematical functions (not too sure about square root, but I would guess so). Would it be much slower for Lua? I would suspect that since it is a built-in Lua function, the interpretation would call the machine code for computing a square root. I feel that the interpretation overhead wouldn't come close to the calculation time. I might be wrong about this though, I just like to learn about these things! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.