• How would I do math+algebra in Lua?
    24 replies, posted
I was wondering how to do math and Algebra particularly in Lua. Sort of like "x+5^2=65 find x" in Lua. I'm using [url]http://www.lua.org/cgi-bin/demo[/url] this to test things.
You can't do it that easily. [editline]07:42PM[/editline] You really need to read up on the capabilities of Lua, try reading the 5.1 Reference Manual and the 1st edition of Programming In Lua. [url=http://www.lua.org/manual/5.1/]here[/url] and [url=http://www.lua.org/pil/]here[/url].
[QUOTE=Chad Mobile;19198784]I was wondering how to do math and Algebra particularly in Lua. Sort of like "x+5^2=65 find x" in Lua. I'm using [url]http://www.lua.org/cgi-bin/demo[/url] this to test things.[/QUOTE] What do you need that for anyway? Unless you want to make an in-game computer algebra system, that's horribly resource intensive and unusable, just solve the equation yourself and then use your solution.
Also, if you look at the math library you can pretty much start combining functions on your own to get your desired equation. Things like sine and cosine are pretty easy to adapt, etc.
[QUOTE=_Kilburn;19199078]What do you need that for anyway? Unless you want to make an in-game computer algebra system, that's horribly resource intensive and unusable, just solve the equation yourself and then use your solution.[/QUOTE] I just want to make a script that solves for x(or any other letter).
Why, though?
[QUOTE=Nerdeboy;19199879]Why, though?[/QUOTE] He just learned how to solve equations probably :v:
[QUOTE=Chad Mobile;19199304]I just want to make a script that solves for x(or any other letter).[/QUOTE] Why don't you just do your homework the normal way.
Lol, this isn't for homework, I just want to explore Lua and what not, find out everything it can do.
[QUOTE=Chad Mobile;19202196]Lol, this isn't for homework, I just want to explore Lua and what not, find out everything it can do.[/QUOTE] I disagree!
Well having Lua solve an equation for you is pointless. Instead of "x+5^2=65" you use "x = 40".
[QUOTE=CowThing;19202387]Well having Lua solve an equation for you is pointless. Instead of "x+5^2=65" you use "x = 40".[/QUOTE] or x = 65 - 5^2.
Computers don't reason. Their name gives their purpose, to compute. Therefore, you're required to give it the specific guidelines as to what it is going to do. Computers, for one, cannot understand that the X there stands for any integer. They would simply detect it as the character "X", and would throw an error since you can't sum different data types. Your first problem would be data types. You would have to turn the equation into a string, separe it's members, simplify what must be simplified, draw the results to every multiplication, addition, substraction, division, exponentiation and root, then draw the results. But isn't it much simpler to just clear the equation yourself and then compute the results?
[QUOTE=Chad Mobile;19198784]I was wondering how to do math and Algebra particularly in Lua. Sort of like "x+5^2=65 find x" in Lua. I'm using [url]http://www.lua.org/cgi-bin/demo[/url] this to test things.[/QUOTE] Writing an equation solver to the fullest extent of the phrase would prove difficult, but if you fix some variables (make them [b]known[/b] instead of [b]variables[/b]) To solve that particular equation you could do: [lua] print("x = "..tostring(65-x^2)) [/lua] Math in lua is very intuitive and straight forward, I suggest you read the manual but this is all valid: [lua] local x; x = 100^2 x = 100+x^2 x = (x+100)/(x^(1/2)) x = (10 % 2)/2 [/lua] [i]Note: Taking anything to the (1/2) power is the same as taking the square root[/i] If you want to make say a quadratic equation solver you could use the quadratic formula to solve. For example: [b]x[/b] = [b]a[/b]*x^2 + [b]b[/b]*x + [b]c[/b] Quadratic forumla: [img]http://3.bp.blogspot.com/_wFU1WLOqOtM/RynX1dDGZBI/AAAAAAAAAAM/kpGxJDIIgco/s400/quadform%5B1%5D.gif[/img] Solver: [lua] --here are you inputs, however you may want to get those via keyboard or what have you local a, b, c; --Check if the result is unreal if ((b^2-4*a*c) < 0) then --can't square a negative number print("Result is unreal.") else --print the results, the "plus or minus" symbol can be emulated by simply doing it twice with a different sign print("x = "..tostring((-b + (b^2-4*a*c)^(.5))/(2*a)))) print("x = "..tostring((-b - (b^2-4*a*c)^(.5))/(2*a)))) end [/lua] Yes I know there are redundant parts of the code (doing the same math multiple times which is bad practice) however I wanted to keep the equation in tack so it was readable.
So much code...:razz: I didn't expect so much! I figured someone would say "fuck off i dont know how" and then the thread would die lol i looked at Howe's reply and was like "holy christ"
Now when you start moving into 2x = 3x - 7 type equations, it'll take alot more than some simple Lua mathematics interpretations to solve for x. As said before me, even computers have trouble at solving simple algebraic equations because they don't reason the same way we do. When we look at the problem 2x = 3x - 7, we can say "Oh, I subtract 3x from 2x giving me -1x". When a computer sees it, it thinks "Oh look, it's a whole lot of English characters that may or may not represent a simple algebraic equation". Once it figures out it's an algebraic equation, or you tell it is, it now thinks "I need x before I can do anything, but I can't solve for x without grouping" So you then begin the long and boring process of tables and math and variables and etc and etc and etc and you CAN come out with the correct answer, but the steps the human coder took to make the computer think as logically as a human are so much more unnecessary than the average 7th-8th grader that it's much better just to say yourself "Ok, I know -1x is on one side, now I can divide that from the other side to get -7/-1." The computer knows how to do -7/-1, therefore your logic made the steps so much easier for the computer and saved you so much time. Wow, I typed all that? :byodood: [editline]12:07AM[/editline] You better not be trolling, I worked hard to explain that :ninja:
I'm not trolling, I just wanted to know how/if you could do long math and stuff in Lua.
Computer algebra systems are not something you can write overnight, they're very complicated. Companies like Texas Instruments have spent years designing the CAS in their calculators today.
Okay, thanks for the replies. Just a question that popped into mah head
[url]http://en.wikipedia.org/wiki/Shunting_yard_algorithm[/url] if you care...
This is where humans are superior...
[QUOTE=Lexic;19202535]or x = 65 - 5^2.[/QUOTE] 65 - 5^2 = 40 That is why I said to use x = 40. There is no need to use operations like that unless you have a variable like x = 65 - y^2 and sometimes when you want to make values easier to read, like 3 minutes = 3*60 seconds instead of 180 seconds. But maybe that's just me.
[QUOTE=haloshadow;19751303][url]http://en.wikipedia.org/wiki/Shunting_yard_algorithm[/url] if you care...[/QUOTE] Heh, I didn't even know there was an actual algorithm for it. I came up with it myself while writing the PHP parser.
Don't bother using Lua, use prolog.
Well someone bumped the thread, I probably wasn't going to try this. I just thought of it while making a simple calculator for Lua.
Sorry, you need to Log In to post a reply to this thread.